Slim 4 how to get $request in a BaseController

Hi,
in Slim3 , I use a BaseController to get or check some pieces of request, using the request service of the container.
I’ve no idea on how to reproduce it with Slim 4, or take an alternate way. Some help would be welcome.
Here is an example of what I do:

class BaseController
{
    protected $container;

    public function __construct($container)
    {
        $this->container = $container;
    }

    protected function parseBoolParam($param, $required = false, $default = null)
    {
        $value = $this->container->get('request')->getParam($param);

        if (is_null($value)) {
            if ($required) {
                throw new \UnexpectedValueException(
                    sprintf("Param [%s] required.", $param));
            }
            return $default;
        } else {
            return in_array(strtolower($value), [1, 'true', 'yes', 'on', 'succeed']);
        }
    }
}


Pascal

In Slim 4 the request instance is not in the container anymore.

http://www.slimframework.com/docs/v4/

Thanks, Daniel, I know that.
I wonder whether there is an alternate way to factorize some methods that operate on $request, and put it in a BaseController class that will be extended by other controllers classes.

You can decorate the request with your utility methods using Decorator Pattern, then change the application ServerRequestFactory to your implementation, the same way the slim/http packages does it.

Another way is to use Traits in your controllers which declare those methods with an extra parameter for the request instance. But I think that the decorator is a more elegant solution.


Many thanks, Gustavo, I’ll give a try with your solution based on Decorator Pattern.

Pascal