MVC - mediahack/mightymouse GitHub Wiki

This document assumes you are familiar with MVC architectures. More info can be found all over the web. Here's a link to the wikipedia definition.

MightyMouse's MVC model is simple and straight forward. The application root is the app folder in your MM source tree.

app
  |_config
  |_controller
  |_core
  |_layout
  |_model
  |_view
    |_controllerName
       |_actionName.php
  |_bootstrap.php
vendor
public
  |_.htaccess
  |_index.php

General Rule

To ensure everything works make sure all your files are lower-cased to accomodate case-sensitive file systems; i.e. *nix based OSes.

Controllers

Here are the basic details for creating controllers.

Namespaces

Define the namespace: namespace App\Controller

Extending

Controllers must extend the App\Core\Controller class.

###Constructors By default controllers don't implement a constructor. They typically don't need one because the parent runs all the Controller logic. It's the Actions we're calling not making instances of the constructor. However, in certain instances you MAY want to have a constructor fire and IF you do, you must have the __constructor call the parent::__construct() or the controller will not work properly.

Actions

Controllers actions that are meant to be accessed by the router should named with the prefix Action. For example: MyController::exampleAction can be resolved in the URL http://mydomain.tld/mycontroller/

Configuration Files

Each controller has access to the main configuration file referenced in the bootstrap.php file. Use the $this->config property to use it.

Parameters

Parameters can be passed to the controllers either via the GET string. For example: http://mydomain.tld/mycontroller/example?thing=123&my=woot. They can also be passed via URL using slashes http://domain.tld/mycontroller/example/124/woot.

Accessing variables from your controllers you can either use the $_POST/$_GET or if you used URL slash building you have access to the $this->params array property from your controller. The $this->params array is populated from all the slashed URL data beyond the controller/action. So in our example above $this->params[0] is 124 and $this->params[1] is woot.

Models

All your models should be stored in the app/models folder.

Views

Views are the content to be rendered to the CONTENT section of a layout. You do not need to define all the base HTML for your page, just the content specific HTML.

Views are stored in the app/views directory. However, they aren't just dropped into that directory. You should create a sub-folder to match your controller name. Then within that folder you can make a PHP file for each action within that controller that will be web accessible.

To follow our example, we would create the following directory structure and files: app/views/mycontroller/example.php