Welcome Guest, Not a member yet? Register   Sign In
best approach for multi controllers with little difference.
#1

[eluser]RpR[/eluser]
Hi,

I am trying to convert my old php to codeigniter to learn some more about codeigniter.
Now I am facing this.

I have some controllers
contact,projects,introduction they all contain the same except some vars which are different. What would be the best approach to code without copy past. I was thinking using a model but that is more for db access.

Example.
contact:
Code:
<?php

class Contact extends Controller {

    function Contact()
    {
        parent::Controller();
    }
    
    function index()
    {
        $data_top['title1'] = "Contact";
        $data_top['url1'] = "";
        $this->load->view('top',$data_top);
        $this->load->view('content');
        $this->load->view('bottom');
    }
}

/* End of file contact.php */
/* Location: ./system/application/controllers/contact.php */

Introduction which is voorstelling in Dutch Smile
Code:
<?php

class Voorstelling extends Controller {

    function Voorstelling()
    {
        parent::Controller();
    }
    
    function index()
    {
        $data_top['title1'] = "Voorstelling";
        $data_top['url1'] = "";
        $this->load->view('top',$data_top);
        $this->load->view('content');
        $this->load->view('bottom');
    }
}

/* End of file voorstelling.php */
/* Location: ./system/application/controllers/voorstelling.php */

As you can see only the $data_top parameters are different and the content will be replaced by a different page but for the rest it stays the same.

Thanks for any input.
#2

[eluser]garymardell[/eluser]
A model would be fine. A model is not just for database access, a model can relate to any data, whether it be xml, from e text document or as your doing. A model just is a means of controlling data and it looks to me that all your wanting is common data. So i think the model is the ideal place to store this.
#3

[eluser]Mirage[/eluser]
HI -

First of all there is generally nothing wrong with making several controllers even if they do share the same logic. The idea is to build your application in a modular way so that it is manageable and extendable/scaleable. If you pack things into a single controller you are removing modularity and before you know it you'll start coding exceptions in your 'common' controller and you're right back where you started before using an MVC framework.

Having said that, you probably don't want to write a controller for every 'page' in your site. Keep in mind that MVC is a great approach for writing web 'applications', not necessarily web 'pages' or even 'sites' in the traditional sense. So how can you combine the two? There numerous approaches - but if you're just starting, this would help compact things a bit:

Instead of writing a controller with index method for each static page, write one controller with a method for each page. E.g.

Code:
class StaticPage extends Controller {

     function index() {
        // this is the home page
    }

   function contact () {
       // contact page
   }

   function projects () {
      // projects page
  }
}

To map your URLs to this controller, modify your router config:

Code:
// routes.php
$route['default_controller'] = "StaticPage";

$route['(contact|projects)(.*)']='staticpage$1';


There you have it. All in one controller separated by method. You could further optimize this by creating a private method to render that static page and taking that out of your methods:

Code:
// static page controller

function _render($top_data, $content_data, $bottom_data) {
        $this->load->view('top',$top_data);
        $this->load->view('content', $content_data));
        $this->load->view('bottom', $bottom_data);
}


HTH,
-m
#4

[eluser]RpR[/eluser]
I agree but projects will have some subs so I like the url to be

projects/categorie/

but I am thinking how I can get a third after it.
#5

[eluser]erik.brannstrom[/eluser]
The only thing I can see that you might want to get rid of the repetition is the view loading. Personally, I use a template view, which looks like this:
Code:
** views/template.php **
<?php $this->load->view('templates/header'); ?>
<?php $this->load->view($partial); ?>
<?php $this->load->view('templates/footer'); ?>

** in your controller **
$data['partial'] = 'myview';
$this->load->view('template', $data);

$data will be accessible to all the views.
#6

[eluser]Mirage[/eluser]
[quote author="RpR" date="1218918751"]I agree but projects will have some subs so I like the url to be

projects/categorie/

but I am thinking how I can get a third after it.[/quote]

Ok, in that case -

If the subs are also static, you can enhance your StaticPage controller to either

- have a _remap() method. In that method you examine URI and load your views accordingly.

or

- accept the subs in the 'projects' method as uri variables and load your views accordingly


If the subs are dynamic (e.g.. need to load stuff from the database) you have more of an application scenario and I would make 'projects' a separate controller, with the first sub being the method. In there evaluate the uri for variable and dispatch your models and views accordingly.

The two approaches can co-exist. simply leverage the power of the router to decide what's static and what isn't.

Cheers,
-m
#7

[eluser]erik.brannstrom[/eluser]
Oh.. I think I missed your point, sorry for that! Smile

Anyway, I have used the routing and _remap method for something similiar to this, i.e. the StaticPage approach. It's very functional. You don't even have to point every call to _remap to it's specific method, a tip is to use for example function_exists($value) to see if the call is valid, and than just make a call to $this->$value(); which to me is very sexy.

But this might not be new to you.




Theme © iAndrew 2016 - Forum software by © MyBB