Home > PHP Framework > Laravel > body text

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

Mini
Release: 2020-05-21 16:12:28
Original
124 people have browsed it

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);
Copy after login

Commonly used Routing method

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


Or directly request the controller

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

Any request Mode match

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

Whitelist array match match combination

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

Redirect route

Route::redirect('/here',  '/404',  404);
Copy after login

2. Parameter routing

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


Regular parameter routing

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


3. Routing alias (accessed with user.profile)

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

// Access method:

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


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;;     
    });
});
Copy after login

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;);    
    });
});
Copy after login

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;);     
    });
});
Copy after login

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;);
});
Copy after login

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    
    });
});
Copy after login

8. Routing namespace

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

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;);
});
Copy after login


9. Access the current route

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

10. Clear the routing cache

php artisan route:cache
Copy after login

11. Delete routing cache

php artisan route:clear
Copy after login


##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!

Related labels:
1
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!