Home  >  Article  >  Backend Development  >  【laravel-0.0.2】Basic 1-Routing

【laravel-0.0.2】Basic 1-Routing

WBOY
WBOYOriginal
2016-07-23 08:54:431030browse

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

  1. Route::get('db',function(){
  2. $name = DB:: connection()->getDatabaseName();
  3. echo $name;
  4. });
Copy code

post
Route::post('article/update','ArticleController@update');

match
match the request method in []

  1. Route::match(['get','post'],'/hello',function(){
  2. return "match";
  3. });
Copy code

any
Match all request methods

  1. Route::any('/hello',function(){
  2. return "any";
  3. });
Copy code
3. Get parameters from route

Required parameters

  1. Route::get('/blog/{name}',function($name){
  2. return $name; // Return name to display
  3. });
Copy The code

means that except for the routing type of /blog/{name}, no one can come in

Optional parameters

  1. Route::get('/blog/{name?}',function($name = 'name'){
  2. return $name; // Return name display, if If not set, take the default value
  3. });
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.

  1. Route::get('/blog/{id?}',function($id="1"){
  2. return "{$id}";//Output the ID of the blog,
  3. })- >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:

  1. public function boot(Router $router)
  2. {
  3. $router->pattern('id','^d+$');
  4. parent::boot($router);//Limit the id globally to numbers
  5. }
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.

  1. /*
  2. |----------------------------------------- ----------------------------------
  3. |Application Routes
  4. |---------- -------------------------------------------------- -------------
  5. |
  6. | Here is where you can register all of the routes for an application.
  7. | It's a breeze. Simply tell Laravel the URIs it should respond to
  8. | and give it the controller to call when that URI is requested.
  9. |
  10. */
  11. /*
  12. |-------------------------------- ---------------------------------------------
  13. | Application routing
  14. |------------------------------------------------- ------------------------
  15. |
  16. | You can easily register all routes here.
  17. | Simply tell Laravel, when a specific address is requested, access the corresponding controller so that the address can be responded to.
  18. |
  19. */
Copy code
laravel


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn