I wanted to continue with the examples directly, but I was afraid that the article would be too long, so I would like to talk about some very basic key points first. I am learning Laravel5.1. Although I have just started and my understanding is very shallow, I still need to summarize and have a clear understanding. It is recommended that everyone go to laravel academy to learn laravel
1. Routing (app/routes.php)
Configure routing settings in routes.php;
As a unified entrance for access, it is the unified scheduling of the controller;
If routing is not configured, there is no correct access path;
Routing needs to set certain rules by yourself to facilitate your own viewing, use and understanding;
2. Basic types of routing and usage examples
get Route::get('articles','ArticleController@index'); or
- Route::get('db',function(){
- $name = DB:: connection()->getDatabaseName();
- echo $name;
- });
Copy code
post Route::post('article/update','ArticleController@update');
match match the request method in []
- Route::match(['get','post'],'/hello',function(){
- return "match";
- });
Copy code
any Match all request methods
- Route::any('/hello',function(){
- return "any";
- });
Copy code
3. Get parameters from route
Required parameters
- Route::get('/blog/{name}',function($name){
- return $name; // Return name to display
- });
Copy The code
means that except for the routing type of /blog/{name}, no one can come in
Optional parameters
- Route::get('/blog/{name?}',function($name = 'name'){
- return $name; // Return name display, if If not set, take the default value
- });
Copy the code
The default value is set and added to the route? If no parameters are entered, the default value will be used
Regular parameters Regular can be more flexible and match more needs.
- Route::get('/blog/{id?}',function($id="1"){
- return "{$id}";//Output the ID of the blog,
- })- >where('name','^d+$');//Regular matching can only be numbers, otherwise the route will not be found;
Copy code
Parameter global constraints in app/ The boot(Router $router) method of Providers/RouteServiceProvider is modified as follows:
- public function boot(Router $router)
- {
- $router->pattern('id','^d+$');
- parent::boot($router);//Limit the id globally to numbers
- }
Copy code
boot() method is used in each service provider (Providers) class and will be started by Providers Executed after the method is executed You can implement dependency injection on Providers through the boot() method
4. Routing can still be done
Give the route an alias or group it
Anti-CSRF attacks
Restful style routing
Details
X. app/routes.php comment translation (poor exercise)
Since I started to come into contact with laravel and github, I found it increasingly difficult to escape my poor English. It was time for me to stop being afraid and face it properly, so I started to gradually translate the English comments that appeared in the laravel source code until I became familiar with the framework. Sometimes, add your own Chinese comments to strengthen your 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 routes here.
- | Simply tell Laravel, when a specific address is requested, access the corresponding controller so that the address can be responded to.
- |
- */
Copy code
|