![](/img/trans.png)
[英]Is it possible to use Auth::route() as authenticator for login other than default laravel login page?
[英]Laravel - Middleware How to change route to our desired page other than Login
我正在從事新的 Laravel 項目。
我已經通過 composer 安裝了 laravel 框架,然后我創建了一個用於測試目的的路由,如下所示:
Route::get('/', function () {
return view('pages.home');
});
這很好用,我得到了想要的頁面。 現在為了理解中間件,我添加了這行代碼:
Route::get('/', function () {
return view('pages.home');
})->middleware('auth');
現在它的拋出錯誤是
Route [login] not defined.
據我所知,它拋出這個錯誤是因為我沒有安裝任何 voyagers 包,這就是為什么它沒有找到'login'
路線。
但我的問題是如何將該Route [login]
更改為我想要的頁面,例如Route [pages.notauth]
。
請幫我解決這個問題。
首先運行php artisan make:auth
來制作 Laravel auth 樣板。 然后在LoginController
添加以下內容:
class LoginController extends Controller {
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function showLoginForm() {
return view("pages.notlogin");
}
}
您收到消息Route [login] not defined.
不是因為您沒有安裝 Voyager 軟件包,而是因為您沒有為一般身份驗證創建或定義任何路由。
是的,如果您安裝 Voyager 包,那么錯誤消息將消失,因為包本身將創建必要的身份驗證路由和控制器。
為此,您必須在命令行中運行php artisan make:auth
。
在 laravel 5.4 中,所有的中間件都已經注冊到
app\Http\Kernel.php
在文件中,你會看到
受保護的 $routeMiddleware = [ 'auth' => \\Illuminate\\Auth\\Middleware\\Authenticate::class,
所有與身份驗證相關的任務都由\\Illuminate\\Auth\\Middleware\\Authenticate
類處理。 所以,如果你想改變 auth 中間件的基本行為,那么你必須擴展這個類。
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as BaseAuthenticator;
class Authenticate extends BaseAuthenticator
{
protected function authenticate(array $guards)
{
// TO DO: do your desired change
}
}
嘗試添加
Auth::routes();
在您的路由文件中,為了消除該錯誤, Auth::routes()
只是一個幫助類,可幫助您生成用戶身份驗證所需的所有路由。
如果您想更改網址,請添加此內容並根據需要進行更改:
// 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('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', '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');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.