Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify input in laravel middleware

Some service makes HTTP request to my site and passes some input. This input has a little bit wrong structure for me, so I'm trying to modify it.

I made a middleware and attached this middleware to my route. The handle method looks like this:

public function handle($request, Closure $next)
{
    $input = $request->all();

    // Input modification

    $request->replace($input);
    \Log::info($request->all()); // Shows modified request

    return $next($request);
}

However in my controller I got old input. Also I'm a little bit confused since I also use FormRequest, and as I realize these two requests are different entities. Then how can I modify the input in the middleware?

like image 508
Victor Avatar asked Nov 28 '15 19:11

Victor


People also ask

How do I change input value in Laravel?

You can use Input::merge() to replace single items. Input::merge(['inputname' => 'new value']); Or use Input::replace() to replace the entire input array. Note: Input:: is just a facade for app('request') .

What is custom middleware in Laravel?

Middleware are used to create a layer between request and response of the http request. it filters or create a logic before the request serve to the controller and also filter or modify the response. we can also use it to validate the user auth, JSON request to manipulate the request and response.

How can you assign middleware in Laravel?

Assigning Middleware To Routes If you would like to assign middleware to specific routes, you should first assign the middleware a key in your application's app/Http/Kernel.php file. By default, the $routeMiddleware property of this class contains entries for the middleware included with Laravel.

Can Laravel use middleware?

Laravel Middleware acts as a bridge between a request and a reaction. It is a type of sifting component. Laravel incorporates a middleware that confirms whether or not the client of the application is verified. If the client is confirmed, it diverts to the home page otherwise, it diverts to the login page.


1 Answers

I don't know what's the exact problem in your case but I'll show you what I did to make it work and it might solve your problem:

app/Http/Middleware/TestMiddleware.php

<?php namespace App\Http\Middleware;

use Closure;

class TestMiddleware
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $input = $request->all();

        if (isset($input['mod'])) {
            list($input['int'], $input['text']) = explode('-', $input['mod']);
            unset($input['mod']);
            // Input modification
            $request->replace($input);

            \Log::info($request->all()); // Shows modified request
        }

        return $next($request);
    }

}

app/Http/Kernel.php

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\VerifyCsrfToken',
    Middleware\TestMiddleware::class, // this line added
];

app/Http/routes.php

 Route::get('/test', ['uses' => 'TestController@index']);

app/Http/Requests/SampleRequest.php

<?php namespace App\Http\Requests;

class SampleRequest extends Request
{        
    public function rules()
    {
        return [
            'int'              =>
                [
                    'required',
                    'integer'
                ],
            'text' => [
                'max: 5',
            ]
        ];
    }
}

app/Http/Controllers/TestController.php

<?php namespace App\Http\Controllers;

use App\Http\Requests;


class TestController extends \Illuminate\Routing\Controller
{

    public function index(Requests\SampleRequest $request)
    {
       dd($request->all());

    }
}

In console I've run composer dump-autoload.

Now when I run the following url:

http://testproject.app/test?mod=23-tav

I'm getting in controller from dd:

array:2 [▼
  "text" => "tav"
  "int" => "23"
]

as expected and when I run for example http://testproject.app/test?mod=abc-tav I'm being redirected to mainpage in my case because data doesn't pass validation from SampleRequest (int is not integer)

like image 50
Marcin Nabiałek Avatar answered Nov 06 '22 05:11

Marcin Nabiałek