Please any one explain to me basicly about Controller&Model&Hooks in MVC sytem ...?

hello ,

sorry for my question … i searched and i found alot of answers but i puzzled . i couldn’t understand cleary what’s job of each one of it ,

so plz any one help me and explain it to me and help me to understand it :slight_smile:


Hi, just quickly:

Each request to your website begins with the browser sending a url to your server. The server (typically Apache) then finds the file that was requested (usually a .html or .php file) and if it is a scripting language, parses the file through the language processor (PHP in the case of this example).

If the file that is requested contains programming code (ie contains PHP code), then the file is considered a controller - that is that the file is expected to handle the LOGIC for the request: get data from db, choose which template/s are to be used, put data into the db, set session cookies etc etc. The controller therefore has the role of ‘controlling’ what happens in response to the url your browser requested.

Thus, everyone who learns to write PHP first learns to write controller with embedded html.

The next step is to learn OOP (object oriented programming), which then demands that class files are organised and stored in a separate folder, usually outside the server’s public root. Class files can be further categorised into “library” and “model” classifications.

Once you have OOP under your belt, you’ll have come up to the concept of “templating”, which is generally to have a re-usable “frame” or “layout” template with placeholders where dynamic content will be inserted. Your templates are then organised generally consistently with the controller path name, but may be more or less dynamic than that depending on requirements. It’s also possible to make copies of the entire template directory for each language being supported, however using a language class and array/db definitions for language content is preferred.

So now once you have all these ideas in practice, what you would end up with is a folder structure like this:


And the code within the controller will look a bit like this:


$TEMPLATE_OBJECT->setVariable('page_title', 'For the title tags');
$TEMPLATE_OBJECT->setTemplate('main_content', 'content1.tpl');


Noting that init.php creates all default objects (db, template, etc) and render parses the templates into rendered html for output in response to the browser.

Finally, most people realise the redundancy of init and render in each controller, so they “bootstrap” the application with an .htaccess “rewrite rule” which sends each request to index.php. The upside of this is that there is less duplicate code in all the files and you can literally call any controller you want regardless of whether it is consistent with the url path. The downside is a few more cpu cycles, however that is usually quite negligible and bootstrapping is the typical recommended practice.

Now, hooks are quite a different beast altogether and probably not what you should get into, but basically what they are used for is to customise what happens inside a class file or controller file without need for hacking the code. This has been most useful for me at one time which I’ll use as an example:

I wrote a user management class: login/registration/profile data etc which had a backend administration.

Later I wrote a subscription module which added data to the user profile but within it’s own tables.

The problem I had was how to display the subscriptions that the user had purchased when the administrator looks at the profile in the backend administration. Either I had to hack the user module to look up data from this table which belonged to the subscription module, or I could place a ‘hook’ there which would basically pass an array to all the other modules and give each module the opportunity to add their own data to the array before sending it to the template for rendering.

Hope this helps to get your head around it a bit :smiley:

i think term hooks is not native to MVC…may be wordpress is using it…

Agreed, hooks are always implemented by an application requiring them, in the strictest form of MVC, hacking (changing core files) is the normal solution to a problem. Hooks facilitate easy flexibility for applications supporting modular design patterns where modules can be installed and transferred between standard installations to provide customized functionality without impacting on the core files or existing module files.

The first time I came across the term hooks was reading something from a Drupal developer, so maybe there will be some more clues there (incidentally, this was a discussion which led to me discovering the term AOP ).

(Crikey, found it )

The second when I was investigating the Template Method pattern (not to be confused with Templates themselves).

I internalise a hook as being a method in a class which leaves you enough wiggle room to extend its’ abilities when you extend or build upon it.

To me it means I have to have foreseen the need for the hook when you created the class in the first place though.

Hmmm, I could be confusing the term “hook” with something quite different, sometimes called an “event” and sometimes called a “hook”… in which case disregard what I have said. Here’s an example of what I was explaining when I was describing how my “hook” solutions works:



    [...checking form fields...]

    	// allow other modules to check their own form fields
	$hookParams = array(
		'error' => false,
		'errors' => array()
	JFX::event('iusers:register_check', $hookParams);
		$error = true;
			foreach($hookParams['errors'] as $errorMsg){

        // safe to process
        [...insert to db...]
        // other modules can do register processing too
        $uData['password'] = $password;
        $uData['email'] = $email;
        $uData['id'] = $userid;
        $hookParams = array('user_data' => $uData);
        JFX::event('iusers:register_process', $hookParams);

    }// end if no error
}// end if submitted

// gonna define the form fields as an array because other modules can add their fields to it
$formFields = array(
        'name' => 'username',
        'value' => post('username'),
        'type' => 'text',
        'label' => $this->Lang('username'),
        'required' => true
        'name' => 'email',
        'value' => post('email'),
        'type' => 'text',
        'label' => $this->Lang('email'),
        'required' => true
    [...other fields defined...]

// implement the hook to get extra form fields from other modules
$hookParams = array('form_fields' => $formFields);
JFX::event('iusers:register_form', $hookParams);
$formFields = $hookParams['form_fields'];

// render the form
$form = JFX::makeRapidForm($formFields, $this->Lang('register_legend'), 'IUsers_Register');
echo $form->render();

There you can see in my controller (which is very low-level, no models used), I am making a form and checking it when submitted and processing it. These three steps facilitate other modules to add their 2c worth by using what I have described as “hooks”, which others have called “events”. I’m feeling a bit confused again to be honest, however this is what works for me and seems similar to the way Drupal uses hooks, however quite different at the same time!

Does anyone know the correct terminology for this technique so I can get my story straight? Cheers!