1.?在視圖控制器中使用
$this->res()
方法來生成資源文件的絕對路徑
$this->res('css/style.css');
生成的連接為http://youdomain.com/static/css/style.css
2. 生成指定app名稱的連接
$this->appUrl()?第一個參數為基礎url, 第二個參數為app名稱, 第三個參數為 控制器:方法 第四個參數為參數列表, 第五個參數標識是否生成加密連接
3. 在布局文件中調用視圖控制器的方法
直接在布局文件中使用$this->action()
就可以調用視圖控制器中的方法, 如下例
<!doctype html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="renderer" content="webkit"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="Keywords" content="<?php echo isset($keywords) ? $keywords : "默認關鍵詞"); ?>"/><meta name="Description" content="<?php echo isset($description) ? $description : '默認描述'; ?>"/><meta name="viewport" content="width=device-width, initial-scale=1"><title><?php echo isset($title)?$title:'默認標題' ?></title><?php $this->loadRes() ?>
</head>
<body><?php echo isset($content)?$content:'' ?>
</body>
</html>
4. 在模板中生成鏈接
在模板中調用
url
方法, 可以自動生成連接, 如果方法有別名, 會優先使用別名
$this->url('controller:action', array('key'=>'value'));
5. 在控制器中跳轉到其他控制器
$this->to([controller:action, params, sec])
跳轉到指定頁面,該方法實際是一個
$this->view->link()
的連接, 生成url后用header函數跳轉.
6. 在控制器中調用視圖
$this->display([data, method, http_response_status])
調用視圖控制,一個
$this->view->display()
的連接。7. 在控制器中接收參數
假設當前的url為
http://domain/skeleton/htdocs/web/controller/action/p1/1/p2/2
在方法內部使用控制器的?$this->params
?屬性可以獲得參數的值:
namespace app\web\controllers;use Cross\MVC\Controller;class User extends Controller
{function index(){print_r($this->params);}
}
打印結果為一個關聯索引數組 此時?skeleton/app/web/init.php
?中的值為url['type'] = 3
Array ( 'p1' => 1 'p2' => 2 )
打印結果根據app配置文件中url項的設置, 可能會有差異
8. 在控制器中使用modules
在控制器中使用modules,以使用UserModules為例:
namespace app\web\controllers;use Cross\MVC\Controller;class User extends Controller
{function index(){$USER = new UserModules();}
}
如果類中每個action都依賴UserModules
, 可以把初始化UserModules的工作放到構造函數中:
namespace app\web\controllers;use Cross\MVC\Controller;class User extends Controller
{/*** @var UserModule*/protected $USER;function __construct(){parent::__construct();$this->USER = new UserModule();}function index(){}
}
然后就可以在控制器中調用modules提供的方法了.
9. 在視圖模板中訪問指定路由
?
等效于 : http://up.kuman.com/api/getWithdrawInfo(將網址寫成固定,但是這種方式有一個缺點,一般線上和本地調試不會用同一個域名,如果直接寫成固定,到時上線后,還需要逐個更改我們定義的路由)
所以,在crossPHP中,還是推薦這種方式進行路由的指定