简体   繁体   English

Laravel重定向登录,主URL

[英]Laravel redirect login, main URL

I have the Laravel auth system configured and it works. 我已经配置了Laravel身份验证系统,并且可以正常工作。 When a user logs in, he is redirected to his dashboard. 用户登录后,将被重定向到他的仪表板。 But when it closes the page or revises the main URL www.xyz.com, it is not redirected to the dashboard. 但是,当它关闭页面或修改主URL www.xyz.com时,它不会重定向到仪表板。 How can I redirect the user to his dashboard when he is logged in and he visit the main URL? 当用户登录并访问主URL时,如何将用户重定向到其仪表板?

LoginController.php LoginController.php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/iboard';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => ['logout', 'userLogout']]);
    }

    /**
    * Get the needed authorization credentials from the request.
    *
    * @param  \Illuminate\Http\Request  $request
    * @return array
    */
   protected function credentials(Request $request)
   {
       $field = filter_var($request->get($this->username()), FILTER_VALIDATE_EMAIL)
           ? $this->username()
           : 'username';

       return [
           $field => $request->get($this->username()),
           'password' => $request->password,
       ];
   }

    public function userLogout()
    {
        Auth::guard('web')->logout();
        return redirect('/logout');
    }
}

Heres a simple solution, when a user visits the page www.xyz.com you can check if the user is logged in by using 这是一个简单的解决方案,当用户访问www.xyz.com页面时,您可以使用以下方法检查用户是否已登录

use Illuminate\Support\Facades\Auth;
if (Auth::check()) {
 //Executes when user is logged in
  return redirect('dashboard');
}

The above code checks if a user is logged in if he is then he gets redirected else he won't get redirected and still be on www.xyz.com. 上面的代码检查用户是否已登录,如果他已登录,则他将被重定向,否则他将不会被重定向,并且仍然在www.xyz.com上。

Hopefully, this answer helps. 希望这个答案会有所帮助。

Here are some cool some cool stuff you can do with laravel auth:- https://laravel.com/docs/5.5/authentication 这里有一些很酷的一些很酷的东西,你可以用laravel权威性做到: - https://laravel.com/docs/5.5/authentication

simply redirect the user when he access's the root for example you can add this to your web.php (or routes.php if version <= 5.2) 只需在用户访问根目录时重定向用户即可,例如,您可以将其添加到web.php(如果版本<= 5.2,则可以将其添加到route.php)

Route::get('/', function () {
 return redirect('dashboard');
});

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

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