Welcome Guest, Not a member yet? Register   Sign In
Question: how can I stop controllers from displaying views from within the parent::Controller() function?
#1

[eluser]petervandijck[/eluser]
I'm trying to do something like this:

Code:
function Welcome()
{
parent::Controller();
// HERE I WANT TO STOP THE index() FUNCTION AND ALL OTHERS FROM DISPLAYING VIEWS (depending on a certain condition).    
}    
function index()
{
$this->load->view('welcome_message');
}

In other words, from within the parent::Controller(), I want to be able to override the index() and other functions (stop them from happening). For example, when a user is not logged-in.
#2

[eluser]WanWizard[/eluser]
Redirect to another controller (which is most common when you hit an "access denied" situation).

Alternatively you can set a flag (a class property) that your controller method can test and act upon. Or use some kind of ACL library so you can check the users rights in the controller method requested. You can also use the remap() method to capture all method calls, and have that process the request based on security information. Lots of options, but stopping in a constructor isn't one of them...
#3

[eluser]petervandijck[/eluser]
Thanks, that's helpful Smile
#4

[eluser]petervandijck[/eluser]
So I have to do this:

if (loggedin()) do some stuff;

in every function (like the index() function), I can't just do it once in a constructor or somewhere else? That seems a little annoying (although livable).
#5

[eluser]Eric Barnes[/eluser]
Use a MY_Controller then add the login check there. Of course you also need an "allowed" pages so the login page will still work.
#6

[eluser]WanWizard[/eluser]
Indeed. Use base classes for all repetitive stuff.

Phil Sturgeon wrote an excellent article on Base classes, worth a read.
#7

[eluser]petervandijck[/eluser]
Thanks for that link on base classes, will check that out Smile




Theme © iAndrew 2016 - Forum software by © MyBB