2

Is there a way to load a controller from a view ?

Here is what i am affter.. I want to use one view multiple times, but this view is being loaded by separate controller that gives the view, information from the db.So becouse of that information from the model i can't just set $this-load->view(); and etc. Is there a way to do this thing, or it has a better way ?

1
  • Can you provide example of your sites code/what it does? Commented Apr 29, 2011 at 16:32

5 Answers 5

4

I think a lot of sites face similar challenges, including one I'm working on that loads the same db content into the sidebar on almost every page in the site. I implemented this with the combination of a library and a helper:

  1. Put the data logic into the library (mine is named common.php). In addition to interfacing with the database, you may want the library to store the data in a local variable in case you want to reference it multiple times on a single load.
    public function get_total_items()
    {
        if ($this->_total_items === NULL)
        {
            $row = $this->ci->db->query("SELECT COUNT(*) FROM items")->row();
            $this->_total_items = $row[0];
        }
        return $this->_total_items;
    }
    
  2. Create a helper to load the library. (Don't load libraries within a view!) I have MY_text_helper that loads the library and returns the data:

    function total_items()
    {
        $CI =& get_instance();
        return $CI->common->get_total_items();
    }

  3. Call the helper function from within the view.

    <p> Total items: <?php echo total_items(); ?> </p>

Sign up to request clarification or add additional context in comments.

3 Comments

Interesting way of doing it, it seems very wordpress-ish and not very codeigniter-y. If have many different files are all including that library and calling upon it; you're probably doing it wrong, especially when creating a sidebar. Instead they should be wrapped into a template controller.
I'm not sure I understand "wordpress-ish" vs. "codeigniter-y", but I'm always open to better suggestions for including my sidebar.php stub in any view, without replicating code across all controllers. (Also, what would a "template controller" be in an MVC framework? Do you mean creating a MY_Controller wrapper than handles the logic?)
Hi. Thanks for the guidance. I just need to know why I shall load both the library and the helper. Thanks a lot.
3

Simply put, you can't and shouldn't load a controller from a view. That sad, I understand your frustration because you want to re-use the model-pulling/acting logic in the controller across multiples views.

There are various ways of doing this;

  1. Re-use the models. Your models should be very simple to select data from, and should be sleek, but if you're doing the same thing over and over it does seem stupid. In which case...

  2. Use a controller as a "main container" and extend upon it from any logic you need. So your basically using the controller as a template, which pulls data down from the model, loads the appropriate view.

Comments

1

MVC doesn't work that way ... Just re-use the model - that's why it's separate from the controller. If that doesn't fit your needs, you should probably implement a library that does the logic.

1 Comment

Yea i know, but just asking.. it isn't problem with the many implementations of the view but it's longer.And the library may be is the answer :)
0

I would use a library.

That way you can wrap up the data retrieval in a reusable package that you can call from any controller you like.

Comments

0

just do this if you controller named controller1 put a link in view just like that

http://your-site.com/index.php/controller1/

if you want specific function add it to your url

http://your-site.com/index.php/controller1/myfunction

that's it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.