redirect() in base controller |
Hi,
In my baseController I've got a function that check if user's role is compatible with the controller's roles. Here is this function : PHP Code: function check(array $roles) { I read and search about using redirect function in basecontroller but wasn't able to find a solution. Any idea ? If you think that I'm trying to do it in a wrong way tell it to me. Thanks
If you want to redirect, you must return a RedirectResponse object from the controller.
Does the controller return a RedirectResponse object?
The constructor method __construct() cannot return values.
It is the specification in PHP language. And initController() in CI4 Controller also cannot return values.
Thanks for your clues !
I succeeded by changing two three things. I'm not sure that the best way to do it but it works. I've loaded "response" as a service. Can't succeeded to make it works with "redirectResponse". Instead of creating a new function in baseController I choose to create a library GAuthLib. If the specialists can give me an opinion. I will be very grateful. My baseController : Code: <?php My auth library : Code: <?php And my test controller : Code: function testRole() {
Somebody can help ??? What I'm I doing wrong ??
I'm still on my way to achieve my function checkRole working correctly. Kenjis said it must return an redirectResponse object. So I've changed my library. I replace $response = \config\Services::response(); by $redirectResponse = \config\Services::redirectresponse(); Here is my code : PHP Code: <?php $redirectResponse->to() return a redirectResponse object (I think !!). Here is a dump on it (just a part) : PHP Code: C:\wamp64\www\g\app\Libraries\GAuthLib.php:15: So if someone can explain me how to achieve this function working correctly... Thanks
You should use a Filter for redirecting.
SEE: Controllers and Routing Controller Filters What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Your code doesn't return anything.
Yes, your library returns some result, but the controller in which this library is called does not return the result of the library. |
Welcome Guest, Not a member yet? Register Sign In |