我同样希望使用原始输入重定向回 POST 请求。除了通过 GET 重定向到预期的 URL 之外,我在 Laravel 中找不到执行此操作的现有方法。
拉拉维尔 5
我首先按照下面的大纲在 Laravel 4 中解决了这个问题,但发现完全相同的设置在 Laravel 5 中不起作用。按照 Laravel 4 的大纲,而不是创建 IntendedUrlServiceProvider 创建一个中间件。
- 问题在于,在 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);
}
}
- 然后,不要像下面的第 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》
扩展 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);
}
}
?>
创建一个新的服务提供者,它在 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');
}
}
创建一个新的服务提供者,它将在重定向后设置预期的方法和输入。
<?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() {
}
}
最后将您的 2 个新服务提供商添加到 app/config/app.php 中的 providers 数组
'GQ\Providers\RedirectServiceProvider',
'GQ\Providers\IntendedUrlServiceProvider',
希望这能引导你朝着一个好的方向前进。这对我有用,但我没有对它进行广泛的测试。也许如果它继续运行良好,我们可以构建一个作曲家包或获得 Laravel 中包含的功能。