0

I'm in the early stages on learning the Model, View, Controller framework with PHP. I'm getting the hang of it thanks to a tutorial found here. From my understanding, the controller will invoke the model so that the appropriate code can be ran in order for the viewable content to be rendered correctly.

The problem is that based upon that particular tutorial, it seems that I'm going to have to create an entirely new file for each page just to invoke the model in order to render the data viewable.

For example, in my controller.php file I have:

require_once("model.php");  

class Controller 
{  
    public $file;
    public $league;

    public function __construct($file, $league)  
    {
        $this->file = $file;  
        $this->league = $league;
    }   


    public function invoke()  
    {
        // Everything that needs to be done in PHP will be done here so that once 
        // whatever file is included, it will just fit in fine and echo out perfectly
        require_once($this->file.".php"); 
    }
}

In my model.php file, I have:

class Model 
{
    private $file;
    private $league;

    public function __construct($file, $league)
    {
        $this->file = $file;
        $this->league = $league;
    }

    More code. You get the idea...
}

In my teams.php file, I have:

$controller = new Controller("teams", "nba");  
$controller->invoke();

But, since the teams.php has to invoke the the model and will ultimately require_once("teams.php"), I have to create an entirely new file just for the view. So, now the display will be featured on teams_display.php. Is there anyway that mod rewrite can help facilitate a framework whereby all of the pages happen in the index.php file while giving users the illusion that they're not?

Sven
  • 69,403
  • 10
  • 107
  • 109
Lance
  • 4,736
  • 16
  • 53
  • 90
  • Where is your front controller. This part usually puts together the controller handling the request, and the template that does the rendering. The controller invokes the model and passes the data from it into the template. Having one controller handling more than one URL is a means of doing the proper routing from URL to controller. – Sven Jan 23 '13 at 08:54
  • Ohhh. A front controller is news to me. I'm a beginner when it comes to MVC. I've been using procedural programming for everything up until a few days ago. Can you show me a exampe of a front controller and how it works? – Lance Jan 23 '13 at 08:57
  • I'd suggest you do not try to create your own MVC framework, but grab one of the existing frameworks and explore it. There are plenty of tutorials for them, too, and the code quality is way better than that of any "build your own MVC framework" tutorial. At least they can act as a demonstration of how things work. – Sven Jan 23 '13 at 08:59
  • So, something like CakePHP or CodeIgniter? – Lance Jan 23 '13 at 09:05
  • 1
    I was thinking about Symfony 2.1 or Zend Framework 2. – Sven Jan 23 '13 at 09:17
  • I would agree with @Sven. Sf2 and ZF2 are the least broken of php frameworks. While both still have issues, at least they are not filled with bad practices. – tereško Jan 23 '13 at 09:43

2 Answers2

2

MVC is not a framework but a design pattern. This tutorial will help you understand the MVC pattern but if you want to use a real framework you can take a look at Zend Framework.

In your case index.php is the entry point, mod rewrite redirect all the requests to index.php who is redirecting to the appropriate controller according to the user request. For example, http://localhost/teams/nba, 'teams' is the controller, 'nba' is the action.

You have to create a TeamController and in the controller you have to create a 'nba' action. The model is dealing with the DB layer so create a Team model with getNbaList method to retrieve all the NBA teams. You have to call the model inside your action and then you can feed the view (mainly html) with all the teams you get from the model.

robinef
  • 312
  • 1
  • 7
  • Your example URL need not necessarily be mapped to a teams controller with nba action. It might as well be the teams module with the nba controller and a default action. It might as well be a teams controller with a default action and "nba" as a parameter. That's a very important feature: URLs can be mapped to any combination of request processing. – Sven Jan 23 '13 at 16:44
  • Sure there are plenty of possibilities, it was just a simple example :) – robinef Jan 23 '13 at 21:33
2

As some already have mentioned. MVC is not a framework.

MVC is quite old design pattern, originally defined for smalltalk. Thought in all the years it has somewhat evolved and produced several related patterns, that have been adapted for use in Web.

The core idea in MVC is separation of concerns. This is why MVC and all the MVC-inspired patterns are made of two layers:

  • presentation layer, that deals with interface in all of it's forms
  • model layer, that handles the storage an business logic.

Since these are two different layers, they should not be aware of other's internal structure. Otherwise you end up with leaking abstraction.

The presentation layer

In well implemented MVC structure, the controller only changes the state of model layer (and in rare cases - state of current view instance). Controllers IS NOT responsible for instantiation of model layer or any structures in it.

The view instances in MVC should responsible for producing a response to a user. In some case it might entail only sending an HTTP location header, while in other case - it produces a HTML output, by combining multiple templates. View request data, that it needs, from model layer.

In this context, the "user" is not the human, but the browser, which is communicating with website.

The model layer

This layer is usually made from three major groups of structures:

  • domain objects, that deal with logic of particular business entity
  • storage abstractions (usually - data mappers), that are able to store data from domain objects
  • services, that contain the application logic (interaction between domain object and storage abstractions)

The presentation layer interacts with model layer through services. Controllers send data to them, which change the state of model layer, while view instances request data form them.

If you are interested, there is a bit longer explanation of the model layer available here.

P.S.

Avoid the PHP frameworks. Non of them actually implements MVC. Instead, most of them, are pure Rails clones, which by it self was never intended to be a production-stage framework(explanation here).

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
  • So, if the controller isn't responsible for any instantiation of the model, should my teams.php page not contain: $controller = new Controller("teams", "nba"); $controller->invoke(); – Lance Jan 23 '13 at 09:54
  • Your `invoke()` method is currently performing autoloading. That should be a process, that's completely unrelated to MVC . You should look into use of [this function](http://php.net/manual/en/function.spl-autoload-register.php) instead. – tereško Jan 23 '13 at 11:39