Created
November 17, 2015 14:59
-
-
Save ceccode/313e7148ab8dd00241ee to your computer and use it in GitHub Desktop.
Laravel ReplaceTestVars Middleware - seen here: https://laracasts.com/discuss/channels/testing/l5-phpunit-testing-the-post-request-with-csrf
This file contains hidden or 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\Http\Middleware; | |
| use Closure; | |
| use Illuminate\Contracts\Routing\Middleware; | |
| use Illuminate\Contracts\Foundation\Application; | |
| class ReplaceTestVars implements Middleware | |
| { | |
| /** | |
| * The application implementation. | |
| * | |
| * @var Application | |
| */ | |
| protected $app; | |
| /** | |
| * Create a new filter instance. | |
| * | |
| * @param Application $app | |
| * | |
| * @return \App\Http\Middleware\ReplaceTestVars | |
| */ | |
| public function __construct(Application $app) | |
| { | |
| $this->app = $app; | |
| } | |
| /** | |
| * Handle an incoming request. | |
| * | |
| * @param \Illuminate\Http\Request $request | |
| * @param \Closure $next | |
| * | |
| * @return mixed | |
| */ | |
| public function handle($request, Closure $next) | |
| { | |
| if ('testing' === $this->app->environment() && ($request->isMethod('post') || $request->isMethod('put')) ) { | |
| $input = $request->all(); | |
| $input['_token'] = $request->session()->token(); | |
| $request->replace($input); | |
| } | |
| return $next($request); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment