Welcome Guest, Not a member yet? Register   Sign In
How to call a controller from another controller ?
#1

[eluser]Vivigeos[/eluser]
Hi everyone,
Here's my problem... I would like to create a controller which could call other controllers.
For example, I've a controller called "index" used to provide the index page (a blog page here) and I would like too call the "category" controller and one of its methods which displays a list of the categories in a view. Then I would like to add this category view to my index view.
Is it possible ?

Thanks for your time.
#2

[eluser]dark_lord[/eluser]
Yes, this is possible, what I can best suggest is to use partial views for this one.

try:
function index()

Code:
class Blog extends CI_Controller {
    
    function __construct()
    {
            parent::__construct();
    }
    
    function index()
    {
            $data['categories'] = $this->load->view('category/list', null, TRUE);

            $this->load->view('container', $data);
        }
}

Hope that helps!
#3

[eluser]Twisted1919[/eluser]
@WishBear - Wrong.
@Vivigeos - By default with CI you cannot achieve this in a clean way.
You can use a include() to include and instantiate the class controller you want to execute from within you controller, or you can use HMVC (search forums) which can do this for you and of course it will provide modularity to your CI .
L.E: Of course, you can create a blog library and create methods in it which you can call everywhere .
#4

[eluser]Vivigeos[/eluser]
Thank you both, I'll try that Smile
#5

[eluser]juanvillegas[/eluser]
Well, theres no reason to do that, unless you just want to exit the controller and start running another one. In that case, just use redirect() function in the URL helper. See http://ellislab.com/codeigniter/user-gui...elper.html
#6

[eluser]johnpeace[/eluser]
[quote author="juanvillegas" date="1297913279"]Well, theres no reason to do that, unless you just want to exit the controller and start running another one. In that case, just use redirect() function in the URL helper. See http://ellislab.com/codeigniter/user-gui...elper.html[/quote]

It's true, the OP is missing the point of what a controller is/does in MVC arch.

It's not whether it's possible to ask, ask if it's the best way to achieve what you're after (hint: it's not).
#7

[eluser]Twisted1919[/eluser]
@juanvillegas & @johnpeace you are both wrong because you didn't understood what he really wants.
He wants to have something like a sidebar displaying the categories and for that to happen, he needs to generate the content of that from the Categories Controller not from the Blog controller, therefore what he needs cannot be done with a redirect() or whatever, but with generating the content by calling a method of the categories controller(and this can be done as i suggested)
#8

[eluser]dark_lord[/eluser]
@Twisted1919: Doesn't my suggestion answer his question? from the categories controller he will pass the result to the blog controller and load that result to the blog container view?
#9

[eluser]Vivigeos[/eluser]
Twisted describe perfectly what I need and HMVC seems to fulfill my needs.

WishBear : In your exemple, you only load the view but the controller "category" is never called right ? So no data can be send to the view ?
#10

[eluser]Twisted1919[/eluser]
[quote author="WishBear*" date="1297967386"]@Twisted1919: Doesn't my suggestion answer his question? from the categories controller he will pass the result to the blog controller and load that result to the blog container view?[/quote]

You say:
Code:
class Blog extends CI_Controller {
    
    function __construct()
    {
            parent::__construct();
    }
    
    function index()
    {
            $data['categories'] = $this->load->view('category/list', null, TRUE);

            $this->load->view('container', $data);
        }
}
So you assign a view to a variable, right ? IE:
$data['categories'] = $this->load->view('category/list', null, TRUE);
BUT you don't pass the categories to the view.
Assuming that in the loading view, there is a foreach($categories AS $cat) loop going through categories and echo them one by one, in your method, this will error, because the $categories is undefined.
Do you understand my point here ?

In order to make it function, you would need something like:
Code:
class Blog extends CI_Controller {
    
    function __construct()
    {
            parent::__construct();
    }
    
    function index()
    {
            // get the blog categories and assign them to a variable
            $this->load->model('categories_model','cat');
            $categories=$this->cat->get_categories();
      
            $data['categories'] = $this->load->view('category/list', array('categories'=>$categories ), TRUE);
            // now, the category/list view can use the foreach() as stated above.
            $this->load->view('container', $data);
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB