Minor "PHP framework" 3. Routing, Controller, view, Minor framework
3.1 Routing
A pretty URL is definitely something a serious web application must do, and this way makes the ugly URLs of index.php?article_id=57 such as hidden and replaced by more popular like/read/intro-to-symfony.
3.1.1 Routing Configuration
Configuration file is app/config/routes.php
PHPreturn [ '/demo/{productname} ' = [ ' name ' = ' = ' Test1 ', ' controller ' = ' App\modules\demo\controller\foocontroller ', ' action ' = > ' Bar ', ' required ' = [' productName ' = ' \w+ '], ],;
Take the top code as an example to detail how to configure an elegant (laravel sick upper ...) The routing
'/demo/{productname} ' is a matching rule for routing, where the required in the configuration is the regular condition that productName must meet, and the routing rules +required resolved to regular expressions during the actual operation of the framework/demo/ (\w+), the action of the controller in the configuration is executed when the URL (eg:xxx.xxx.xxx/demo/testproduct) matches to this rule, with the following execution: Foocontroller->bar ($ ProductName);
You can see that the contents of the curly braces in the routing rules (ProductName) are just the parameters of the bar method. Therefore, when matching routes, it is important to note that the routing rules must be consistent with the number of parameters of the specific controller's method, or the controllerexception will be thrown.
3.1.2 Default Routing configuration
If we were to configure a single controller to have a route that would lead to less efficient development, minor provides a default routing mechanism to prevent this problem from occurring. When we access Http://xxx.xxx.xxx/demo/foo/bar, we execute the App\modules\demo\foocontroller bar method, which is the default route:
http://xxx.xxx.xxx/{Module name}/{Controller name}/{method name}
3.1.3 Disadvantages
As you can see, minor's routing is not powerful. Restrictions on request methods are not supported, HTTTPS restrictions are not supported, 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 you inherit the Minor\controller\controller base class (of course you can not inherit, but the methods and properties in the base class will not be used, which is very well understood), first create the folder App/modules /demo/controller/, and then create the file foocontroller.php:
Phpnamespace App\modules\demo\controller; Use Minor\controller\controller; // Define a controller class extends controller{ // Define a method public function Bar () { return ' Hello world '; }}
By accessing Xxx.xxx.xxx/demo/foo/bar (the default route, you can also configure your own route) you can see that Hello World is returned.
3.2.2 URL Generation
The Gen method that invokes the URL can convert the default path to a URL that conforms to the routing rule
$url = Url:gen ($path);
Url::gen ('/demo/foo/bar?productname=test ') will return/demo/test as per the routing configuration in 3.1.1.
3.2.3 Page Jump Redirect, redirect forward
Minor provides three jump methods, namely redirect, Forward, Forwardurl (these three minor\controller\controller methods are protected).
When jumping to a different URL can be called in the controller: $this->redirect ($url);
When the steering (froward) to another URL can be called in the controller: $this->forwardurl ($url), (the implementation of this method is actually through the route to resolve the URL request of the Controller and method and then call forward ($ Controller, $action, $param))
When the steering (forward) to another method can be called in the controller: $this->forward ($controller, $action, $params); (Parameter $controller is the class name of the controller, including the namespace)
Cases:
class extends controller{ publicfunction Bar () { $this->redirect ( ' www.baidu.com '); return $this->forward (' App\modules\demo\controller\foocontroller ', ' bar ', ' Test '); return $this->forward ('/demo/testpro ');} }
3.2.4 GET Request Parameters
Call Minorrequest's Get ($paramName, $defaultParamValue = null) or post ($paramName, $defaultParamValue = null) method to get the request method, This can be called in the controller:
class extends controller{ publicfunction Bar () { $this->app-> Getminorrequest ()
$minorRequest->get (' Paramkey ', ' DefaultValue ');
... }}
3.2.5 Get Request method
Call Minorrequest's GetMethod () method to get the requested method:
class extends controller{ publicfunction Bar () { $ Minorrequest$this->app->getminorrequest (); $method$minorRequest, GetMethod (); ... }}
3.3 views
Minor provides an extremely powerful template engine with the name of the template engine: PHP. Yes! It's PHP that you read correctly. Why does minor not provide a template engine similar to Smarty or twig? Because there is no need, PHP itself is good enough, if minor to recreate a template engine will undoubtedly make minor more difficult to get started, so minor directly use PHP as the language of the view file.
3.3.1 using views in the Controller
Using the view in the controller requires only calling View::render (' module name: Controller name: View filename ', [' param1key ' = ' param1value ', ' param2key ' = ' param2value ' ...]) Cases
class extends controller{ publicfunction Bar () { $param 1 = ' Hello ' ; $param 2 = ' world '; return $param 1 $param 2 ]); }}
The second parameter of the render function ([' param1key ' + ' = ' param1value ', ' param2key ' = ' param2value ' ...] ) is the variable passed to the view file, which we can use in the view file:
Files: 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 (! Definedrealpath$module$controller$tpl;} function url ($path) { return url::gen ($path);}
Use:
html>
php include_tpl (' Public ', ' public ', ' header.php '); ?> < Body > < H1 > Hello!
H1> <href= "
" >
body>
html>
These two functions are defined in the app/resource/functions.php file, and you can customize the view functions you need in this file.
3.4 Response
The current Minorresponse object can be obtained in the controller by invoking the Getminorresponse () method of the App object. The Minorresponse class provides six methods, namely:
Public function Send ();//used to send the response object to client public function SetHeader ($header);//Set Response header Public Function setcontent ($content) ; Sets the content of the Response object public function beforecontent ($content); Add content before current content public function appendcontent ($content); Append content after current existing content public function getcontent ();//Gets the response content in the object
http://www.bkjia.com/PHPjc/1135100.html www.bkjia.com true http://www.bkjia.com/PHPjc/1135100.html techarticle Minor "PHP framework" 3. Routing, Controllers, views, Minor Framework 3.1 routing Pretty URLs are definitely a serious web application that must do, this way makes index.php?article_id ...