10

It seems that Redirect::guest('login') will only work for GET requests. Ie. it will redirect an authenticated user to the original intended URL (GET).

In a situation where there is a POST request, is there a way for the auth filter to continue on to POST to a URL after the user has successfully logged on?

A simple example: I want to show a form available to anyone to view. Upon hitting the submit button, the auth filter kicks in which will bring a guest to the login page. After successful authentication, I would like the submit request (ie. POST request) to continue onwards.

4

2 回答 2

11

我同样希望使用原始输入重定向回 POST 请求。除了通过 GET 重定向到预期的 URL 之外,我在 Laravel 中找不到执行此操作的现有方法。

拉拉维尔 5

我首先按照下面的大纲在 Laravel 4 中解决了这个问题,但发现完全相同的设置在 Laravel 5 中不起作用。按照 Laravel 4 的大纲,而不是创建 IntendedUrlServiceProvider 创建一个中间件。

  1. 问题在于,在 Laravel 5 中,会话似乎是从 StartSession 开始的,它在所有 ServiceProviders 之后运行。

/app/Http/Middleware/IntendedUrl.php

<?php namespace App\Http\Middleware;

use Closure;
use Request;
use Session;

class IntendedUrl {

    /**
     * This loads saved POST input data and changes the method to POST if a visitor tried to access a page
     * but was blocked via an auth filter. Auth filter saves data via the Redirect::guest() and after
     * login it needs to be repopulated to simulate a POST.
     *
     * GET requests also may pass through here. I am less certain if it is required for them but shouldn't hurt
     * and may help load any input data.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Check to see if we were redirected to this page with the Redirect::intended().
        //      We extended the class to track when the redirect occurs so we know to reload additional request data
        if (Session::has('intended.load')) {
            // intended.load could be set without these being set if we were redirected to the default page
            //      if either exists, both should exist but checking separately to be safe
            if (Session::has('intended.method')) {
                Request::setMethod(Session::get('intended.method'));
            }
            if (Session::has('intended.input')) {
                Request::replace(Session::get('intended.input'));
            }
            // Erase all session keys created to track the intended request
            Session::forget('intended');

            // Laravel 5.2+ uses separate global and route middlewares. Dispatch altered request as the route type changed. *Credit to Munsio in answer below
            return \Route::dispatch($request);
        }

        return $next($request);
    }

}
  1. 然后,不要像下面的第 4 步那样添加 IntendedUrlServiceProvider,而是在 /app/Http/Kernel.php 的 $middleware 数组中的 StartSession 之后添加新的中间件
protected $middleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',

    'App\Http\Middleware\IntendedUrl',
];
  • 另外值得注意的是,为了组织,我将我的客户服务提供商移动到新标准 /App/Providers 并更改了他们的命名空间。

拉拉维尔 4

我决定扩展框架以添加此功能。很难详细说明我的完整解决方案,但这里有一个大纲。为此,您需要非常熟悉该框架并阅读如何扩展它。 http://laravel.com/docs/extending#ioc-based-extension

我还参考了泰勒的书《从学徒到工匠的 Laravel》

  1. 扩展 Redirector 类以记录有关预期请求的附加信息。

    <?php namespace GQ\Routing;
    class Redirector extends \Illuminate\Routing\Redirector {
        /**
         * ** Extended to add functionality for restoring POST input and the POST method after a login
         */
        public function guest($path, $status = 302, $headers = array(), $secure = null)
        {
            // Recording the method and input for the request so that it can be reloaded after being redirected back to the intended page
            $this->session->put('intended.method', $this->generator->getRequest()->getMethod());
            $this->session->put('intended.input', $this->generator->getRequest()->all());
    
            return parent::guest($path, $status, $headers, $secure);
        }
    
        /**
         * ** Extended to record in the session when we redirect to an intended page so method and input can be loaded on the next page
         */
        public function intended($default = '/', $status = 302, $headers = array(), $secure = null)
        {
            $redirect_response = parent::intended($default, $status, $headers, $secure);
    
            // Set the intended.load session variable so we know we returned to the intended page and can load the additional method and input
            return $redirect_response->with('intended.load', true);
        }
    }
    ?>
    
  2. 创建一个新的服务提供者,它在 IOC 容器中写入“重定向”。我最初尝试扩展 RoutingServiceProvider 但无法正常工作。

    <?php namespace App\Providers;
    
    use GQ\Routing\Redirector;
    use Illuminate\Support\ServiceProvider;
    class RedirectServiceProvider extends ServiceProvider {
    
        protected $defer = true;
    
        /**
         * Register the Redirector service.
         *
         * ** Copy of class registerRedirector from RoutingServiceProvider,
         * using a different "use" statement at the top to use the extended Redirector class
         * Extending the RoutingServiceProvider was more of a pain to do right since it is loaded as a base provider in the Application
         *
         * @return void
         */
        public function register()
        {
            $this->app['redirect'] = $this->app->share(function($app)
            {
                $redirector = new Redirector($app['url']);
    
                // If the session is set on the application instance, we'll inject it into
                // the redirector instance. This allows the redirect responses to allow
                // for the quite convenient "with" methods that flash to the session.
                if (isset($app['session.store']))
                {
                    $redirector->setSession($app['session.store']);
                }
    
                return $redirector;
            });
        }
        public function provides() {
            return array('redirect');
        }
    }
    
  3. 创建一个新的服务提供者,它将在重定向后设置预期的方法和输入。

    <?php
    
    namespace GQ\Providers;
    
    use Illuminate\Support\ServiceProvider;
    
    class IntendedUrlServiceProvider extends ServiceProvider {
        /**
         * Bootstrap the application events.
         *
         * @return void
         */
        public function boot() {
            // Check to see if we were redirected to this page with the Redirect::intended().
            //        We extended the class to track when the redirect occurs so we know to reload additional request data
            if (\Session::has('intended.load')) {
                // intended.load could be set without these being set if we were redirected to the default page
                //        if either exists, both should exist but checking separately to be safe
                if (\Session::has('intended.method')) {
                    \Request::setMethod(\Session::get('intended.method'));
                }
                if (\Session::has('intended.input')) {
                    \Request::replace(\Session::get('intended.input'));
                }
                // Erase all session keys created to track the intended request
                \Session::forget('intended');
            }
        }
    
        public function register() {
        }
    }
    
  4. 最后将您的 2 个新服务提供商添加到 app/config/app.php 中的 providers 数组

    'GQ\Providers\RedirectServiceProvider',
    'GQ\Providers\IntendedUrlServiceProvider',
    

希望这能引导你朝着一个好的方向前进。这对我有用,但我没有对它进行广泛的测试。也许如果它继续运行良好,我们可以构建一个作曲家包或获得 Laravel 中包含的功能。

于 2013-12-20T21:30:21.840 回答
1

在 Laravel 5.2 中,他们实现了中间件组,对于新项目,他们将默认的“web”组应用于整个 routes.php 文件。

问题:
组中间件是在路由确定后调用的,所以简单地改变当前请求的方法是没有效果的。

有两种不同的方法可以让它恢复工作(我建议 nr. 2)


解决方案 1:
将会话和预期的 url 中间件放回 Kernel.php 文件中的全局中间件数组 - 这很容易并且可以工作,但有时您的项目旁边有一些 REST-API 路由,恕我直言,会话与那里无关.


解决方案2:
将想要的url类放在web组的ShareErrorsFromSession类之后,并采用如下所示的文件:

// Erase all session keys created to track the intended request
Session::forget('intended');

$response = Route::dispatch($request);
return $response;

通过调度修改后的请求,我们打破了当前的生命周期并调用了一个新的生命周期,因此使用了正确的路由并按预期工作。第二种方法还使我们可以根据需要将意图的 url 功能仅定义到选定的路由。

于 2016-07-20T07:09:23.383 回答