Last active
July 18, 2017 13:44
-
-
Save mikimaine/2b35a805a35edaa290c125bcb9b4e254 to your computer and use it in GitHub Desktop.
Request service provider for Lumen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Http\Requests\Request; | |
use Laravel\Lumen\Http\Redirector; | |
use Illuminate\Support\ServiceProvider; | |
use Symfony\Component\HttpFoundation\Request; | |
use Illuminate\Contracts\Validation\ValidatesWhenResolved; | |
class FormRequestServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register the service provider. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
/** | |
* Bootstrap the application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
$this->app->afterResolving(ValidatesWhenResolved::class, function ($resolved) { | |
$resolved->validate(); | |
}); | |
$this->app->resolving(FormRequest::class, function ($request, $app) { | |
$this->initializeRequest($request, $app['request']); | |
$request->setContainer($app)->setRedirector($app->make(Redirector::class)); | |
}); | |
} | |
/** | |
* Initialize the form request with data from the given request. | |
* | |
* @param FormRequest $form | |
* @param \Symfony\Component\HttpFoundation\Request $current | |
* @return void | |
*/ | |
protected function initializeRequest(FormRequest $form, Request $current) | |
{ | |
$files = $current->files->all(); | |
$files = is_array($files) ? array_filter($files) : $files; | |
$form->initialize( | |
$current->query->all(), $current->request->all(), $current->attributes->all(), | |
$current->cookies->all(), $files, $current->server->all(), $current->getContent() | |
); | |
$form->setJson($current->json()); | |
if ($session = $current->getSession()) { | |
$form->setLaravelSession($session); | |
} | |
$form->setUserResolver($current->getUserResolver()); | |
$form->setRouteResolver($current->getRouteResolver()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment