Hello !
Currently I using Container Resolution on my Controllers and I was able to “extend” the Routing Strategy by modifying RequestResponse.php.
Code Example:
/**
* Default route callback strategy with route parameters as an array of arguments.
*/
class RequestResponse implements InvocationStrategyInterface
{
/**
* Invoke a route callable with request, response, and all route parameters
* as an array of arguments.
*
* @param array|callable $callable
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param array $routeArguments
*
* @return mixed
*/
public function __invoke(
callable $callable,
ServerRequestInterface $request,
ResponseInterface $response,
array $routeArguments
) {
foreach ($routeArguments as $k => $v) {
$request = $request->withAttribute($k, $v);
}
---------------------------- HERE IS THE CHANGE!!! ------------------------
if (!$callable instanceof Closure && $callable[0] instanceof BaseController) {
$controller = $callable[0];
$controller($request, $response, $routeArguments);
}
---------------------------- END OF CHANGE ------------------------
return call_user_func($callable, $request, $response, $routeArguments);
}
The idea behind this is to my Controller process all Slim Data ( Using my Parsers ) and to give my ControllerBase more control over the PSR Objects beforehand. ( Give me thoughts about this or if this is a bad practise at all )
Its a simple trick to pass the PSR objects and arguments directly to my Controller, instead of calling in each method of the class parent__invoke(); and keep the normal call to the method.
Example of this usage:
// Route Controller Classes extends from BaseController.
// This could be also registerUser(){} without the PSR Objects.
// return Slim Response Object
function registerUser(Request $request, Response $response, Array $args){
// Method from my Controller that returns a formated type of data using Request Body for my own purposes.
$data = this->getRequestBody();
return $this->getResponse();
}
The thing is that Middlewares does not follow this RequestResponse Strategy and I would like to do the same to my Middlewares ( Yes I use a MiddlewareController ).
At the moment I have to call parent__invoke(request,response) at the top of my middleware method.
Example:
// Middlewares classes extends from BaseController
function middleware(Request $request, Response $response){
// Don't wanna call that.
// parent__invoke($request,$response);
// Do Stuffs....
}
I would like to understand where the Strategy is invoked so I can learn how to extend Middlewares functionality to my purposes.
Thanks in Advance,
LosLobos.