简体   繁体   English

如何在 Laravel 7 中正确定义我的身份验证路由?

[英]How to properly define my authenticate routes in Laravel 7?

I am new to laravel and i am trying to define my routes.And the logic is "/" routing to "/login" if the user is not authenticated and "/login" routing to login view blade.Down below are the routes.我是 laravel 的新手,我正在尝试定义我的路由。如果用户未通过身份验证,逻辑是“/”路由到“/login”,而“/login”路由到登录视图刀片。下面是路由。

Auth::routes();

Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
});

Route::get('login', function (){

    return view('auth/login');

});

Route::get('register', function (){

    if(Auth::check()){

        return view('home');

    }else{

        return view('auth/register');
    }

});

Route::get('/', function () {

    if(Auth::check()){

        return view('home');

    }else{

        return redirect('/login');

    }
});




Route::get('/home', 'HomeController@index')->name('home');

But i am keep getting this error "Route [login] not defined."但我不断收到此错误“未定义路由 [登录]”。 and i can't see the problem.我看不到问题所在。

If I add Auth::routes() in a fresh laravel 7 installation shows this on artisan route:list :如果我在新的 laravel 7 安装中添加Auth::routes() ,则会在artisan route:list上显示:

+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
| Domain | Method   | URI                    | Name             | Action                                                                 | Middleware   |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+
|        | GET|HEAD | /                      |                  | Closure                                                                | web          |
|        | GET|HEAD | api/user               |                  | Closure                                                                | api,auth:api |
|        | GET|HEAD | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest    |
|        | POST     | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest    |
|        | POST     | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web          |
|        | GET|HEAD | password/confirm       | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    | web,auth     |
|        | POST     | password/confirm       |                  | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            | web,auth     |
|        | POST     | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web          |
|        | GET|HEAD | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web          |
|        | POST     | password/reset         | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web          |
|        | GET|HEAD | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web          |
|        | GET|HEAD | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest    |
|        | POST     | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest    |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+--------------+

You should then avoid defining the same routes again with a different implementation.然后,您应该避免使用不同的实现再次定义相同的路由。 Let Laravel doing the work for you.让 Laravel 为您完成工作。
As you can see, there are also already middlewares applied to these predefined routes.如您所见,这些预定义路由也已经应用了中间件。 web in this scenario is not a middleware itself but a middleware group defined at app/Http/Kernel.php .此场景中的web本身不是中间件,而是在app/Http/Kernel.php中定义的中间件组。 Please see below for more info about middleware groups.有关中间件组的更多信息,请参见下文。

If you want to kind of "remove" the / -route from your project you can use a redirect route.如果您想从项目中“删除” / -route,您可以使用重定向路由。
https://laravel.com/docs/7.x/routing#redirect-routes https://laravel.com/docs/7.x/routing#redirect-routes

So your route file should, after all, look like this:所以你的路由文件应该是这样的:

Auth::routes();

Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
});

// this will let laravel automatically redirect again if already logged in
Route::permanentRedirect('/', '/login');

Route::get('/home', 'HomeController@index')->name('home');

The automation which will redirect you towards the /home -route is an interaction between app/Http/Middleware/RedirectIfAuthenticated.php and app/Providers/RouteServiceProvider.php .将您重定向到/home路由的自动化是app/Http/Middleware/RedirectIfAuthenticated.phpapp/Providers/RouteServiceProvider.php之间的交互。

Within the latter, you have an attribute defining your HOME在后者中,您有一个定义您的HOME的属性

    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/home';

If you have later more routes which should apply the auth() -middleware you have three ways to do that (or probably 4):如果您以后有更多应该应用auth() -middleware 的路由,您有三种方法可以做到这一点(或者可能是 4 种):

Please compare the following examples to this link:请将以下示例与此链接进行比较:
https://laravel.com/docs/7.x/middleware#middleware-groups https://laravel.com/docs/7.x/middleware#middleware-groups

As a general thing maybe a bit hard to see in the beginning, there is no difference between a controller route and those which use a closure function.一般来说,一开始可能有点难以看到,controller 路由和使用闭包 function 的路由之间没有区别。 The methods which can be used are the same for both implementations.两种实现可以使用的方法相同。 Only the handling of the route within the application will be different.只有在应用程序中对路由的处理会有所不同。

For a single route which is needing a middleware, just add it to the end.对于需要中间件的单个路由,只需将其添加到末尾即可。 This is applicable for controller routes as well as for closure routes.这适用于 controller 路线以及封闭路线。

Route::get('/', function () {
    //
})->middleware('web');
// ^^^^ is as possible as:
Route::get('/', 'Controller@Method')->middleware('web');

For multiple routes using the same middleware use the middleware method.对于使用相同中间件的多个路由,请使用中间件方法。 It is possible to use multiple middlewares assigned as parts of the array.可以使用分配为数组的一部分的多个中间件。

Route::middleware(['web', 'subscribed'])->group(function () {
    //
});

If you still need more customization use the group() -method directly.如果您仍需要更多自定义,请直接使用group() -方法。 Here you can define not only middleware but also for example a prefix for all enclosed routes在这里,您不仅可以定义中间件,还可以定义所有封闭路由的前缀

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

As the last option, you can add any middleware you want to either an existing middleware group or add your own middleware group within app/Http/Kernel.php?作为最后一个选项,您可以将任何您想要的中间件添加到现有的中间件组中,或者在app/Http/Kernel.php? . . But this will cause pretty strong integration hard to build an exception for if you want to go off track because everything within a middleware group is used all the time for every route this group is applied to.但这将导致非常强大的集成很难构建一个例外,如果你想 go 偏离轨道,因为中间件组中的所有内容一直用于该组所应用的每个路由。

Please do not mix Route groups with middleware groups as these are different things.请不要将路由组与中间件组混用,因为它们是不同的东西。

All of these examples were found in the docs for the 7th version, so they should be applicable also for your case.所有这些示例都可以在第 7 版的文档中找到,因此它们也应该适用于您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM