Google News
logo
Slim Framework - Interview Questions
What is Container Resolution in Slim Framework?
You are not limited to defining a function for your routes. In Slim there are a few different ways to define your route action functions.
 
In addition to a function, you may use:
 
* container_key:method
* Class:method
* Class implementing __invoke() method
* container_key

This functionality is enabled by Slim’s Callable Resolver Class. It translates a string entry into a function call. Example:
$app->get('/', '\HomeController:home');
Alternatively, you can take advantage of PHP’s ::class operator which works well with IDE lookup systems and produces the same result:
$app->get('/', \HomeController::class . ':home');
You can also pass an array, the first element of which will contain the name of the class, and the second will contain the name of the method being called:
$app->get('/', [\HomeController::class, 'home']);
In this code above we are defining a / route and telling Slim to execute the home() method on the HomeController class.
 
Slim first looks for an entry of HomeController in the container, if it’s found it will use that instance otherwise it will call it’s constructor with the container as the first argument. Once an instance of the class is created it will then call the specified method using whatever Strategy you have defined.
Advertisement