Creating a custom request class


#1

I’m trying to figure out if I can create a custom $request object in Slim 3, with some of my own custom methods. I saw https://github.com/slimphp/Slim/issues/1480 through which I wrote:

'settings' => [
	'request' => function ($c) {
		return \App\Request::createFromEnvironment($c['environment']);
	},
]

$app = new \Slim\App($settings);

Then in my routes:

$app->get('/systems', '\App\Controllers\SystemsController:index');

But in my controller method, my $request object doesn’t have any of the methods in my extended function. Not sure what I’m misunderstanding.


#2

This should do the trick:

$container['request'] = function ($container) {
    // Replace this class with your extended implementation
    return Request::createFromEnvironment($container['environment']);
};

PS: Maybe it would be better to implement a middlware for your purposes.