Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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..



99.6k
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.



157

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.