Welcome Guest, Not a member yet? Register   Sign In
Redirect inside Constructor NOT WORKING

(This post was last modified: 09-03-2020, 01:51 AM by blaasvaer.)

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

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

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

Redirect in filter no in ctl
Enlightenment  Is  Freedom

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

Theme © iAndrew 2016 - Forum software by © MyBB