Routing Explanation of laravel 5.1 Framework Foundation
- 2021-12-21 04:27:23
- OfStack
This article illustrates the routing of the foundation of laravel 5.1 framework. Share it for your reference, as follows:
I am studying Laravel 5.1. Although I have just started, my understanding is very shallow, but I still need to do induction and clear understanding
It is recommended that you study laravel at laravel College
1. Routing (app/routes. php)
Route setting in routes. php; As the access entrance of Uniform 1, it is the Uniform 1 scheduling of the controller; If the route is not configured, the path is not accessed correctly; Routing needs to stipulate its own rules, which is convenient for itself to view, use and understand;2. Basic types of routing and examples of use
get
Route::get('articles','ArticleController@index');
Or
Route::get('db',function(){
$name = DB::connection()->getDatabaseName();
echo $name;
});
post
Route::post('article/update','ArticleController@update');
match
Matching
[]
Request mode in
Route::match(['get','post'],'/hello',function(){
return "match";
});
any
Match all request methods
Route::any('/hello',function(){
return "any";
});
3. Get parameters from the route
Required parameters
Route::get('/blog/{name}',function($name){
return $name; // Return name Display
});
That is, no route type can come in except/blog/{name}
Optional parameters
Route::get('/blog/{name?}',function($name = 'name'){
return $name; // Return name Display , If it is not set, take the default value
});
That is, the default value is set, and the route is added? Default values are used if no parameters are entered
Regular parameterRegularity can be more flexible and match more requirements.
Route::get('/blog/{id?}',function($id="1"){
return "{$id}";// Output blog Adj. ID ,
})->where('name','^\d+$');// Regular matching can only be numbers, otherwise the route will not be found;
Parameter global constraint
In app/Providers/RouteServiceProvider
boot(Router $router)
The method is modified as follows:
public function boot(Router $router)
{
$router->pattern('id','^\d+$');
parent::boot($router);// Will id Global limit to numbers
}
boot()
Method is useful in every service provider (Providers) class and will be executed after Providers starts the method execution
You can implement dependency injection on Providers through the boot () method
4. Routing can also be done
Give routes an individual name or group
Anti-CSRF attack
Restful style routing
Details
X, app/routes. php annotation translation (poor exercise)
Since I started to contact laravel and github, it is more and more difficult to escape my poor English. I should stop being afraid and face it well, so I began to translate the English annotations appearing in laravel source code step by step. When I am familiar with the framework, I will add my own Chinese annotations to strengthen my understanding.
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
/*
|--------------------------------------------------------------------------
| Application routing
|--------------------------------------------------------------------------
|
| You can easily register all the routes here.
| Simply tell laravel, When a specific address is requested, the corresponding controller is accessed to make the address respond.
|
*/
More readers who are interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"
I hope this article is helpful to the PHP programming based on Laravel framework.