slimphp / Slim

Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.
http://slimframework.com
MIT License
11.94k stars 1.95k forks source link

Change default controller if go middleware #1911

Closed andrey900 closed 8 years ago

andrey900 commented 8 years ago

Can you tell, and if I could change the controller or method used by default when you go middleware? Example:

$app->get('/articles/{page}', "DefaultControler:indexAction")->add(new ControllerChanger());

class ControllerChanger{
    public function __invoke($request, $response, $next)
    {
        // Change controller handler if
/* 
* case($page) ... 
* DefaultControler:tagAction
* SearchControler:indexAction
*/
        return $response;
    }
}
geggleto commented 8 years ago

There is no way to change the Action of a Route once it has been created.

Reference: https://github.com/slimphp/Slim/blob/3.x/Slim/Route.php Getter no Setter.

JoeBengalen commented 8 years ago

No, but you can update the route in middleware before the action is determined.

But why would you need to? Just register multiple routes (one for each switch case in your example)

andrey900 commented 8 years ago

I generate routes on the fly from a database, but I want to be able to change the default behavior of the controller, depending on a URL. I thought that there will use middleware. I'll try use, the pattern strategy for the controller.

silentworks commented 8 years ago

I think @JoeBengalen answer is a solution to your issue. Closing this issue for that reason. Please reopen if you feel its not resolved.