Jump to content

Controllers in MVC


markyoung1984

Recommended Posts

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).

 

<?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();
    }
  }

?>

Link to comment
https://forums.phpfreaks.com/topic/135176-controllers-in-mvc/#findComment-704993
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.