PHP code example of midgard / midgardmvc-core

1. Go to this page and download the library: Download midgard/midgardmvc-core library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

midgard / midgardmvc-core example snippets


    class net_example_calendar_controllers_date
    {
        public function __construct(midgardmvc_core_request $request)
        {
            $this->request = $request;
        }

        public function get_date(array $args)
        {
            $this->data['date'] = strftime('%x %X');
        }
    }

Showtime
--------

Once a controller has been run, the next phase in MVC execution is templating. There are two levels of templates used:

* Template entry point: the "whole page" template, which w-date`. When MVC gets into the templating stage. This is defined in the route:

    show_date:
        - path: '/date'
        - controller: net_example_calendar_date
        - action: date
        - template_aliases:
            - content: show-date

When the templating phase of the route happens, MVC will look for such element from the template stack. Template stack is a list of components running with the current request. First MVC looks for the element in the current component, and if it can't be found there it goes looking for it down the stack:

* Current running component `templates` directory
* `templates` directories of any components injected to the template stack
* Midgard MVC core `templates` directory

The first matching template element will be used and executed via TAL. The data returned by the component will be exposed into TAL as a `current_component` variable. In case of our date example the template could simply be a `net_example_calendar/templates/show-date.xhtml` file with following contents:

    <p>Current date is <span tal:content="current_component/date">5/8/1999 01:00</span></p>

Request isolation and making of sub-requests
--------------------------------------------

Midgard MVC supports handling multiple requests within same web page. For example, the main content of your page can be served from a request, and then a news listing in a sidebar can be handled from a sub-request.

Since this means that potentially multiple routes, controllers and templates will be run within the same PHP execution, every request must be isolated within the PHP variable scope. To accomplish this, the principle is that all request-specific information is stored within the Request object that gets passed around between the front controller, dispatcher and actual controllers, and all of them are actually stateless. For example, the `dispatch` method of a dispatcher, or the `template` method of the front controller may be run multiple times within same PHP execution.

Within any stage of Midgard MVC execution you can make a sub-request in the following way:

    
    // Set up intent, for example a hierarchy node, node URL or component name
    $intent = '/myfolder/date';
    // Get a Request object based on the intent
    $request = midgardmvc_core_request::get_for_intent($intent);
    // Process the Request
    midgardmvc_core::get_instance()->dispatcher->dispatch($request);
    // Use the resulting data
    $component_data = $request->get_data_item('current_component');
    echo $component_data['date'];