ThinkPHP02: Routing
1. Route definition
-
Routing is enabled by default and can be turned off in config/app.php.
-
The routing configuration is in config/route.php, and the routing is defined in route/app.php.
-
rule()
The default request is any.Route::rule("details/:id", "Address/details", "GET|POST");
-
Others include get, post, etc.
Route::get("details/:id", 'Address/details');
2. Variable rules
-
The system default routing variable rule is \w+, which means letters, numbers, Chinese and underscores, which can be changed in config/route.php.
-
Set variable rules in routes.
Route::get("details/:id", 'Address/details')->pattern(["id" => '\d+']);
-
Dynamic combination assembly, addresses and parameters are dynamic.
Route::get("hello-<name>-<id>", 'Hello:name/details')->pattern(["id" => '\d+']);
-
Closure routing can be executed directly through the URL without the need for controllers and methods, and also supports passing parameters and dynamic rules.
Route::get('think/:name', function ($name) { return 'hello, '.$name.'!'; });
3. Routing address
-
The routing address is generally: controller/operation method.
-
Multi-level controller routing.
Route::rule('blog/:id', 'group.Blog/details');
-
Complete route
Route::rule('ds/:id', "\app\controller\Address@details");
-
route redirect
Route::redirect("ds/:id", "http://localhost:8000", 302);
4. Routing parameters
-
When setting up routing, you can set related methods to implement match detection and behavior execution.
-
ext()
The method is used to detect URL suffixes, force all URL suffixes, anddenyExt()
prohibit the use of suffixes.Route::get("details/:id", 'Address/details')->ext('html|shtml');
-
https()
Method is used to detect whether it is https protocol.Route::get("details/:id", 'Address/details')->https();
-
domain()
The method checks whether the current domain name matches, either the full domain name or the subdomain name.Route::get("details/:id", 'Address/details')->domain("localhost");
-
ajax/pjax/json
Used to detect whether the current page is the above request method.Route::get("details/:id", 'Address/details')->ajax();
-
filter()
Used to detect additional parameters, which can be submitted in the form.Route::get("details/:id", 'Address/details')->filter(['id' => 5, "type" => 1]);
-
append()
Method is used to append additional parameters.Route::get("details/:id", 'Address/details')->append(['status' => 1]);
-
allowCrossDomain()
Method can resolve cross-origin requests.oute::get("details/:id", 'Address/details')->allowCrossDomain([ "Access-Control-Allow-Origin" => "*" ]);
-
option()
Used to configure multiple parametersRoute::get("details/:id", 'Address/details')->option([ 'ext' => 'html', 'https' => true ]);
5. Routing grouping
-
Route grouping can combine routes with the same prefix into groups to simplify route definition.
Route::group("address", function () { Route::rule("ds/:id", "/details"); Route::rule("rd/:id", "/read"); })->prefix('Address');
-
The files defined by routing rules will be parsed and consume more resources when loading. You can enable delayed parsing in config/route.php, and the parsing will only be registered when there is a match.
'url_lazy_route' => true,
6. MISS
-
MISS will automatically jump to MISS when it cannot match the corresponding rules.
-
Global MISS.
# app/controller/Error.php public function miss() { return "404 Not Found"; } # route/app.php Route::miss('public/miss');
-
Group MISS
# app/controller/Address.php public function miss() { return '404 Address'; } # route/app.php Route::group("address", function () { Route::rule("ds/:id", "/details"); Route::rule("rd/:id", "/read"); Route::miss("miss"); })->prefix('Address');
7. Resource routing
-
The system provides commands to quickly generate resource controllers.
php think make:controller Blog
-
Register resource routing. After successful registration, the CURD method will be automatically provided without manual registration. The request methods include GET, POST, PUT, and DELETE.
Route::resource('blog', 'Blog'); # 自动注册 http://localhost:8000/blog/ (index) http://localhost:8000/blog/5 (read) http://localhost:8000/blog/5/edit (edit)
-
The default parameter takes the id name. Can also be customized
# route/app.php Route::resource('blog', 'Blog')->vars(['blog' => 'blog_id']); # app/controller/Blog.php public function read($blog_id) { return "显示指定的资源: ". $blog_id; }
-
only()
Used to limit resource methods andexcept()
exclude resource methods provided by the system.Route::resource('blog', 'Blog')->only(['index', 'read']); Route::resource('blog', 'Blog')->except(['delete', 'update']);
-
rest()
Change the default method given by the system and place it in front of the resource method. The identifier of the resource route cannot be changed, but the generated routing rules and corresponding operation methods can be modified.# rest方法要放在resource前面 Route::rest([ # 资源路由标识 => 请求方式,请求地址,操作方法 'save' => ["POST", "/:id/save", "save"], 'update' => ["PUT", "/:id", "update"], ]); Route::resource('blog', 'Blog');
-
Using resource nested routing allows upper-level resources to operate on lower-level resources.
# app/controller/Comment.php class Comment { public function read($id, $blog_id) { return "评论ID:" . $id . ",博客ID:" . $blog_id; } public function edit($id, $blog_id) { return "评论ID:" . $id . ",博客ID:" . $blog_id; } } # route/app.php 注册资源嵌套路由 Route::resource("blog.comment", "Comment"); # 路由规则 http://localhost:8000/blog/:blog_id/comment/:id
8. Annotation routing
-
Annotation routing is a routing written in annotations and is used when the project is very simple.
-
The routing annotation method is not supported by the system by default, but is an optional solution that requires additional installation of extensions.
composer require topthink/think-annotation
-
When writing routes in the controller, double quotes must be used. Single quotes cannot be parsed
# app/controller/Address.php use think\annotation\Route; class Address { /** * @param $id * @return string * @route("ds/:id", method="GET", https=1); */ public function details($id) { return '详情id:' . $id; } }
-
Annotation mode also supports resource routing and grouping.
use think\annotation\Route\Resource; /** * Class Blog * @package app\controller * @Resource("blog"); */ class Blog { ... }
9. URL generation
-
Use to
buildUrl()
get the URL address of the route. The default suffix is html, which can besuffix()
changed using . Usedomain()
to generate a URL plus a domain name.Route::buildUrl("Url/details", ['id' => 8])->suffix('shtml')->domain(true);
-
You can define an alias for the route and use the alias when generating the URL.
-
You can directly use the routing address to generate the URL.
-
Helper function
url()
can be directly replacedRoute::buildUrl()
url(ds/5)->domain(true);