Minor [PHP framework] 3. routing, controller, view, minor framework _ PHP Tutorial

Source: Internet
Author: User
Minor [PHP framework] 3. routing, controller, view, and minor framework. Minor [PHP framework] 3. routing, controller, and view. minor framework 3.1 beautiful routing URLs are absolutely required by a serious web application. in this way, index. php? Article_id Minor [PHP framework] 3. routing, controller, view, and minor framework

3.1 Route

Beautiful URLs are definitely required by a serious web application. in this way, index. php? Ugly URLs such as article_id = 57 are hidden and replaced by more popular ones such as/read/intro-to-symfony.

  3.1.1 route configuration

The configuration file is app/Config/routes. php.

   [        'name'                =>    'test1',        'controller'        =>    'App\Modules\Demo\Controller\FooController',        'action'            =>    'bar',        'required'          =>  ['productName' => '\w+'],    ],];

The code above is used as an example to describe how to configure an elegant (laravel diseased body...) route

'/Demo/{productName}' is the routing matching rule. in the configuration, required specifies the regular condition that productName must meet, in the actual running process of the framework, the routing rule + required will be parsed into a regular expression/demo/(\ w +), when the url (eg: xxx. xxx. when xxx/demo/testproduct) matches this regular expression, the controller action in the configuration is executed. the specific execution is: FooController-> bar ($ productName );

We can see that the content (productName) in the braces in the routing rule is the parameter of the bar Method. Therefore, when matching a route, you must note that the routing rules must be consistent with the number of parameters of the specific controller method. Otherwise, the ControllerException will be thrown.

  3.1.2 default route configuration

If we need to configure a route for each defined Controller, the development efficiency will be low. to prevent this problem, Minor provides a default routing mechanism. When we access the ingress:

Http://xxx.xxx.xxx/{ }/{ }}/{}

  3.1.3 Disadvantages

As you can see, Minor's routing is not powerful. Request method restrictions are not supported, htttps restrictions are not supported, and filters are not supported.

3.2 Controller

  3.2.1 create a controller of your own

The definition of Controller is very simple, as long as it inherits the basic class of Minor \ Controller (of course you can also do not inherit, but the methods and attributes in the base class cannot be used, first, create the app/Modules/Demo/Controller/folder, and then create the file FooController. php:

 

By accessing xxx. xxx. xxx/demo/foo/bar (default route, you can also configure your own route), you can see that Hello World is returned.

  3.2.2 Url generation

You can call the gen method of the Url to convert the default path to a url that complies with the routing rules.

$url = Url:gen($path);

For example, configure Url: gen ('/demo/foo/bar? ProductName = test') returns/demo/test.

  

  3.2.3 page jump redirect and redirection forward

Minor provides three redirection methods: redirect, forward, and forwardUrl (both are the protected methods of Minor \ Controller ).

When you jump to another url, you can call $ this-> redirect ($ url) in the controller );

When switching to another url, you can call $ this-> forwardUrl ($ url) in the controller ); (the implementation of this method is to parse the controller and method of the url request through routing and then call forward ($ controller, $ action, $ param ))

When turning to another method, you can call it in the controller as follows: $ this-> forward ($ controller, $ action, $ params ); (the parameter $ controller is the class name of the controller, including the namespace)

Example:

class FooController extends Controller{    public function bar()    {        $this->redirect('www.baidu.com');        return $this->forward('App\Modules\Demo\Controller\FooController', 'bar', 'test');        return $this->forward('/demo/testpro');    }}    

  3.2.4 obtain request parameters

Call the get ($ paramName, $ defaultParamValue = null) or post ($ paramName, $ defaultParamValue = null) method of MinorRequest to obtain the request method. in the controller, you can call the following method:

class FooController extends Controller{    public function bar()    {       $minorRequest = $this->app->getMinorRequest(); 
    $paramValue = $minorRequest->get('paramKey', 'defaultValue');
    ... }}

  3.2.5 request retrieval method

Call the getMethod () method of MinorRequest to obtain the request method:

class FooController extends Controller{    public function bar()    {       $minorRequest = $this->app->getMinorRequest();     $method = $minorRequest->getMethod();    ...    }}  

3.3 View

Minor provides an extremely powerful template engine named PHP. Yes! It's PHP. Why does Minor not provide a template engine like smarty or Twig? Because there is no need, PHP itself is good enough. if Minor creates a template engine, it will undoubtedly make Minor more difficult to use. Therefore, Minor directly uses PHP as the view file language.

  3.3.1 use a view in the controller

To use a View in a controller, you only need to call View: render ('Module name: Controller name: View filename ', ['param1key' => 'param1value ', 'param2key' => 'param2value'...]); example:

class FooController extends Controller{    public function bar()    {        $param1 = 'Hello';        $param2 = 'World';         return View::render('Demo:Foo:bar.php', ['param1' => $param1, 'param2' => $param2]);    }}  

The second parameter of the render function (['param1key' => 'param1value', 'param2key' => 'param2value'...]) it is the variables passed to the view file. we can use these variables in the view file:

File: app/Modules/Demo/Controller/Tpl/Foo/bar. php

  3.3.2 view built-in functions

Minor provides functions that can be used in two view files:

function include_tpl($module, $controller, $tpl){    require_once (!defined('APP_DIR') ? APP_DIR : realpath(__DIR__ . '/../../app/') .DIRECTORY_SEPARATOR) . 'Modules' . '/' . $module . '/Tpl/' . $controller . '/' . $tpl;}function url($path){    return Url::gen($path);}

Usage:

html>       
               

Hello!

">

These two functions are defined in the app/Resource/functions. php file. you can customize the View functions you need in this file.

 

3.4 Response

You can obtain the current MinorResponse object by calling the getMinorResponse () method of the App object in the controller. The MinorResponse class provides the following six methods:

Public function send (); // send the response object to the public function setHeader ($ header) of the client; // set the public function setContent ($ content) of the response header ); // set the public function beforeContent ($ content) of the response object; // add the public function appendContent ($ content) before the existing content ); // Append the public function getContent () after the existing content; // Obtain the response content of the object

Ingress PHP framework] 3. routing, controller, view, minor framework 3.1 beautiful routing URL is absolutely a serious web application must do, this method makes index. php? Article_id...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.