CrossPHP框架的常用操作

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中,还是推荐这种方式进行路由的指定


猜你喜欢

转载自blog.csdn.net/m_nanle_xiaobudiu/article/details/79550691