This article mainly introduces ThinkPHP, ZF2, Yaf, and Laravel framework route competition materials. For more information, see
This article mainly introduces ThinkPHP, ZF2, Yaf, and Laravel framework route competition materials. For more information, see
Preface
I have read a technical article about Zend Framework2, ZF2 multi-level Tree Routing Route configuration instance, which introduces Route configuration. I think it is interesting. This is the requirement:
/User corresponding user list page
/User/: user_id corresponds to the user's personal homepage. For example,/user/AlloVince corresponds to the user's personal homepage of AlloVince.
/User/: user_id/blog/the corresponding user's blog list page. For example,/user/AlloVince/Blog lists the blogs written by AlloVince.
/User/: user_id/blog/: a blog post of the user corresponding to blog_id
The solution is referenced in the original article:
'Router' => array ('routes '=> array ('user' => array ('type' => 'segment ', 'options' => array ('route '=>'/user [/] ', 'defaults' => array ('controller' => 'usercontroller ', 'action' => 'index',),), 'may _ terminate' => true, 'Child _ routes '=> array ('profile' => array ('type' => 'segment ', 'options' => array ('route' => '[: id] [/] ', 'straints' => array ('id' =>' [a-zA-Z0-9 _-] + '), 'defaults' => array ('action' => 'get'),), 'may _ terminate' => true, 'Child _ routes '=> array ('blog' => array ('type' => 'segment ', 'options' => array ('route '=> 'blog [/]', 'straints' => array (), 'defaults' => array ('action' => 'blog '), 'may _ terminate' => true, 'Child _ routes '=> array ('post' => array ('type' => 'segment', 'options' => array ('route '=> '[: post_id] [/] ', 'straints' => array ('Post _ id' =>' [a-zA-Z0-9 _-] + '), 'defaults' => array ('action' => 'post'), 'may _ terminate' => true ,),),),), // profile child_routes end), // profile end), // user child_routes end), // user end ),),
After reading this article, I plan to use the PHP framework I have used to implement this routing requirement.
ThinkPHP
Create a ThinkPHP project:
The Code is as follows:
Composer create-project topthink/thinkphp tp -- prefer-dist
The command line shows that I installed 3.2.2.
Installing topthink/thinkphp (3.2.2)
I think the latest stable version of ThinkPHP is 3.2.3.
I went to the packagist official website to check whether the stable version in the library is 3.2.2.
I need to use 3.2.3. Why do I have to worry about this? Because:
3.2 of the routing function is set for the module, so the module name in the URL cannot be routed, and the routing definition is usually placed in the module configuration file. 3.2.3 supports global routing definition. You can define routes in the public configuration file of the project.
That is to say, the routing rewrite part is the Controller and Action part, and the Moudle still exists.
I want/user instead of home/user. (In ThinkPHP, the DEFAULT Module is Home, 'default _ module' => 'home', which can be modified)
Of course, this problem can also be solved by modifying the. htaccess file. However, I decided to install 3.2.3.
Download the latest package from the ThinkPHP website and decompress it.
Use a browser to access the project's entry file, so that ThinkPHP automatically generates a default application module Home.
Modify the public configuration file tp \ Application \ Common \ Conf \ config. php:
<? Phpreturn array (// enable routing 'url _ ROUTER_ON '=> true, // URL access mode. Optional parameters: 0, 1, 2, and 3, which indicate the following four modes: // 0 (normal mode); 1 (PATHINFO mode); 2 (REWRITE mode); 3 (compatible mode) the default PATHINFO mode is 'url _ model' => 2, // set the URL pseudo-static suffix. If it is null, all static suffixes are supported. // when you use the U function to generate a URL without the suffix 'url _ HTML_SUFFIX '=> '', // The URL variable is bound to the Action method parameter. The default value is true 'url _ PARAMS_BIND '=> true. // The Type 0 bound to the URL variable is bound to variable name 1 and variable order, the default value is 0 'url _ PARAMS_BIND_TYPE '=> 0, // route configuration 'url _ ROUTE_RULES' => ar Ray ('/^ url $/' => 'home/User/url', '/^ user $/' => 'home/User/Index ', '/^ user \/([a-zA-Z0-9 _-] +) $/' => 'home/User/show? Name =: 1', '/^ user \/([a-zA-Z0-9 _-] +) \/blog $/' => 'home/Blog/index? Name =: 1', '/^ user \/([a-zA-Z0-9 _-] +) \/blog \/([0-9] +) $/'=> 'home/Blog/show? Name =: 1 & blog_id =: 2',),);?>
Create the tp \ Application \ Home \ Controller \ UserController. class. php file:
<? Phpnamespace Home \ Controller; use Think \ Controller; class UserController extends Controller {public function url () {$ name = 'jing'; $ blogId = 1; $ urls = array (U ('/user'), U ("/user/{$ name }"), U ("/user/{$ name}/blog"), U ("/user/{$ name}/blog/{$ blogId }"),); foreach ($ urls as $ url) {echo "{$ url}
\ N ";}} public function index () {echo 'I am the user list ^_^';} public function show ($ name) {echo" Welcome, {$ name} ";}}?>
Create the tp \ Application \ Home \ Controller \ BlogController. class. php file:
<? Phpnamespace Home \ Controller; use Think \ Controller; class BlogController extends Controller {public function index ($ name) {echo "this is the blog list of {$ name ";} public function show ($ blog_id, $ name) {echo "{$ name}'s blog id is {$ blog_id}" ;}}?>
Access:
Output:
The Code is as follows:
/Tp/user
/Tp/user/jing
/Tp/user/jing/blog
/Tp/user/jing/blog/1
Access the above four links and return them in sequence:
I am a user list ^_^
Welcome, jing
This is a list of jing blogs.
Jing's blog id is 1
Other frameworks below also output the above content.
Zend Framework 2
Use the ZF2 skeleton program to create a ZF2 project:
Composer create-project -- stability = "dev" zendframework/skeleton-application zf2
Modify the configuration file zf2 \ module \ Application \ config \ module. config. php of the default module Application: