Home  >  Article  >  PHP Framework  >  Learn all the basic routing knowledge of Laravel framework in one article

Learn all the basic routing knowledge of Laravel framework in one article

Mini
MiniOriginal
2020-05-21 15:51:21107browse

This section mainly explains the detailed routing of the Laravel framework, mainly explaining the definition of routing, parameters, rules, access controllers and other operations.

1. Commonly used routing actions

Syntax

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

Commonly used Routing method

Route::post('/', function () {    
    return view('welcome');
});
Route::get('/', function () {    
    return view('welcome');
});
Route::delete('/', function () {    
    return view('welcome');
});


Or directly request the controller

Route::post('/',  'WelcomeController@index');
Route::get('/',  'WelcomeController@index');
Route::delete('/',  'WelcomeController@index');

Any request Mode match

Route::any(['get', 'post'], '/', function () {
    return view('welcome');
});
Route::any(['get', 'post'], '/', 'WelcomeController@index');

Whitelist array match match combination

Route::match(['get', 'post'], '/', function () {
    return view('welcome');
});
Route::match(['get', 'post'],'/', 'WelcomeController@index');

Redirect route

Route::redirect('/here',  '/404',  404);

2. Parameter routing

Route::get('user/{id?}', function ($id = 1) {    
    return "用户ID: " . $id;
});


Regular parameter routing

Route::get('page/{id}', function ($id) {    
    return '页面ID: ' . $id;
})->where('id', '[0-9]+');
Route::get('page/{name}', function ($name) {    
    return '页面名称: ' . $name;
})->where('name', '[A-Za-z]+');
Route::get('page/{id}/{slug}', function ($id, $slug) {    
    return $id . ':' . $slug;
})->where(['id' => '[0-9]+', 'slug' => '[A-Za-z]+']);


3. Routing alias (accessed with user.profile)

Route::get('user/{id?}', function ($id = 1) {    
    return "用户ID: " . $id;
})->name('user.profile');

// Access method:

<a href="{{ route(&#39;user.profile&#39;, [&#39;id&#39; => 100]) }}">


Four , Routing grouping

Route::group([], function () {     
    Route::get(&#39;hello&#39;, function () {         
        return &#39;Hello&#39;;     
    });     
    Route::get(&#39;world&#39;, function () {         
        return &#39;World&#39;;     
    });
});

5. Routing grouping, middleware (2 methods - generally use the second one)

Route::middleware(&#39;auth:api&#39;)->group(function () {    
    Route::get(&#39;dashboard&#39;, function () {        
        return view(&#39;dashboard&#39;);    
    });    
    Route::get(&#39;account&#39;, function () {        
        return view(&#39;account&#39;);    
    });
});

or

Route::group([&#39;middleware&#39; => &#39;auth:api&#39;], function () {     
    Route::get(&#39;dashboard&#39;, function () {         
        return view(&#39;dashboard&#39;);     
    });     
    Route::get(&#39;account&#39;, function () {         
        return view(&#39;account&#39;);     
    });
});

6. Routing path prefix

Route::prefix(&#39;api&#39;)->group(function () {    
    Route::get(&#39;/&#39;, function () {        
    // 处理 /api 路由    
    })->name(&#39;api.index&#39;);   
     
    Route::get(&#39;users&#39;, function () {        
    // 处理 /api/users 路由    
    })->name(&#39;api.users&#39;);
});

7. Routing Subdomain name

Route::domain(&#39;{account}.blog.test&#39;)->group(function (){    
    Route::get(&#39;/&#39;, function ($account) {        
        //TODO    
    });    

    Route::get(&#39;user/{id}&#39;, function ($account, $id) {        
        //TODO    
    });
});

8. Routing namespace

Route::namespace(&#39;Admin&#39;)->group(function() {     
    // App\Http\Controllers\Admin\AdminController     
    Route::get(&#39;/admin&#39;, &#39;AdminController@index&#39;);
});

Routing namespace, prefix, grouping, Parameter and alias combination

// Route naming path prefix

Route::name(&#39;user.&#39;)->prefix(&#39;user&#39;)->group(function () {    
    Route::get(&#39;{id?}&#39;, function ($id = 1) {        
        // 处理 /user/{id} 路由,路由命名为 user.show        
        return route(&#39;user.show&#39;);    
    })->name(&#39;show&#39;);  
      
    Route::get(&#39;posts&#39;, function () {        
        // 处理 /user/posts 路由,路由命名为 user.posts    
    })->name(&#39;posts&#39;);
});


9. Access the current route

$route  = Route::current();
$name   = Route::currentRouteName();
$action = Route::currentRouteAction();

10. Clear the routing cache

php artisan route:cache

11. Delete routing cache

php artisan route:clear


##Summary:

Through the above learning summary, we have learned Laravel’s routing definition and access methods, and routing is very convenient, for novices It is said that it is very convenient to learn and very easy to learn.

The above is the detailed content of Learn all the basic routing knowledge of Laravel framework in one article. For more information, please follow other related articles on the PHP Chinese website!

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