php - Laravel redirect()->intended() 在自定义登录 Controller 中不起作用

标签 php laravel laravel-5 laravel-4

我有一个自定义登录 Controller 及其使用 return redirect()->intended(route('home')) ,根据文档,这应该将用户重定向到他们尝试访问的 URL,然后再被身份验证中间件拦截。

但就我而言,每次重定向到 home route .我确信我做对了,或者至少我认为我做对了。谁能告诉我我在哪里做错了??

我的 logincontroller是这样的:

public function __construct()
    {
        $this->middleware('guest');
    }

 public function login(Request $request)
    {
        $validatedData = $request->validate([
            'email' => 'required|email|max:255',
            'password' => 'required|max:255',
        ]);
        try {
            $response = HelperFunctions::fetchData('post', 'login/login', [
                'loginId' => $request->get('email'),
                'password' => md5($request->get('password'))
            ]);
            if ($response['code'] == 200 && $response['success']) {
                session([
                    'api_token' => $response['sessionId'],
                    'user_data' => $response['data']['profile']
                ]);

                return redirect()->intended(route('home'));
            } else {
                return redirect()->back()->with('error', 'Please provide valid credentials');
            }
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'Opps!! Something is wrong. We are trying to fix it');
        }

我的认证检查中间件
class checkAuthentication
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //check if user is logged in
        if(Session::has('api_token') && Session::has('user_data')) {
            return $next($request);
        }

        return redirect(route('login'));

    }
}

我也试过dd() Session::get('url.intended')但它返回空。

我确实尝试在 google、laracast 和 stackoverflow 上寻找引用资料,但我找到了一些引用资料,但这些对我没有帮助。任何人都可以请帮助我谢谢。

我检查过并尝试过的一些引用资料:
  • https://laracasts.com/discuss/channels/laravel/redirect-intended-not-working-after-login?page=1
  • Laravel 5 - redirect()->intended() after authentication not going to intended
  • https://laravel.io/forum/11-24-2014-problems-with-redirectintended-and-auth
  • 最佳答案

    最简单的方法是 redirect()->guest()在您的 Auth中间件而不是您当前拥有的重定向:

    return redirect()->guest(route('login'));
    

    使用 guest()重定向器上的方法添加了 url.intended session 的值(value)。

    或者,如果中间件未通过身份验证,您可以抛出 AuthenticationException,而不是从中间件返回。 :
    public function handle($request, Closure $next)
    {
        //check if user is logged in
        if(Session::has('api_token') && Session::has('user_data')) {
            return $next($request);
        }
    
        throw new AuthenticationException('Unauthenticated.');
    }
    

    这将允许您利用 Laravel 的 ExceptionHandler如果你需要。您还需要添加 use Illuminate\Auth\AuthenticationException;到你类(class)的顶端。

    关于php - Laravel redirect()->intended() 在自定义登录 Controller 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51882925/

    相关文章:

    javascript - ajax将信息发布到php $_POST中

    PHP 获取没有扩展名的当前页面名称?

    laravel - 使用带有 vagrant + homestead 的 virtualbox 在虚拟机上缓慢文件上传

    mysql - 我如何计算 laravel 表中的寄存器?

    php - 如何使用mysql获取数组列内特定索引的值?

    css - 我可以在 css 样式中使用 Laravel 代码吗?

    php - 如何从 mysql 数据库中获取数据?

    PHP 正则表达式查找并用 1 个管道替换 1 个或多个管道的字符串

    php - 如何从公共(public)文件夹 Laravel 获取文件列表

    css - 在 <form action> 中使用 CSS