Welcome Guest, Not a member yet? Register   Sign In
How to use several classes on one page?
#1

[eluser]mr. echo[/eluser]
Hello there!

I just installed ci today and after my first glance, tries and tutorials I must say: It's great! Congratulations to the authors!! :-)

My question is probably a typical newbie question and is likely already covered somewhere. However I couldn't find a proper answer. So if anybody could provide me with some links...

Let's say I want to create a project where I want to create objects of several classes on specific pages, e.g.:

- navigation-class, same class implemented on every page
- content-class, same class implemented on every page
- contact-form-class implemented on page x only
- etc.

How do I go about doing this since as I understand it on each view-file (which will be my uri & page) I can only implement one controller? Any answer / link would be appreciated!

Thanks.
Mr echo

PS: A hint on how to create a navigation-class I found in this thread:
http://ellislab.com/forums/viewthread/98704/
But it has a rather brief answer and no tipps on how to implement,
#2

[eluser]mr. echo[/eluser]
O.K., I guess I found the solution: ;-)

If I want sth. to appear on every page of my project, e.g. a header, I would extend the core class Controller
( http://ellislab.com/codeigniter/user-gui...asses.html )
Code:
<?php
class MY_Controller extends Controller {

    function MY_Controller()
    {
        echo 'HEADER'; //in live sytem will be replaced by $this->load->view...
        parent::Controller();
    }
}

Is this best practice? Any comments / suggestions?
Thx
#3

[eluser]jedd[/eluser]
Well, it's a practice. Smile

Have a read of this thread - it's what I've adopted for my practice:
[url="http://ellislab.com/forums/viewthread/106945/"]http://ellislab.com/forums/viewthread/106945/[/url]

I'd suggest that you don't call out to view within the constructor, though. It doesn't seem the most elegant approach, as there'll always be exceptions. It might be better to have a pro forma section that assembles your views and then dumps them out. Again, this is what I've started doing after some advice on here, and it's working reasonably well.

But yes, extending the controller is pretty common practice I gather.

Here's mine (somewhat snipped)
Code:
function MY_Controller ()  {
        parent::Controller();

        $this->data['main_menu_data'] = $this->_generate_menu_data();
        }

    private function  _generate_menu_data ()  {
        // @TODO - lookup user-id from auth system and use it to generate
        // the 'users/$id' link below under MEMBERS[]
        $menu_data_array = array (
                            "Help" => array (
                                "About this site"   => "meta/about",
                                "Contact us"        => "meta/contactus",
                                ),
                            "Places" => array (
                                "Browse"  => "locations/browse",
                                "Search"  => "locations/search",
                                "Compare" => "locations/compare",
                                ),
                            );

        // @TODO - set up parallel ADMIN_MENU_ARRAY .. and test here
        // if they're an admin, and if they are merge() the two arrays

        return ($menu_data_array);
        }

My view code is very basic - perhaps 40 lines - and includes a bunch of div's with echos inside that dump the 4 or 5 major components of the page.
#4

[eluser]mr. echo[/eluser]
Thx for your answer!

It seems though that I always have to initialize the component I want to appear on each page. If e.g. I always want a header to appear on every page, I always have to type
Code:
$this->head();
in each controller, don't I?

Or is there a simple way to make a code-snip appear on every page where I want it to appear?
#5

[eluser]jedd[/eluser]
[quote author="mr. echo" date="1236573990"]Thx for your answer!
Or is there a simple way to make a code-snip appear on every page where I want it to appear?[/quote]

There is. Go and read [url="http://ellislab.com/forums/viewreply/538402/"]this particular bit[/url] from the thread I posted above.

So the answer to your question is yes and no. As always Smile

No, you don't have to initialise it - that's done in the MY_controller which happens automatically for all your inherited controllers.

If you adopt the approach from that thread (above) then your template view becomes all you ever call, and you just modify the contents of the other DIVs. That is to say, your consistent menu code (for example) doesn't require the code-snip on each page. And that's the Yes bit, though it assumes you're happy to go with that 'generic view' approach and make calls out to generate the views for the DIVs within the full page view.

Does this make more sense?
#6

[eluser]mr. echo[/eluser]
[quote author="jedd" date="1236574947"]Does this make more sense?[/quote]Uh..well I guess I have to get into this a bit more deeply.... Thanks for trying to help a newbie! :-)

I just found this though:
Quote:Template is right for you if:

* You feel like using views can be clunky, especially when "embedding" views.
* You don't like calling header, footer, and other global views from every Controller method.
* You prefer having one "master template" that can be changed for any controller in order to meet unique application design needs.
* You don't want to drastically alter the way you interface controllers and views.
* You like clear, thorough documentation on par with CodeIgniter's User Guide.
Yes, I do! ;-)
http://www.williamsconcepts.com/ci/codei...index.html




Theme © iAndrew 2016 - Forum software by © MyBB