thinkphp5 routing experience

Posted by yarin on Sat, 18 May 2019 15:12:10 +0200

The role of routing:
1. Simplify the URL address for everyone to remember.
2. Favorable for search engine optimization, such as can be captured by Baidu's crawler

Optimizing URl
1. Front and rear end separation
Modify the entry file, create a new admin.php file under public, and add the following code into the

1 // Define application directories
2  
3 define('APP_PATH', __DIR__ . '/../application/');
4  
5 // Loading Framework Boot File
6  
7 require __DIR__ . '/../thinkphp/start.php';

2. Binding module
1) Functions of front-end and back-end separation
The entry file index.php can only enter the foreground module
admin.php This entry file can only enter the background module
2) Binding module
Add define('BIND_MODULE','index') to index.php so that only the front-end module can be accessed at http://www.demo.com/index.php/. No background access, http://www.yd.com/index.php/index/index
Add define('BIND_MODULE','admin') to admin.php so that http://www.demo.com/admin.php can only access the background module and can not access the foreground, http://www.yd.com/admin.php/index/index
3) Hide the entry file (how to operate without writing, you can see the description of the hidden entry file under the URL access in the document), so that access to the front-end module can omit index.php, which can be accessed directly by http://www.yd.com/index/index

Close Background Routing
Add this code to admin.php under public think App:: route (false);

// Define application directories
 
define('APP_PATH', __DIR__ . '/../application/');
 
//Binding background
 
define('BIND_MODULE','admin');
 
// Loading Framework Boot File
 
require __DIR__ . '/../thinkphp/start.php';
 
 
 
//Close admin Module routing must be written after loading the framework boot file
 
\think\App::route(false);

 

There are three modes of routing:
1. Ordinary mode: access completely using PASH_INFO, such as http://www.yd.com/index.php/index/index, domain name+module+controller
2. Hybrid mode: Routing can be used or not.
3. Mandatory mode: Routing must be used

Route setting

I. Dynamic Individual Settings

Change in route.php file under application

1 use think\Route; //Introduce Route
2  
3 Route::rule('test','index/index/demo'); //When URL Visit http://At www.yd.com/test, the demo method of the controller under index module is accessed.

Routing form:
Static routing: Route::rule('test','index/index/demo');
Route::rule('getid/:id','index/User/getId') with parameters;
For example, I visit http://www.yd.com/getid/7, or http://www.yd.com/getid/8, or http://www.yd.com/getid/9, which is a parameter after getid.

 1 //First of all index Under module User Write one in the controller getId Method
 2  
 3 public function getId(){
 4  
 5 echo input('id'); //output id
 6  
 7 }
 8  
 9 //Then in route.php Add this line of code
10  
11 Route::rule('getid/:id','index/User/getId');
12  
13 //Finally when we http://Add a number after www.yd.com/geted, such as http://www.yd.com/getid/20, and the page will display 20.

Routing with multiple parameters, such as two parameters

 1 //index Under module User Write one in the controller myTime Method
 2  
 3 public function myTime(){
 4  
 5 echo input('year').' year '.input('month').'month'; //output n year n month
 6  
 7 }
 8  
 9 //Then in route.php Add this line of code
10  
11 Route::rule('time/:year/:month','index/User/myTime');
12  
13 //Finally, when we visit http://www.yd.com/time/2018/9, the page will show September 2018

Selective band parameters, that is, when we access the url, the URL can be followed by parameters, or not, in writing the parameters on the routing file with middle brackets on the line
For example, year or month of output

 1 public function myTime(){
 2  
 3 echo input('year').' year '.input('month').'month'; //output n year n month
 4  
 5 }
 6  
 7 //Then in route.php Add this line of code
 8  
 9 Route::rule('time/:year/[:month]','index/User/myTime'); //A key:month Outside plus[]
10  
11 //Finally, when we visit http://www.yd.com/time/2018/9, the page will show September 2018.
12  
13 //When we visit http://www.yd.com/time/2018, the page will display the month of 2018.

Pure band parameter Of routing is not recommended

 1 //Route writing
 2  
 3 Route::rule(':x/:y','index/User/XAndY');
 4  
 5 //Method
 6  
 7 public function XAndY(){
 8  
 9 echo input('x').' '.input('y');
10  
11 }
12  
13 //Visit http://www.yd.com/5/3, page output 53

Perfectly matched routing with a $symbol at the end of the routing

 1 public function comp(){
 2  
 3 echo 'I am perfectly matched routing';
 4  
 5 }
 6  
 7 //No addition $Symbols, Our Words comp How many paths are added, such as http://www.yd.com/comp/asdfda/asdfasfd/aaa/bbb, all pages can output that I am a fully matched routing
 8  
 9 Route::rule('comp','index/User/comp');
10  
11  
12  
13 //Add $Symbol, we are in comp How many paths are added, such as http://www.yd.com/comp/asdfda/asdfasfd/aaa/bbb, page cannot output method content
14  
15 Route::rule('comp','index/User/comp');

2. Routing in batches
The first way is to register all the above single dynamic registered routes in batches.

 1 Route::rule([
 2  
 3 "test"=>"index/index/demo",
 4  
 5 'getid/:id'=>'index/User/getId',
 6  
 7 'time/:year/[:month]'=>'index/User/myTime',
 8  
 9 ':x/:y'=>'index/User/XAndY',
10  
11 'comp$'=>'index/User/comp'
12  
13 ],'','get');

The second way is to use get as an example.

 1 Route::get([
 2  
 3 "test"=>"index/index/demo",
 4  
 5 'getid/:id'=>'index/User/getId',
 6  
 7 'time/:year/[:month]'=>'index/User/myTime',
 8  
 9 ':x/:y'=>'index/User/XAndY',
10  
11 'comp$'=>'index/User/comp'
12  
13 ]);

3. Configuration file setting routing, batch registration with configuration file, or writing in route.php file

 1 return[
 2  
 3 "test"=>"index/index/demo",
 4  
 5 'getid/:id'=>'index/User/getId',
 6  
 7 'time/:year/[:month]'=>'index/User/myTime',
 8  
 9 ':x/:y'=>'index/User/XAndY',
10  
11 'comp$'=>'index/User/comp'
12  
13 ];

Routing Request Patterns
There are four request modes in TP, GET, POST, PUT and DELETE. If we don't specify the request type, the default is *, all request types.

There are two ways to write requests. Here's an example of get

1 Route::rule('qtype','index/User/questType','get');
2  
3 Route::get('gtype','index/User/questType');

Writing that supports both get and post

Route::rule('type','index/User/questType','get|post');

Two Writings Supported by All Request Forms

Route::any('type','index/User/questType');
 
Route::rule('type','index/User/questType','*');

The last parameter of the variable rule, Route::rule(); is an array that can specify multiple parameters and write them in regular expressions to specify what data type the parameter must be, or what data it must be, for example.

Route::rule('getid/:id','index/User/getId','get',[],['id'=>'\d']);  //The last parameter, the representation id Passing parameters must be numbers

The penultimate parameter of the routing parameter, Route::rule(); is an array that can be used to specify the data type of the request or to specify the URL suffix of the request, such as

Route::rule('getid/:id','index/User/getId','get',['method'=>'get','ext'=>'html'],['id'=>'\d']);
 
//The mode of request must be get,The suffix of the request must be html,Visiting url by http://www.yd.com/getid/9.html, request fails without HTML suffix

Resource routing, your background module may have add, delete, modify and check operations, but one by one writing is too hard, resource routing automatically help you generate these routes, you only need to write these methods in the controller,

such as

 1 //First create block
 2  
 3 namespace app\index\controller;
 4  
 5 class Block
 6  
 7 {
 8  
 9 public function index(){
10  
11 echo 'I'm under the front desk module. block';
12  
13 }
14  
15 public function create(){
16  
17 echo 'I'm under the front desk module. block Of create Method';
18  
19 }
20  
21 public function read($id){
22  
23 echo $id;
24  
25 }
26  
27 }
28  
29 //Then in route.php Write down resource routing
30  
31 Route::resource('block','index/Block');
32  
33  
34  
35 //Effect:
36  
37 //When you visit http://www.yd.com/block URL, you access the index method
38  
39 //When you visit http://www.yd.com/block/15 URL, you access the read method
40  
41 //When you visit http://www.yd.com/block/create URL, you access the Create method

Shortcut routing
Create a Fastroute controller under the index module. Write down the following examples. In addition to index, all other methods need to add get.

 1 namespace app\index\controller;
 2  
 3 class Fastroute
 4  
 5 {
 6  
 7 public function index(){
 8  
 9 echo 'I am Fast Routing index';
10  
11 }
12  
13 public function getAA(){
14  
15 echo "I am getAA";
16  
17 }
18  
19 public function getBB(){
20  
21 echo "I am BB";
22  
23 }
24  
25 public function postInfo()
26  
27 {
28  
29 }
30  
31  
32  
33 public function putInfo()
34  
35 {
36  
37 }
38  
39  
40  
41 public function deleteInfo()
42  
43 {
44  
45 }
46  
47 }

Write down shortcut routing in route.php

1 //Note: The routing name should be the same as the controller name.
2  
3 Route::controller('Fastroute','index/Fastroute');
4  
5 //Then we want to visit getAA Method, we can access URL http://Visit www.yd.com/Fastroute/AA
6  
7 //To access getBB(), you can access it at http://www.yd.com/Fastroute/BB.
8 1

There are two ways to generate URL s. I don't know what's the use of them.

1 Url::build('index/User/index');
2 Url::build();
3 
4  
5 Url::root('/index.php'); //File with entry
6  
7 dump(Url('index/User/index'));
8  
9 dump(Url::build('index/User/index'));

Topics: PHP