(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.
You can always redirect using the "vanilla" PHP way:
PHP Code:
//Redirects to Unauthorized main page
header("Location: /unauthorized");
Or send a JSON message and stop php after:
PHP Code:
//Return a JSON response with the error
//Set the header for the response to be identified as a JSON
header('Content-Type: application/json');
//Status Code 401 = Unauthorized
header("HTTP/1.1 401 Unauthorized");
//Writes the JSON message for the user
echo $e->getMessage();
die; //Stops executing the PHP
That is how I did it inside construct...