redirect() in base controller - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: redirect() in base controller (/showthread.php?tid=87125) Pages:
1
2
|
redirect() in base controller - sTis - 03-16-2023 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 RE: redirect() in base controller - kenjis - 03-16-2023 If you want to redirect, you must return a RedirectResponse object from the controller. Does the controller return a RedirectResponse object? RE: redirect() in base controller - sTis - 03-16-2023 Thank for your answer. I saw that in ci4 documentation but.. I'm not sure how to do that. Could you explain it please ? Function check is in standard baseController. RE: redirect() in base controller - kenjis - 03-16-2023 The constructor method __construct() cannot return values. It is the specification in PHP language. And initController() in CI4 Controller also cannot return values. RE: redirect() in base controller - sTis - 03-17-2023 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() { RE: redirect() in base controller - sTis - 03-19-2023 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 RE: redirect() in base controller - InsiteFX - 03-19-2023 You should use a Filter for redirecting. SEE: Controllers and Routing Controller Filters RE: redirect() in base controller - iRedds - 03-20-2023 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. RE: redirect() in base controller - sTis - 03-24-2023 Hi, @InsiteFX : I tought about filter but... checkAccess function need parameters that are different for each controller. I explain : each controller list, in an array, roles that can access it. How can I, in a filter, do this ? I thought to do it in routes definition but I'm not sure how to make it work. @iRedds : Thank, I will explore this way. RE: redirect() in base controller - Muzikant - 04-19-2023 (03-16-2023, 04:02 AM)kenjis Wrote: If you want to redirect, you must return a RedirectResponse object from the controller. I am strugling whit this too. There should be a simple example in a documentation of how to use it. For example: PHP Code: $redirectResponse = \config\Services::redirectresponse(); Is it a recommended way of using it? |