[TOC]
### 基本視圖的使用
控制器`Index`:
~~~php
<?php
namespace module\index\controller;
use lying\base\Controller;
class IndexCtrl extends Controller
{
public function index()
{
return $this->render();
}
}
~~~
在控制器對應視圖目錄`module/index/view/index/`下定義視圖文件`index.html`:
~~~
<h1>歡迎使用Lying</h1>
~~~
當你訪問這`index`方法的時候,你就能看見大大的'歡迎使用Lying'。
如果帶上參數:
~~~php
<?php
namespace module\index\controller;
use lying\base\Controller;
class IndexCtrl extends Controller
{
public function index()
{
return $this->render('welcome');
}
}
~~~
那么對應的視圖文件就是`module/index/view/index/welcome.html`咯
### 視圖賦值
控制器`Index`:
~~~php
<?php
namespace module\index\controller;
use lying\base\Controller;
class IndexCtrl extends Controller
{
public function index()
{
$this->assign('name', 'Lying');
$this->assign(['id'=>10, 'sex'=>'男']);
return $this->render();
}
}
~~~
在控制器對應視圖目錄`module/index/view/index/`下定義視圖文件`index.html`:
~~~php
<h1>歡迎使用{$name} {$id} {$sex}</h1>
~~~
當你訪問這`index`方法的時候,你還是能看見大大的`'歡迎使用Lying 10 男'`。
### 視圖文件路徑
> 如果你的控制器名不想和視圖文件夾名稱對應,或者想使用其他視圖目錄下的視圖文件或者布局文件,你可以這樣做:
~~~php
return $this->render('welcome'); //使用當前module下'view/控制器ID/welcome.html'
return $this->render('my/welcome'); //使用當前module下'view/控制器ID/my/welcome.html'
return $this->render('/welcome'); //使用當前module下'view/welcome.html'
return $this->render('/welcome/t'); //使用當前module下'view/welcome/t.html'
~~~
### 使用自定義視圖路徑
視圖的路徑默認是 `module/當前模塊/view`,你可以自定義模板路徑:
~~~php
<?php
namespace module\index\controller;
use lying\base\Controller;
class IndexCtrl extends Controller
{
public function init()
{
parent::init();
$this->viewPath = DIR_ROOT . DS . 'tpl'; //自定義視圖路徑
}
public function index()
{
//這時候模板路徑就是 DIR_ROOT . DS . 'tpl/index/index.html'
return $this->render();
}
}
~~~
- 序言
- 更新日志
- 安裝
- 規范
- 常量
- 配置
- 自動加載
- MVC
- 模塊
- 控制器
- 模型
- 視圖
- php原生模板
- 模板引擎
- 變量輸出
- 模板注釋
- 模板繼承
- 模板引用
- 流程控制
- 原樣輸出
- 服務組件
- Hook組件
- Request組件
- Router組件
- Cookie組件
- Encrypter組件
- Dispatch組件
- Response組件
- View組件
- Session組件
- Helper組件
- 數據分頁
- 數據驗證
- Logger組件
- Cache組件
- Redis組件
- Connection組件
- 執行sql語句
- 查詢生成器
- 查詢方法詳解
- Schema
- Captcha組件
- CLI
- CLI工具
- 事件
- 類事件
- 實例事件
- 全局事件
- 助手函數
- 擴展
- 異常
- 部署
- Apache
- Nginx
- IIS
- 虛擬主機