Home>Article>PHP Framework> Learn all the basic routing knowledge of Laravel framework in one article
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:
Four , Routing grouping
Route::group([], function () { Route::get('hello', function () { return 'Hello'; }); Route::get('world', function () { return 'World'; }); });
5. Routing grouping, middleware (2 methods - generally use the second one)
Route::middleware('auth:api')->group(function () { Route::get('dashboard', function () { return view('dashboard'); }); Route::get('account', function () { return view('account'); }); });
or
Route::group(['middleware' => 'auth:api'], function () { Route::get('dashboard', function () { return view('dashboard'); }); Route::get('account', function () { return view('account'); }); });
6. Routing path prefix
Route::prefix('api')->group(function () { Route::get('/', function () { // 处理 /api 路由 })->name('api.index'); Route::get('users', function () { // 处理 /api/users 路由 })->name('api.users'); });
7. Routing Subdomain name
Route::domain('{account}.blog.test')->group(function (){ Route::get('/', function ($account) { //TODO }); Route::get('user/{id}', function ($account, $id) { //TODO }); });
8. Routing namespace
Route::namespace('Admin')->group(function() { // App\Http\Controllers\Admin\AdminController Route::get('/admin', 'AdminController@index'); });
Routing namespace, prefix, grouping, Parameter and alias combination
// Route naming path prefix
Route::name('user.')->prefix('user')->group(function () { Route::get('{id?}', function ($id = 1) { // 处理 /user/{id} 路由,路由命名为 user.show return route('user.show'); })->name('show'); Route::get('posts', function () { // 处理 /user/posts 路由,路由命名为 user.posts })->name('posts'); });
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:
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!