This code basically takes in a request and add escape quotes to the input data, thus data passing to controllers will be filtered and incase of base64 encoded data coming as input we are avoiding that to be filtered
When registering your middleware you have 3 choices.
- First choice is that you add the middleware to be run on every request handled by your app. You can do that by opening up App\Http\Kernel.php and adding it to your $middleware array like so:
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array<int, class-string|string>
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
// our new class.
\App\Http\Middleware\PreventSQLInjection::class,
];
- Second choice is to have the middleware run on registered routes only, you can register it like so:
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array<string, class-string|string>
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
// our new class.
'sql' => \App\Http\Middleware\PreventSQLInjection::class,
];
And then add the middleware to the specific routes like so:
Route::get('/ip', ['middleware' => 'sql', function() { return "IP"; }]);
- The last option would be adding it directly to the route group
Route::group(['namespace' => 'Api', 'middleware' => [\App\Http\Middleware\PreventSQLInjection::class]], function () {
Route::get('/app-basic-details', [HomeController::class, 'applicationBasicDetails']);
Route::get('/terms-conditions', [HomeController::class, 'termsConditions']);
// And all other routes will be here ...
});