Generate routes using annotations
As long as comments are generated according to PHPDOC rules, valuable routing information can be extracted
Annotation routing
ThinkPHP supports the use of annotations to define routes (also called annotation routing). This is a simple route registration method (basic route definition can be completed). It is closed by default. If you need to enable it, set it in the routing configuration file. :
// 开启注解路由 'route_annotation' => true,
If you need to use annotation routing, you need to install additional extensions:
composer require topthink/think-annotation
Then you only need to define it directly in the method annotation of the controller class, for example:
<?php namespace app\controller; class Index { /** * @param string $name 数据名称 * @return mixed * @route('hello/:name') */ public function hello($name) { return 'hello,'.$name; } }
@ route('hello/:name') is the content of the annotation route. Please be sure to pay attention to the specification of the annotation. Otherwise, the annotation route parsing may fail. You can use the IDE to generate standardized annotations. If you use PHPStorm, it is recommended to install the PHP Annotations plug-in: https://plugins.jetbrains.com/plugin/7320-php-annotations , which can support the automatic completion of annotations.
The routes defined in this way take effect in real time in debugging mode. In deployment mode, you need to use the optimize:route command to generate a routing rule file.
Note that @route((is case-sensitive, there cannot be a space between route and (), it is recommended that the route definition be written in the last paragraph of the comment, otherwise a blank line is required after it.
Then use the following URL address to access:
http://tp5.com/hello/thinkphp
Page output
hello,thinkphp
The default registered routing rule supports all requests. If you need to specify the request type, you can specify it in the second parameter. Request type:
<?php namespace app\controller; class Index { /** * @param string $name 数据名称 * @return mixed * @route('hello/:name','get') */ public function hello($name) { return 'hello,'.$name; } }
If there are routing parameters and variable rules that need to be defined, you can add methods directly at the end, for example:
<?php namespace app\controller; class Index { /** * @param string $name 数据名称 * @route('hello/:name','get') * ->https() * ->pattern(['name' => '\w+']) * * @return mixed */ public function hello($name) { return 'hello,'.$name; } }
Note that there is no need to add at the end of adding routing parameters and variable rules; , and make sure there is a blank line between other comments that follow.
Supports defining resource routes in class comments, for example:
<?php namespace app\controller; /** * @route('blog') */ class Blog { public function index() { } public function read($id) { } public function edit($id) { } }
If you need to define routing groups, you can use
<?php namespace app\controller; use think\annotation\route\Group; use think\annotation\route\Route; /** * @Group("blog") */ class Blog { /** * @param string $name 数据名称 * @return mixed * @Route("hello/:name", method="GET") */ public function hello($name) { return 'hello,'.$name; } }
The annotation routing in the current controller will be automatically added to the blog group. Finally, a routing rule for blog/hello/:name will be registered. You can also set public parameters for this routing group, for example:
<?php namespace app\controller; use think\annotation\route\Middleware; use think\annotation\route\Group; use think\annotation\route\Route; use think\middleware\SessionInit; /** * @Group("blog",ext="html") * @Middleware({SessionInit::class}) */ class Blog { /** * @param string $name 数据名称 * @return mixed * @Route("hello/:name",method="GET") */ public function hello($name) { return 'hello,'.$name; }