SoFunction
Updated on 2025-03-02

Detailed explanation of the basic routing of laravel5.1 framework

This article describes the basic routing of the laravel5.1 framework. Share it for your reference, as follows:

I am studying Laravel5.1. Although I have just started and have a very shallow understanding, I still need to summarize and understand clearly
It is recommended that everyone arrivelaravel collegeLearn laravel

1. Routing (app/)

  • Make routing settings in;
  • As a unified access portal, it is the unified scheduling of the controller;
  • Without a configured route, there is no correct access to the path;
  • Routing needs to set certain rules for your own viewing, use and understand;

2. Basic routing types and usage examples

  • 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

match[]Request method 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 to name display});

That is, except/blog/{name}No routing type can be entered

  • Optional parameters
Route::get('/blog/{name?}',function($name = 'name'){
  return $name; // Return to name display, if not set, take the default value});

That is, the default value is set, and has it been added to the route? If no parameters are entered, the default value will be used.

  • Regular parameters

Regularity can be more flexible and match more needs.

Route::get('/blog/{id?}',function($){
  return "{$id}";//Output the blog ID,})->where('name','^\d+$');//The regular match can only be a number, otherwise the route will not be found;
  • Parameter global constraints

existapp/Providers/RouteServiceProviderofboot(Router $router)The method is modified as follows:

public function boot(Router $router)
{
  $router->pattern('id','^\d+$');
  parent::boot($router);//Restrict id globally to number}

boot()Methods are used in each service provider class. The providers will be executed after the method is started.

Dependency injection can be implemented in Providers through the boot() method

4. The routing can also be done

Give alias or group the route
Anti-CSRF attacks
Restful style routing
Details

X, app/ comment translation (bad exercise)

Since I started to come into contact with laravel and github, it is becoming increasingly difficult to escape my poor English. I should begin to be no longer afraid and face it well, so I began to gradually translate the English annotations that appear in the source code of laravel. 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 routes here.
 | Simply tell laravel that when a specific address is requested, access the corresponding controller to make the address respond.
 |
 */

For more information about Laravel, readers who are interested in this site can view the topic:Laravel framework introduction and advanced tutorial》、《Summary of excellent development framework for php》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope that this article will be helpful to everyone's PHP programming based on the Laravel framework.