简体   繁体   English

Laravel目标URL始终重定向到索引

[英]Laravel Intended URL always redirect to index

I am facing a weird (at least for me) problem working with Laravel 5.3. 我在使用Laravel 5.3时遇到一个奇怪的问题(至少对我来说)。 I try to get the previous link (before user logged in) via session and redirect them back to this link after they succesfuly logged in. 我尝试通过会话获取先前的链接(在用户登录之前),并在他们成功登录后将它们重定向回该链接。

I use this line of code to put in session: 我使用以下代码行进行会话:

Session::put('url.intended', $link);

I use this line of code to get from session: 我使用以下代码从会话获取:

Session::get('url.intended', url('/'));

When i run in development and read from session, i get this url as url.intended: 当我在开发中运行并从会话读取时,我将此URL作为url.intended:

http://example.test/profile/data?_url=%2Fprofile%2Fdata

With this url the redirect works fine after login! 使用此URL,重定向在登录后可以正常工作!

Now, when i am in production mode (AWS, ELB, NGINX), i get this url as url.intended: 现在,当我处于生产模式(AWS,ELB,NGINX)时,我将此URL作为url.intended:

https://example.org/profile/data

without ?_url=%2Fprofile%2Fdata so does not work and redirects to homepage. 没有?_url =%2Fprofile%2Fdata,因此无法正常工作并重定向到首页。

It's a Laravel 5.3 issue or loadbalancer and nginx setup is wrong? 这是Laravel 5.3的问题或负载平衡器,而nginx设置错误吗?

nginx configuration: nginx配置:

server {
    listen 80;
    server_name example.org;
    rewrite ^/(.*)$ https://www.$host$request_uri redirect;
}


    server {

        listen 80;
            root /var/www/public;
            index index.php index.html index.htm;
            server_name  www.example.org;

            if (!-e $request_filename) {
                rewrite ^.*$ /index.php last;
            }

            if ($http_x_forwarded_proto = http) {
                return 301 https://$server_name$request_uri;
            }

            location / {
                    sendfile off;
                    try_files $uri $uri/ /index.php?_url=$uri&$args;
            }

            location ~ \.php$ {

                    try_files $uri = 404;
                    fastcgi_pass phpfpm:9000;
                    fastcgi_index /index.php;
                    include fastcgi_params;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            }
    }

Is something wrong with my setup or there is something in laravel that i can do, not to remove the ?_url from url.intended ?? 我的设置有问题吗,或者在laravel中可以执行某些操作,而不是从url.intended中删除?_url?

If u use Laravel 5.3: 如果您使用Laravel 5.3:

Modify the new Middleware auth redirector 修改新的中间件身份验证重定向器

/app/Http/Middleware/RedirectIfAuthenticated.php

Change the handle function to look like that: 将handle函数更改为如下形式:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect()->intended('/home');
    }

    return $next($request);
}

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

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