在 Laravel 5.2 中更改登录/注册 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35298174/
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 13:12:30  来源:igfitidea点击:

Change the login/register URL in Laravel 5.2

phplaravel

提问by dwilson390

I'm currently writing an application which only has accounts for staff of the company, not regular website visitors. As such, I would like to keep my URLs which relate to the 'admin' area of the site, under the /admin URL which means changing /loginto /admin/loginand /registerto /admin/register.

我目前正在编写一个应用程序,它只有公司员工的帐户,而不是普通的网站访问者。因此,我想保持我的网址,其涉及到网站的“管理”区域,这意味着要改变/管理URL下/login/admin/login/register/admin/register

However, I'm at a loss as how to change the login and register URLs in Laravel 5.2, it seems that in previous versions it was a simple matter of adding protected $loginPath = '/admin/login';to Auth\AuthControllerHowever, this makes no difference when adding it to my AuthController.

不过,我不知所措,如何更改登录和Laravel 5.2注册的网址,似乎在以前的版本中,它是将一件简单的事情protected $loginPath = '/admin/login';Auth\AuthController。然而,这并没有区别将它添加到我的时候AuthController

The following is the output of php artisan route:list;

以下是输出php artisan route:list

+--------+----------+-------------------------+------+-----------------------------------------------------------
| Domain | Method   | URI                     | Name | Action
+--------+----------+-------------------------+------+-----------------------------------------------------------
|        | GET|HEAD | /                       |      | App\Http\Controllers\PageController@index
|        | GET|HEAD | admin                   |      | App\Http\Controllers\AdminPageController@index
|        | GET|HEAD | admin/profile           |      | App\Http\Controllers\AdminPageController@profile
|        | GET|HEAD | login                   |      | App\Http\Controllers\Auth\AuthController@showLoginForm
|        | POST     | login                   |      | App\Http\Controllers\Auth\AuthController@login
|        | GET|HEAD | logout                  |      | App\Http\Controllers\Auth\AuthController@logout
|        | POST     | password/email          |      | App\Http\Controllers\Auth\PasswordController@sendResetLink
|        | POST     | password/reset          |      | App\Http\Controllers\Auth\PasswordController@reset
|        | GET|HEAD | password/reset/{token?} |      | App\Http\Controllers\Auth\PasswordController@showResetForm
|        | GET|HEAD | register                |      | App\Http\Controllers\Auth\AuthController@showRegistrationF
|        | POST     | register                |      | App\Http\Controllers\Auth\AuthController@register
+--------+----------+-------------------------+------+-----------------------------------------------------------

回答by Jilson Thomas

In L5.2, all authentication routes are grouped in a route called Route::auth().

在 L5.2 中,所有身份验证路由都分组在名为 的路由中Route::auth()

If you search for the symbol auth(), you can see the auth() function which contains all the route links.

如果您搜索符号auth(),您可以看到包含所有路由链接的 auth() 函数。

Check this file: Illuminate\Routing\Routerfor the auth()function.

检查这个文件: Illuminate\Routing\Router对于auth()函数。

But I'm not sure if it's a good practice to change it here.

但我不确定在这里更改它是否是一个好习惯。

Edit:

编辑:

So if you need to change the default auth routes, include all routes in your routes.phpfile and change the uri as you want it.

因此,如果您需要更改默认身份验证路由,请在routes.php文件中包含所有路由并根据需要更改 uri。

To get a reference:

要获得参考:

    // Authentication Routes...
    Route::get('login', 'Auth\AuthController@showLoginForm');
    Route::post('login', 'Auth\AuthController@login');
    Route::get('logout', 'Auth\AuthController@logout');

    // Registration Routes...
    Route::get('register', 'Auth\AuthController@showRegistrationForm');
    Route::post('register', 'Auth\AuthController@register');

    // Password Reset Routes...
    Route::get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
    Route::post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
    Route::post('password/reset', 'Auth\PasswordController@reset');

回答by indyo

You can call Route::auth()inside a prefixed group.

您可以Route::auth()在带前缀的组内呼叫。

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

回答by Khaled Rahman

It's bad practice to change anything in the vendor folder, especially because they will be overwritten any time you run composer update.

更改供应商文件夹中的任何内容都是不好的做法,特别是因为在您运行 composer update 时它们将被覆盖。

So Change Auth::routes();on routes/web.phpto

所以更改Auth::routes();routes/web.php

    // Authentication Routes...
    $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
    $this->post('login', 'Auth\LoginController@login');
    $this->post('logout', 'Auth\LoginController@logout')->name('logout');

    // Registration Routes...
    $this->get('registers', 'Auth\RegisterController@showRegistrationForm');
    $this->post('registers', 'Auth\RegisterController@register');

    // Password Reset Routes...
    $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
    $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
    $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
    $this->post('password/reset', 'Auth\ResetPasswordController@reset');
    // Authentication Routes...
    $this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
    $this->post('login', 'Auth\LoginController@login');
    $this->post('logout', 'Auth\LoginController@logout')->name('logout');

    // Registration Routes...
    $this->get('registers', 'Auth\RegisterController@showRegistrationForm');
    $this->post('registers', 'Auth\RegisterController@register');

    // Password Reset Routes...
    $this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
    $this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
    $this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
    $this->post('password/reset', 'Auth\ResetPasswordController@reset');

回答by san

Use named routes instead of using Route::auth();

使用命名路由而不是使用 Route::auth();

Route::get('/admin/login', 'Auth\AuthController@showLoginForm');
Route::post('/admin/login', 'Auth\AuthController@login');
Route::get('/admin/logout', 'Auth\AuthController@logout');