CodeIgniter Forums
Redirect inside Constructor NOT WORKING - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Redirect inside Constructor NOT WORKING (/showthread.php?tid=74537)

Pages: 1 2


RE: Redirect inside Constructor NOT WORKING - blaasvaer - 09-03-2020

(12-02-2019, 10:15 AM)kilishan Wrote: "code cleanness" can mean any number of things. For many of those definitions, Controller Filters, as previously mentioned, will keep it clean for you.

If you absolutely must do it from the constructor, you can do a slightly hacky thing and throw a RedirectException with the URL to redirect to as the message. But I highly encourage looking into controller filters, as they're specifically designed to handle the types of situations you're asking about.

Then please add this 'note' to the place where we all seem to end up, namely: typing 'redirect' into the search field of the docs and then onwards from there ...

This is the 'user experience' we get:

Search Results
Search finished, found 18 page(s) matching the search query.
  • redirect (PHP function, in Global Functions and Constants)
  • Security Guidelines
  • URI Routing
  • CodeIgniter URLs
  • Controller Filters
  • CURLRequest Class
  • Email Class
  • Error Handling
  • Global Functions and Constants
  • HTTP Feature Testing
  • Models, Views, and Controllers
  • Security Class
  • Testing Controllers
  • Version 4.0.0-alpha.2
  • Version 4.0.0-alpha.3
  • Version 4.0.0-alpha.4
  • Version 4.0.0-rc.3
  • Version 4.0.0-rc.4
Now, Controller Filters are only fifth in this list ... and it would be nice if we in the first entry; redirect ... as it takes a while to get down the list ... and usually in the process kind of lose track of what one was initially trying to achieve.


RE: Redirect inside Constructor NOT WORKING - cvlancvlan - 08-04-2021

PHP Code:
public function __construct() {
 
 throw new \
CodeIgniter\Router\Exceptions\RedirectException('login');
 
#throw new \CodeIgniter\Router\Exceptions\RedirectException('login', 302);
 
 



More info https://codeigniter.com/user_guide/general/errors.html


RE: Redirect inside Constructor NOT WORKING - paliz - 08-05-2021

Redirect in filter no in ctl


RE: Redirect inside Constructor NOT WORKING - superior - 08-08-2021

I would recommend using Filters as well, keep your code organized is important.