顯示具有 Yii 標籤的文章。 顯示所有文章
顯示具有 Yii 標籤的文章。 顯示所有文章

2013-09-09

使用 beginContent() 和 endContent() 設定 Yii 的 layouts

Yii 的 views/layouts 是用來放置 layouts 的目錄,在預設的情況下會有 main.php 和 column1.php 和 column2.php。
main.php 內容定義了,<head> 以及 page header 和 footer 等。 column1.php 和 column2.php 是使用 main.php 的網頁佈局,但修改內容的部份。
例如,我們有個 layout 叫做 mylayout.php:
<?php $this->beginContent('//layouts/main'); ?>
<div>
    <?php echo $content ?>
</div>
<div class="sidebar">
    <ul>
        <li>option 1</li>
        <li>option 2</li>
    </ul>
</div>
<?php $this->endContent() ?>
beginContent('//layouts/main') 表示以 main 為佈局,在  beginContent() 和 endContent() 之間為 content 呈現的修改。 beginContent() 和 endContent() 之外的範圍不建議加入 HTML,否則會在 main.php 內容的前面或後面(<html> 標籤之前或 </html> 標籤之後)。

在 controller 裡可以這樣使用:
public function actionMylayout() {
    $this->layout = 'mylayout';
    $this->render('//site/index');
}
表示以 mylayout.php 為佈局,views/site/index.php 為內容。





2013-09-08

利用 renderPartial() 重複使用 view

Yii 可以利用 renderPartial() 引用某個 view。
例如有個 view 的路行為 views/common/part1.php,內容為:
<table>
    <tr>
        <td>Name</td>
        <td>Phone</td>
    </tr>
    <tr>
        <td><?php echo !empty($name) ? $name : 'anonymous'; ?></td>
        <td><?php echo !empty($phone) ? $phone : ''; ?></td>
    </tr>
</table>

在另外的 view 可以使用 renderPartial() 引用:
<?php
$this->renderPartial('//common/part1', array(
    'name' => 'John',
    'phone'=> '123456',
));
?>
renderPartial() 是 controller 的方法,所以也可以在 controller 裡面使用。
第一個參數為 view 的路徑,路徑中的 // 指的是 views/ 的路徑。
第二個參數為關聯式陣列,包含欲傳入的變數。
第三個參數為「是否回傳」,預設值為 false,就是直接輸出,不回傳。若為 true,則是不輸出,只回傳字串。

2013-09-07

啟用 Yii::trace() 功能

啟用 Yii::trace() 功能有兩個步驟:
1. 在專案目錄的 index.php 中 設定 (預設情況) :
defined('YII_DEBUG') or define('YII_DEBUG',true);

2. 將 config/main.php 中,log component 設定中移除註解:
'log'=>array(
 'class'=>'CLogRouter',
 'routes'=>array(
  array(
   'class'=>'CFileLogRoute',
   'levels'=>'error, warning',
  ),
  // uncomment the following to show log messages on web pages
  /*
  array(
   'class'=>'CWebLogRoute',
  ),
  */
 ),
),

以上設定好之後,會顯示所有 level 為 trace 的訊息,若只要顯示我們 trace 的訊息,可以設定 categories 限定:
'log'=>array(
    'class'=>'CLogRouter',
    'routes'=>array(
        array(
            'class'=>'CFileLogRoute',
            'levels'=>'error, warning',
        ),
        array(
            'class'=>'CWebLogRoute',
            'categories'=>'application',
        ),
    ),
),

2013-08-04

Yii 在網址列隱藏 index.php

Yii 在網址列隱藏 index.php
參考 http://www.yiiframework.com/wiki/214/url-hide-index-php/

.htaccess 檔的內容使用
RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

config/main.php 修改內容
'urlManager'=>array(
    'urlFormat'=>'path',
    'rules'=>array(
      '<controller:\w+>/<id:\d+>'=>'<controller>/view',
      '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
      '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
    ),
    'showScriptName'=>false,
    'caseSensitive'=>false,
),

2013-07-23

Yii 筆記 2 (CRUD)

在 MySQL 建立資料庫和資料表。資料庫連線設定在 protected\config\main.php 的 application components 內,將其設定為 MySQL。

使用 gii 的 Model Generator 建立 Model 檔,欄位填入 Model 類別名及對應的資料表名稱。

使用 gii 的 CRUD Generator 建立 CRUD,欄位填入 Model 類別名及欲新增的 Controller ID。建立之後,新增一個 Controller 類別檔,並在 View 的子目錄裡新增數個 View 內容檔。

表單欄位驗證設定位於 Model 類別的 rules() 方法裡。格式:
array('屬性列表', '驗證器', ...其它設定選項);

驗證器:
boolean:  CBooleanValidator, 布林值 true 或 false。
captcha:  CCaptchaValidator, 圖形驗證碼。
compare:  CCompareValidator, 判斷兩欄位的值是否相等。
email:    CEmailValidator, 有效的 e-mail address。
date:     CDateValidator, 有效的時間值。
default:  CDefaultValueValidator, 是否為預設值。
exist:    CExistValidator, 在資料表中是否已存在。
file:     CFileValidator, 檔案欄位。
filter:   CFilterValidator, 使用 filter 轉換。
in:       CRangeValidator, 某範圍的值。
length:   CStringValidator, 限定資料長度。
match:    CRegularExpressionValidator, 符合某 regular expression。
numerical:CNumberValidator, 數值。
required: CRequiredValidator, 必填欄位。
type:     CTypeValidator, 特定類型的資料。
unique:   CUniqueValidator, 資料表中的唯一資料。
url:      CUrlValidator, 有效的網址格式。

2013-07-21

Yii 筆記 1 (建立 webapp)

下載 yii 並解壓縮到 web server 的 DocumentRoot。以 Yii 的 requirements 檢查環境是否符合需求 http://localhost/yii-1.1.13/requirements/

利用 yiic 建立 webapp
c:\wamp\www\yii-1.1.13\framework>yiic webapp .\..\..\yiitest01

建立的 webapp 並沒有包含 Yii framework, 可以將 c:\wamp\www\yii-1.1.13\framework 複製到 yiitest01/ 裡。然後修改 index.php 裡 $yii 的 framework 路徑。

到 protected/config/main.php 開啟 gii module 設定
 'modules'=>array(
  'gii'=>array(
   'class'=>'system.gii.GiiModule',
   'password'=>'密碼',
   // If removed, Gii defaults to localhost only. Edit carefully to taste.
   'ipFilters'=>array('127.0.0.1','::1'),
  ),
 ),

連到 http://localhost/yiitest01/index.php?r=gii 輸入密碼後,可以看到 gii 預設的功能選單,包含:Controller Generator、Crud Generator、Form Generator、Model Generator、Module Generator。

點選 Controller Generator,在 Controller ID 欄填入 message,Action IDs 欄填入 hello(預設為 index),接著按 "preview" 按鈕,將產生兩個檔案:
controllers\MessageController.php
views\message\hello.php
點按 "Generate" 按鈕,以產生檔案。

點按 "try it now" 時,由於沒有 index action 所以會得到 404
http://localhost/yiitest01/index.php?r=message    // 得到 404
http://localhost/yiitest01/index.php?r=message/hello    // 得到正常頁面

查看 MessageController.php
class MessageController extends Controller
{
    public function actionHello()
    {
        $this->render('hello');
    }
}

也可以設定某個 action 為 default action
public $defaultAction = 'hello';

在 View 裡,變數對應的關係:
$this    // controller object
$this->id    // controller id
$this->action->id    // action id
echo CHtml::link('連結字串', array('控制器/動作')); // 顯示連結

FB 留言