We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Using condition on router for dispatching or different controller and action

I'm trying to use one uri in router but used different controller for some users role:

<?php
$routerGroup->addGet('example',function(){
$role = ?? //how can I get role from session from here
if('admin'==$role){
[
'controller'=>'example',
'action'=>'displayforadmin'
]
}
else{
[
'controller'=>'example',
'action'=>'displayforpublic'
]
    }
});
  • How can I use session from router like that ?
  • How can I use condition controller used by $role value?

Thank You in advance..



145.0k
Accepted
answer

First of all - it doesn't accept function as second argument.

The easiest will be just to have one action for both and inside it check if role is admin then execute other method and if it's other then other method.



526

Hi Wojciech Ślawski, Thanks for your response and idea. I think that's a really simple and great idea! Thanks for the answer.

First of all - it doesn't accept function as second argument.

The easiest will be just to have one action for both and inside it check if role is admin then execute other method and if it's other then other method.