Welcome Guest, Not a member yet? Register   Sign In
pick between two controllers from one URL
#1

[eluser]Eric_WVGG[/eluser]
I have a controller class called List. It is extended by a bunch of subclasses — including List_all and List_mine — which provide different database queries and output handling to List.

I need my base-level URL (http://mysite.com/) to serve List_all if the user is not logged in, and List_mine if a login is present.

If I were not working within CodeIgniter, I'd do something like...
Code:
if( $is_logged_in ) $page = new List_mine;
else $page = new List_all;
... but CI can't call a controller from within another controller. (The Wick CI library seems to do this but is not considered production-ready, and voodoo and whatnot.)

So, thoughts?
#2

[eluser]WanWizard[/eluser]
Yes, don't try to solve this using controllers.

Controllers in an MVC pattern are, as the name implies, only meant to control the flow from URI request to output. This is a classic case for two different models, which are meant to house all your data handling code.

If you want to do the same as above, you could do:
Code:
// load the correct page model based on the logged-in state
if ( $is_logged_in )
{
    $this->load->model('list_mine', 'page');
}
else
{
    $this->load->model('list_all', 'page');
}
// call the model
echo $this->page->do_something();
#3

[eluser]bretticus[/eluser]
Yeah, your implementation is very inadvisable. You are using controllers for which uses controllers are not meant. The only definition of a controller from the user manual is:

Quote:A Controller is simply a class file that is named in a way that can be associated with a URI.

It really shouldn't be doing things that libraries and models are meant to do. You can quite easily (and should) put this functionality into your models. I know people extend stuff like security into controllers, but that tends to lead to bad design that puts you in situations like this.
#4

[eluser]Eric_WVGG[/eluser]
That makes perfect sense, thank you.




Theme © iAndrew 2016 - Forum software by © MyBB