• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redirect from outside a controller

#1
Is it possible to redirect from outside a controller (from a library to a given URL for example ?).

I read this question : https://forum.codeigniter.com/thread-745...#pid368427 but that does not solve my problem since I don't want to check for authentification.
Reply

#2
(05-12-2020, 06:28 AM)imabot Wrote: Is it possible to redirect from outside a controller (from a library to a given URL for example ?).

I read this question : https://forum.codeigniter.com/thread-745...#pid368427 but that does not solve my problem since I don't want to check for authentification.

I update this question since I don't have a satisfying answer :

A simple example: some pages are restricted to some users, depending on the user rights. I need to load the page data before checking if the current user is allowed to visit the page. Filters are not great because I have to load and process the page data twice (once in the filter and once in the controller). Redirecting from models / libraries / helpers is no longer possible. So it must be done in controllers. The problem is that my controllers becomes more and more complicated. Even worse, I can't split my code into functions, because redirection in CI4 is based on return, so I can't redirect from a private method in my controller or BaseController. Any idea?
Reply

#3
(05-14-2020, 11:24 PM)imabot Wrote:
(05-12-2020, 06:28 AM)imabot Wrote: Is it possible to redirect from outside a controller (from a library to a given URL for example ?).

I read this question : https://forum.codeigniter.com/thread-745...#pid368427 but that does not solve my problem since I don't want to check for authentification.

I update this question since I don't have a satisfying answer :

A simple example: some pages are restricted to some users, depending on the user rights. I need to load the page data before checking if the current user is allowed to visit the page. Filters are not great because I have to load and process the page data twice (once in the filter and once in the controller). Redirecting from models / libraries / helpers is no longer possible. So it must be done in controllers. The problem is that my controllers becomes more and more complicated. Even worse, I can't split my code into functions, because redirection in CI4 is based on return, so I can't redirect from a private method in my controller or BaseController. Any idea?


Without seeing the code (or a example of what you are doing) it's hard to give advice on how to clean it up. You may want to look into doing something like this:

PHP Code:
return (!$this->checkUser()) ? redirect()->to('not-a-user') : view('user-view'$pageData); 

Where $this->checkUser() is a private method/function that returns true or false based on your criteria for a user.


You can also nest that with something like:

PHP Code:
return (!$this->checkUserIsLoggedIn()) ? redirect()->to('not-a-user') : (!$this->checkUserRoleIsAdmin()) ? redirect->to('not-an-admin') : view('user-view',$pageData); 

Where the above code checks if they are even logged in then checks their role with redirects to the appropriate page. and returns the view if everything checks out.
Reply

#4
Thank you for the advices, I'm already able to write these codes, but that's not what I call simple and concises when you have dozen of tests.

In my opinion, there is a fundamental problem with the redirection in CI4. Redirecting only from the controller is too restrictive.
Reply

#5
(05-18-2020, 02:10 AM)imabot Wrote: Thank you for the advices, I'm already able to write these codes, but that's not what I call simple and concises when you have dozen of tests.

In my opinion, there is a fundamental problem with the redirection in CI4. Redirecting only from the controller is too restrictive.

I have not tested this but it should be worth a try.

In order to redirect from some non-controller class, you need to work directly with a Response object. Then you can use Response class methods redirect() and send() directly. You can get a Response object anywhere using Services.

PHP Code:
$response Services::response();
$response
    
->redirect($uri)
    ->send();
exit; 

You might find the RedirectResponse class to be handy instead of the vanilla Response class.  You have to call send() on it too.

That can also be done using the common function redirect() which returns a RedirectResponse  object.

PHP Code:
$response redirect($uri);
$response
    
->withInput()
    ->send();
exit; 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.