Welcome Guest, Not a member yet? Register   Sign In
Dynamic/Static pages controller
#1

[eluser]FrankStar[/eluser]
Hey guys! First of all, yes I'm a noob so let's get this out of the way Smile

This is my first experience with CI and an MVC structure. I'm trying to figure out when a new controller is needed. Right now, my website has the following sitemap:

- Home (dynamic content)
- Static 1
- Static 2
- Search Result (dynamic content)

Should I create a controller that will handle the dynamic pages and a different one for the statics? If this is the way to go, I guess I'll need to add some routes?

Forgive me if I'm wrong or mixing things up!
#2

[eluser]Rok Biderman[/eluser]
First of all, welcome. As to your question, there is no reason for static/dynamic separation in your controllers. It's basically a question of architecture, feel free to stick it all into a single controller if you're comfortable with it. There is a thumb rule about keeping controllers lean, but it has more to do with not keeping inside the controllers what could go into models. Your proposed structure seems just as appropriate as any.

You only change routes if you're not happy with the way your urls look.
#3

[eluser]FrankStar[/eluser]
Thanks a lot for the feedback!
#4

[eluser]CroNiX[/eluser]
Really, the only difference between a static and dynamic view is a dynamic view gets variables passed to it. You could easily have a single function that serves both types by passing an array to it. In the case of a static page, it would just be an empty array (or have an empty array be the default value if nothing is passed to it.
A very basic sample:
Code:
function show_page($view_file = '', $variables = array())
{
  if ($view_file != '')
  {
    $this->load->view($view_file, $variables);
  }
}
To output a static view:
Code:
show_page('my_static_file');

To output a dynamic view:
Code:
$variables = array(
  'page_title'  => 'My Page Title',
  'h1_tag'      => 'My H1 Tag, yo'
);
show_page('my_dynamic_view', $variables);




Theme © iAndrew 2016 - Forum software by © MyBB