Common operations of the CrossPHP framework

1.  Use the method in the view controller $this->res() to generate the absolute path to the resource file
$this->res('css/style.css');

The resulting connection ishttp://youdomain.com/static/css/style.css


2. Generate a connection with the specified app name

$this->appUrl()  The first parameter is the base url, the second parameter is the app name, and the third parameter is the controller: the fourth parameter of the method is the parameter list, and the fifth parameter identifies whether to generate an encrypted connection


3. Call the view controller's method in the layout file

$this->action()You can call the method in the view controller by using it directly in the layout file , as in the following example

<!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. Generate links in templates

Call the url method connection can be automatically generated, if the method has an alias, the alias will be used first

$this->url('controller:action', array('key'=>'value'));


5. Jump to other controllers in the controller

$this->to([controller:action, params, sec])

Jump to the specified page, this method is actually a connection, after generating the url, use the header function to jump.   $this->view->link()  

6. Call the view in the controller
$this->display([data, method, http_response_status])

Invoke view control, $this->view->display() a connection of .

7. Receive parameters in the controller


Suppose the current url is
http://domain/skeleton/htdocs/web/controller/action/p1/1/p2/2

Use the controller's property inside the method to get the parameter's value: $this->params 

namespace app\web\controllers;

use Cross\MVC\Controller;

class User extends Controller
{
    function index()
    {
        print_r($this->params);
    }
}

The result is printed as an associative index array and the value in this case is skeleton/app/web/init.php url['type'] = 3

Array ( 'p1' => 1 'p2' => 2 )

The print result may vary according to the setting of the url item in the app configuration file

8. Using modules in controllers


Use modules in controllers, take UserModules as an example:
namespace app\web\controllers;

use Cross\MVC\Controller;

class User extends Controller
{
    function index()
    {
        $USER = new UserModules();
    }
}

If every action in the class depends on UserModulesit, you can put the work of initializing UserModules in the constructor:

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()
    {

    }
}

Then you can call the methods provided by modules in the controller.


9. Access the specified route in the view template

 

等效于 : http://up.kuman.com/api/getWithdrawInfo(将网址写成固定,但是这种方式有一个缺点,一般线上和本地调试不会用同一个域名,如果直接写成固定,到时上线后,还需要逐个更改我们定义的路由)

所以,在crossPHP中,还是推荐这种方式进行路由的指定


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643413&siteId=291194637