Author Topic: Controllers in MVC  (Read 877 times)

0 Members and 1 Guest are viewing this topic.

Offline markyoung1984Topic starter

  • Irregular
  • Posts: 34
    • View Profile
Controllers in MVC
« on: December 02, 2008, 11:06:50 AM »
In an MVC environment, what should a controller be like?  Should it be a long series of switch statements?  Should it be an object or some description?  I just don't know and its confusing me.

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Controllers in MVC
« Reply #1 on: December 02, 2008, 11:09:05 AM »
A basic front controller could simply be a series of conditions within a switch. A more robust front controllershould attempt to resolv urls to objects / methods.

Offline markyoung1984Topic starter

  • Irregular
  • Posts: 34
    • View Profile
Re: Controllers in MVC
« Reply #2 on: December 03, 2008, 04:02:01 AM »
I do use objects in my code.  I am relating GET variables to objects but within a switch statement.  Is that how you would do it?

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: Controllers in MVC
« Reply #3 on: December 03, 2008, 10:24:54 AM »
I do use objects in my code.  I am relating GET variables to objects but within a switch statement.  Is that how you would do it?

No. A switch statement would meen its hard coded. Everytime you add a new action controller you would need to update the switch.

A simple example of doing it dynamically (Not tested, just a demo).

Code: [Select]
<?php

  $controller 
$_GET['c'];
  
$action $GET['a'];

  if (
file_exists("controllers/$controller.class.php")) {
    include 
"controllers/$controller.class.php";
    
$obj = new $controller;
    if (
is_callable(array($obj$action))) {
      
$obj->$action();
    }
  }

?>