Laravel 分组路由什么是最好的前缀或中间件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47994187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:10:15  来源:igfitidea点击:

Laravel grouping routes what is best prefix or middleware

laravelroutinglaravel-routinglaravel-middleware

提问by Shahid Karimi

When I start thinking grouping my routes and check the documentation. I lost there. There are too many things like prefix, middleware etc.

当我开始考虑将我的路线分组并检查文档时。我在那里迷路了。有太多的东西,比如前缀、中间件等。

What is the best way to group routes?

对路线进行分组的最佳方法是什么?

Route::group(['middleware' => 'admin'], function () {});

Route::group(['prefix' => 'admin'], function () {});

Route::group(['namespace' => 'admin'], function () {})

Which approach is best? And why? When to use what approach?

哪种方法最好?为什么?什么时候使用什么方法?

回答by Luis felipe De jesus Munoz

Wait. Prefix and middleware are two different things

等待。前缀和中间件是两个不同的东西

prefixis a way to Prefix your routes and avoid unnecessary typing e.g:

prefix是一种为路由添加前缀并避免不必要的输入的方法,例如:

Route::get('post/all','Controller@post');
Route::get('post/user','Controller@post');

This can be grouped using prefix post

这可以使用前缀分组 post

Route::group(['prefix' => 'post'], function(){
    Route::get('all','Controller@post');
    Route::get('user','Controller@post');
})

In the other hand, Middleware :

另一方面,中间件:

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.

中间件提供了一种方便的机制来过滤进入应用程序的 HTTP 请求。例如,Laravel 包含一个中间件,用于验证您的应用程序的用户是否已通过身份验证。如果用户未通过身份验证,中间件会将用户重定向到登录屏幕。但是,如果用户通过了身份验证,中间件将允许请求进一步进入应用程序。

For example using last example now i want the users to be authenticated in my post routes. I can apply a middleware to this group like this:

例如,现在使用最后一个示例,我希望用户在我的 post 路由中进行身份验证。我可以像这样将中间件应用于这个组:

Route::group(['prefix' => 'post', 'middleware' => ['auth']], function(){
        Route::get('all','Controller@post');
        Route::get('user','Controller@post');
    })

You should check the docs to get more informed.

您应该查看文档以获取更多信息。

https://laravel.com/docs/5.5/middleware

https://laravel.com/docs/5.5/middleware

https://laravel.com/docs/5.5/routing#route-groups

https://laravel.com/docs/5.5/routing#route-groups

回答by Naeem Ijaz

Both are different But to use both at the same time Best technique for grouping route middleware and prefix your route avoid unnecessary typing

两者都不同但同时使用两者的最佳技术分组路由中间件并为路由添加前缀避免不必要的输入

Route::group(['prefix' => 'admin','middleware' => ['auth:admin']], function() {
    Route::get('dashboard','AdminController@dashboard');
});