Welcome Guest, Not a member yet? Register   Sign In
How to create a common controller type? Does it makes sense?
#1

Hello,

first, thanks for this fine framework.
I'll still starting, but feel fine.

I've create a controller which can load several pages as parameter.

Code:
    public function view($page = "dashboard")
    {
        if ( ! file_exists(APPPATH.'/views/page/'.$page.'.php'))
        {
            show_404();
        }
        
        $this->load->model('pages_nav');
        
        $data = array();
        $data['nav_list'] = $this->pages_nav->getNavArray();
        $data['titles'] = $this->pages_nav->getTitlesForPage($page);
        $data['base_url'] = base_url();
        $data['page_title'] = "Mail2Log";
        $data['current_user'] = "Stefan Kittel";
        $data['logout_url'] = base_url() . "logout";
        
        $this->load->view('page/_header', $data);
        $this->load->view('page/_nav', $data);
        $this->load->view('page/_top_header', $data);
        $this->load->view('page/_content_pre', $data);
        $this->load->view("page/" . $page);
        $this->load->view('page/_content_post', $data);
        $this->load->view('page/_footer1', $data);
        $this->load->view('page/_list', $data);
        $this->load->view('page/_footer2', $data);
    }

I use a modul to dynamic create an array getNavArray().

This works fine, but I'm unsure how to work on with this.
I want to add JSQuery datatables and more to several pages.

So I can select here which page to show and load the data I need.
But I think this will look awefull and I will loose the overview.

I could create one controller for each sections.
But than I must add this block to alle controllers.
If I change the theme, I must modify all controllers.

Can I extend the controller to my own version?
Or should I create a library where I call GetPageInfos and GetAllPageViews?

Thanks

Stefan
Reply
#2

Use a "base controller". Search the forum for this Smile
Reply
#3

Hello,

I've not found anything complete here, but there
https://philsturgeon.uk/codeigniter/2010...ng-it-DRY/

Thanks

Stefan
Reply
#4

Hmmm, when I search our forum for "base controller", I come up with the following on just the first page:
http://forum.codeigniter.com/thread-6391...controller
http://forum.codeigniter.com/thread-6347...controller
http://forum.codeigniter.com/thread-6319...controller
http://forum.codeigniter.com/thread-6315...controller
http://forum.codeigniter.com/thread-6278...controller
http://forum.codeigniter.com/thread-6277...controller

Each seems relevant to your question.

The user guide is relevant, though it does not explicitly address the base controller...
http://www.codeigniter.com/user_guide/ge...asses.html

Lonnie's book would help:
http://www.codeigniter.com/user_guide/ge...asses.html

And, if you are looking for a concrete example:
https://github.com/bcit-ci/codeigniter-website

And you already found Phil's excellent article Smile
Reply
#5

(This post was last modified: 01-26-2016, 03:13 PM by cartalot.)

You want to call a bunch of view files in a standard way. so it can be appropriate to put those calls in a template.
Here's one way to do it:
http://forum.codeigniter.com/thread-6379...#pid326426

very similar example with added example of calling a model for a dynamic header
http://stackoverflow.com/questions/34909...4#34911214
Reply
#6

You should really consider the use of a template engine. Check the Smarty project. You'll find a lot of tutorials to integrate that with you CI installation
Best regards,
José Postiga
Senior Backend Developer
Reply




Theme © iAndrew 2016 - Forum software by © MyBB