1

Hello i have a few questions about mvc pattern and frameworks in general. I know mvc stands for model - view -controller and that models are fat and controllers are skinny but i'm not quite sure about few details .. on the view part. let's say for example i have this model

<?php
class Menu_Model extends Models
{
    public function listMenuItems()
    {
       return $this->query('some_select');
    }
}

controller

<?php
class Menu_Controller extends Controllers
{
   public function index()
   {
        $this->load('menu', 'Menu_Model');
        $this->view->assign('menuItems', $menu->listMenuItems());
        $this->view->add('menu.php');
   }
}

view

<div class="menu">
<li>{echo_some_data_from_controller}</li>
</div>

The above code let's say is for a simple menu fast wrote now .. as an example. by the mvc ideea it needs to have a model a view and a controller good but then how do i implement this menu in each of the views i have? let's pretend that: the head.php file where i keep the import css starting of the html with basic stuff and the header of the website to get data from mysql for the menu i would need to call the model but the model is called in the controller and each page got it's own controller so from what i understand so far for each controller method i would need to call for a certain model menu, login form etc... to output on each page i need to get data or how do i do it ?.

Bogdan
  • 693
  • 7
  • 26

1 Answers1

3

The responsibility of a Controller is to handle User Input. If your menu doesn't require any user input, put the code into a View Helper and then fetch the Model data from there. Then call that View Helper from the View. In other words: you dont need a controller then.

An alternative would be to provide some sort of mechanism that allows you to register common functionality on each call to a Controller, e.g. something like Zend Frameworks's pre- and post-dispatch hooks. You could then write plugins for these hooks to load and inject certain Model data on each request.

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • so on basics what does not require a controller i only write a model / plugin .. that i call it directly in the views without writing some sort of controller for them? Check out this code here / questions [Link](http://stackoverflow.com/questions/10703064/tinymvc-model-plugin-how-to-implement) – Bogdan May 23 '12 at 06:37
  • @Bogdan Sorry, I dont understand what you just asked. Can you rephrase it please? – Gordon May 23 '12 at 06:39
  • for each model that does not require user input like a form only output data in the view i do not need a controller, i can just write it as a plugin / module / helper and call it in the view no ? – Bogdan May 23 '12 at 06:50