Laravel 分组路由什么是最好的前缀或中间件
Laravel grouping routes what is best prefix or middleware
当我开始考虑对路线进行分组并查看文档时。我在那里迷路了。前缀、中间件等东西太多了
分组路线的最佳方式是什么?
Route::group(['middleware' => 'admin'], function () {});
Route::group(['prefix' => 'admin'], function () {});
Route::group(['namespace' => 'admin'], function () {})
哪种方法最好?为什么?什么时候使用什么方法?
等等。前缀和中间件是两个不同的东西
prefix
是一种为您的路线添加前缀并避免不必要的输入的方法,例如:
Route::get('post/all','Controller@post');
Route::get('post/user','Controller@post');
这可以使用前缀 post
进行分组
Route::group(['prefix' => 'post'], function(){
Route::get('all','Controller@post');
Route::get('user','Controller@post');
})
另一方面,中间件:
Middleware provide a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.
例如,现在使用上一个示例,我希望用户在我的 post 路由中进行身份验证。我可以像这样向这个组应用中间件:
Route::group(['prefix' => 'post', 'middleware' => ['auth']], function(){
Route::get('all','Controller@post');
Route::get('user','Controller@post');
})
您应该查看文档以获取更多信息。
两者不同但同时使用两者 对路由中间件进行分组并为路由添加前缀的最佳技术避免不必要的输入
Route::group(['prefix' => 'admin','middleware' => ['auth:admin']], function() {
Route::get('dashboard','AdminController@dashboard');
});
当我开始考虑对路线进行分组并查看文档时。我在那里迷路了。前缀、中间件等东西太多了
分组路线的最佳方式是什么?
Route::group(['middleware' => 'admin'], function () {});
Route::group(['prefix' => 'admin'], function () {});
Route::group(['namespace' => 'admin'], function () {})
哪种方法最好?为什么?什么时候使用什么方法?
等等。前缀和中间件是两个不同的东西
prefix
是一种为您的路线添加前缀并避免不必要的输入的方法,例如:
Route::get('post/all','Controller@post');
Route::get('post/user','Controller@post');
这可以使用前缀 post
Route::group(['prefix' => 'post'], function(){
Route::get('all','Controller@post');
Route::get('user','Controller@post');
})
另一方面,中间件:
Middleware provide a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.
例如,现在使用上一个示例,我希望用户在我的 post 路由中进行身份验证。我可以像这样向这个组应用中间件:
Route::group(['prefix' => 'post', 'middleware' => ['auth']], function(){
Route::get('all','Controller@post');
Route::get('user','Controller@post');
})
您应该查看文档以获取更多信息。
两者不同但同时使用两者 对路由中间件进行分组并为路由添加前缀的最佳技术避免不必要的输入
Route::group(['prefix' => 'admin','middleware' => ['auth:admin']], function() {
Route::get('dashboard','AdminController@dashboard');
});