This website uses Cookies to provide you with the best possible service. Please see our Privacy Policy for more information. Click the check box below to accept cookies. Then confirm with a click on "Save".  
Status: 2023-12-29

MVC [Model View Controller]

The so called MVC Pattern is commonly named in this order, but the order of the commands is different, with C (Controller) in first place.


Controller

Route => Controller

"The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model."
wikipedia, "Model–view–controller#Interactions", 2023-12-28

Accepts input and converts it to commands for the model or view. Here is where Business Logic is placed.

Rerquirements

assuming we have a Route, accepting GET Requests leading to module's Foo Controller Index with method index

\MVC\Route::get('/', '\Foo\Controller\Index::index'); // expecting GET Requests

writing the Controller class

  • The Controller Class has to be placed inside your module's Controller folder (see /modules/{moduleName}/
  • Naming Convention for the php file: Pascal Case (see wiki.c2.com/?PascalCase)
  • The Controller must have implement the interface \MVC\MVCInterface\Controller; therefore simply extend App\Controller: class Index extends App\Controller { } as it fullfills the required interface.

Now in the method index you can place your business logic.

Illustration: Module Foo, Controller Index with method index, responsible for incoming requests via the corresponding Route

<?php

namespace Foo\Controller;
use App\Controller;
use MVC\DataType\DTRequestCurrent;
use MVC\DataType\DTRoute;

class Index extends Controller
{    
    /**
     * @param \MVC\DataType\DTRequestCurrent $oDTRequestCurrent
     * @param \MVC\DataType\DTRoute          $oDTRoute
     * @throws \ReflectionException
     */
    public function __construct(DTRequestCurrent $oDTRequestCurrent, DTRoute $oDTRoute)
    {
        parent::__construct($oDTRequestCurrent, $oDTRoute);
    }    

    /**
     * @param \MVC\DataType\DTRequestCurrent $oDTRequestCurrent
     * @param \MVC\DataType\DTRoute          $oDTRoute
     * @return void
     * @throws \ReflectionException
     */
    public function index(DTRequestCurrent $oDTRequestCurrent, DTRoute $oDTRoute)
    {
        ;    
    }
}

Method Parameter

There are two DataType objects as parameters sending to a Controller method by default:

Example Usage of parameters

public function index(DTRequestCurrent $oDTRequestCurrent, DTRoute $oDTRoute)
{
    // get potential data sent
    $mInput = $oDTRequestCurrent->get_input();

    // get additional object
    /** @var \MVC\DataType\DTRoutingAdditional $oDTRoutingAdditional */
    $oDTRoutingAdditional = $oDTRoute->get_additional();

    // get title 
    $sTitle = $oDTRoutingAdditional->get_sTitle();        
}

Model

Controller => Model => Controller

The "Worker". Here you are receiving commands from Controller and do things with data. Try to avoid implementing business logic here - this always belongs to a Controller.

A Model Class has to be placed inside your module's Model folder (see /modules/{moduleName}/


View

Controller => View

The view renders presentation in a particular format, receiving commands from Controller.

A View Class has to be placed inside your module's View folder (see /modules/{moduleName}/


Examples

here you find a complete Controller class extending a _Master Controller.

Example Controller

Example Controller /modules/Foo/Controller/Index.php

<?php
/**
 * Index.php
 *
 * @package Emvicy
 * @copyright ueffing.net
 * @author Guido K.B.W. Üffing <emvicy@ueffing.net>
 * @license GNU GENERAL PUBLIC LICENSE Version 3. See application/doc/COPYING
 */

/**
 * @name $FooController
 */
namespace Foo\Controller;

use MVC\DataType\DTRequestCurrent;
use MVC\DataType\DTRoute;

/**
 * @extends \Foo\Controller\_Master
 */
class Index extends _Master
{
    /**
     * @return void
     * @throws \ReflectionException
     */
    public static function __preconstruct()
    {
        parent::__preconstruct();
    }

    /**
     * @param \MVC\DataType\DTRequestCurrent $oDTRequestCurrent
     * @param \MVC\DataType\DTRoute          $oDTRoute
     * @throws \ReflectionException
     */
    public function __construct(DTRequestCurrent $oDTRequestCurrent, DTRoute $oDTRoute)
    {
        parent::__construct($oDTRequestCurrent, $oDTRoute);
    }

    /**
     * @param \MVC\DataType\DTRequestCurrent $oDTRequestCurrent
     * @param \MVC\DataType\DTRoute          $oDTRoute
     * @return void
     * @throws \ReflectionException
     */
    public function index(DTRequestCurrent $oDTRequestCurrent, DTRoute $oDTRoute)
    {
        view()->autoAssign();
    }

    /**
     * @param \MVC\DataType\DTRequestCurrent $oDTRequestCurrent
     * @param \MVC\DataType\DTRoute          $oDTRoute
     * @return void
     * @throws \ReflectionException
     */
    public function notFound(DTRequestCurrent $oDTRequestCurrent, DTRoute $oDTRoute)
    {
        http_response_code(404);
        view()->autoAssign();
    }

    /**
     * @throws \ReflectionException
     * @throws \SmartyException
     */
    public function __destruct ()
    {
        view()->render();
    }
}

Example Master Controller

Example Master Controller /modules/Foo/Controller/_Master

<?php
/**
 * _Master.php
 *
 * @package Emvicy
 * @copyright ueffing.net
 * @author Guido K.B.W. Üffing <emvicy@ueffing.net>
 * @license GNU GENERAL PUBLIC LICENSE Version 3. See application/doc/COPYING
 */

/**
 * @name $FooController
 */
namespace Foo\Controller;

use App\Controller;
use Foo\Model\DB;
use MVC\DataType\DTRequestCurrent;
use MVC\DataType\DTRoute;
use MVC\MVCTrait\TraitDataType;

/**
 * @extends Controller
 */
class _Master extends Controller
{
    use TraitDataType;

    /**
     * @return void
     * @throws \ReflectionException
     */
    public static function __preconstruct()
    {
        parent::__preconstruct();
    }

    /**
     * @param \MVC\DataType\DTRequestCurrent $oDTRequestCurrent
     * @param \MVC\DataType\DTRoute          $oDTRoute
     * @throws \ReflectionException
     */
    public function __construct(DTRequestCurrent $oDTRequestCurrent, DTRoute $oDTRoute)
    {
        parent::__construct($oDTRequestCurrent, $oDTRoute);

        // View
        if (true === $this->isPrimary())
        {
            function view() {return \Foo\View\Index::init();}
        }
        else
        {
            \Foo\View\Index::init();
        }

        // Init Database
        DB::init();
    }

    public function __destruct()
    {
        ;
    }
}