phpのYiiフレームワークを使ってサイトを作ってみた

phpのYiiフレームワークを使ってサイトを作ってみました。作った過程で、分かったこと、困ったことなどを備忘録を兼ねてまとめていきます。

今回は、サイト作成時に導入したエクステンションを2つ紹介します。

 

※利用したYiiフレームワークのバージョンは1.1.12です。

※導入や基本的な使い方、Tipsなどは、Yii公式ガイドが詳しいので、まず、ガイドを読みましょう。

 

dbprofiler

Yiiでデータベースを利用した際に、発行したSQL、かかった時間などを表示してくれるエクステンションです。無駄なSQLが発行されていないか、また、遅いSQLはないかなどを確認するのに便利です。

 

Yii-bootstrap

YiiでBootstrapを利用できるようにするエクステンションです。このエクステンション以外にもYii-Boosterというすごくゴージャスなのもあるんですが、処理がどうも重いので、今回はYii-Bootstrapを利用します。

なお、作成に利用したのは、yii-bootstrap-1.2.0.r300.zipです。現時点で同エクステンションを確認したところ、導入方法が違っていますので、1.2.0.r300での導入方法を記しておきます。

1)

yii-bootstrapエクステンションを解凍して、yiiフレームワークのprotect/extensions/bootstrap/以下に設置します。

 

2)

設置したextensions/bootstrap/gii/bootstrap/templates/default/_search.phpにはバグがありますので修正しておきます。修正箇所は、23行目に

'buttonType'=>'submit'

の末尾に「,」を追加して

'buttonType'=>'submit',

のようにすればOKです。

 

3)

protected/config/mail.phpにbootstrap関係の設定を追加します。

return array(

  //bootstrap
  'theme'=>'bootstrap',

  // preloading 'log' component
  'preload'=>array(
    'log',
    //bootstrap
    'bootstrap'
  ),

  'modules'=>array(
     // uncomment the following to enable the Gii tool

     'gii'=>array(
      'class'=>'system.gii.GiiModule',
       'password'=>'hogehoge',
       // If removed, Gii defaults to localhost only. Edit carefully to taste.
       'ipFilters'=>array('127.0.0.1','::1'),
       //bootstrap
       'generatorPaths'=>array('bootstrap.gii')
     ),
  ),

  // application components
  'components'=>array(
    //bootstrap
    'bootstrap'=>array(
      'class'=>'application.extensions.bootstrap.components.Bootstrap',
      'responsiveCss'=>true,
    ),
  ),
);

 

4)

themes/以下にbootstrapディレクトリを作成します。さらに、このディレクトリ以下に以下のような感じにディレクトリとファイルを用意します。

themes/
  bootstrap/
    css/
      main.css
    views/
      layouts/
        column1.php
        column2.php
        main.php
      .htaccess

各ファイルの内容は以下のような感じです。

main.css

#page
{
    margin-top: 60px;
}

column1.php

<?php /* @var $this Controller */ ?>
<?php $this->beginContent('//layouts/main'); ?>
<div id="content">
<?php echo $content; ?>
</div><!-- content -->
<?php $this->endContent(); ?>

column2.php

<?php /* @var $this Controller */ ?>
<?php $this->beginContent('//layouts/main'); ?>

<div class="visible-phone">
    <?php
        $this->widget('bootstrap.widgets.TbButtonGroup', array(
            'buttons'=>array(
                array('label'=>'操作', 'icon'=>'cog', 'items'=>$this->menu),
            )
        ));
    ?>
</div>

<div class="row">
<div class="span9">
  <div id="content">
    <?php echo $content; ?>
  </div><!-- content -->
</div>
<div class="span3 hidden-phone">
  <div id="sidebar">
  <?php
    $this->beginWidget('zii.widgets.CPortlet', array(
      'title'=>'操作',
    ));
        $this->widget('bootstrap.widgets.TbMenu', array(
            'type'=>'pills',
            'stacked'=>true,
            'items'=>$this->menu,
            'htmlOptions'=>array('class'=>'operations'),
        ));
    $this->endWidget();
  ?>
  </div><!-- sidebar -->
</div>
</div>
<?php $this->endContent(); ?>

main.php

<?php /* @var $this Controller */ ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="language" content="en" />

    <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/styles.css" />

  <title><?php echo CHtml::encode($this->pageTitle); ?></title>

  <?php Yii::app()->bootstrap->register(); ?>
</head>

<body>

<?php $this->widget('bootstrap.widgets.TbNavbar',array(
    'items'=>array(
        array(
            'class'=>'bootstrap.widgets.TbMenu',
            'items'=>array(
                array('label'=>'Home', 'url'=>array('/site/index')),
                array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
                array('label'=>'Contact', 'url'=>array('/site/contact')),
                array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
                array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
            ),
        ),
    ),
)); ?>

<div class="container" id="page">

  <?php if(isset($this->breadcrumbs)):?>
    <?php $this->widget('bootstrap.widgets.TbBreadcrumbs', array(
      'links'=>$this->breadcrumbs,
    )); ?><!-- breadcrumbs -->
  <?php endif?>

  <?php echo $content; ?>

  <div class="clear"></div>

  <div id="footer">
    Copyright © <?php echo date('Y'); ?> by My Company.<br/>
    All Rights Reserved.<br/>
    <?php echo Yii::powered(); ?>
  </div><!-- footer -->

</div><!-- page -->

</body>
</html>

.htaccess

deny from all

 

以上で作成前の下準備は完了です。

 

 

2013/4/19追記。

ちなみに作成したサイトは、EventJamというイベント情報などを扱うサイトです。

よろしければアクセスしてみてください。

投稿日:
カテゴリー: php タグ: