General principles of views

In MV framework views are PHP files located in folder "/views/". These files are responsible for the look of frontend of websites and are included by routing system. Each requested URL will match one of the views, even if the page was not found in the database.

All templates are located in "/views/" folder, also in this folder is allowed to make new subfolders to group views files. There is "Router" class that contains the requested URL and is responsible for routing and including of views. This class stores the initial cleared URL, and also an array of URL parts. This object is described in more details in Object of Router class section.

Sample of working with Router object.

//Get full cleared URL
$url = $mv -> router -> getUrl()

//Get an array of URL parts
$url_parts = $mv -> router -> getUrlParts()

Method of page searching in database by URL parts. Samples of working with URLs like "/event/32/" and "/event/birthday/".

public function defineEventPage(Router $router)
{
    $url_parts = $router -> getUrlParts();
 
    if(count($url_parts) == 2 && $url_parts[0] == "event")
        if(is_numeric($url_parts[1]))
            return $this -> findRecord(array("id" => $url_parts[1], "active" => 1));
        else if(!is_numeric($url_parts[1]))
            return $this -> findRecord(array("url" => $url_parts[1], "active" => 1));
}
 
//Sample of method call 
$event_content = $mv -> events -> defineEventPage($mv -> router);
$mv -> display404($event_content); //Shows 404 if the object is empty

Samples of Routing settings

The example of content of "config/routes.php" file.

$mvFrontendRoutes = array(
 
"index" => "view-index.php",
 
"404" => "view-404.php",
 
"default" => "view-default.php",
 
"contacts/" => "view-contacts.php",
 
"contacts/map/" => "view-location.php",
 
"client/" => "client/view-home.php",
 
"client/messages/" => "client/view-messages.php",
 
"article/special/*/" => "view-special.php",
 
"article/*/" => "view-article.php",
 
"products->" => "view-products.php",
 
"events->" => "company/live/view-events.php"
);

Order of Router's work

  1. Initially, router checks the URL if main website page was requested.
  2. If non-homepage has been requested, then a look up for exact match of URL with one of templates will occur (for example "contacts/", "about/", "register/complete/").
  3. Then if there is no exact match found, a template of "module/extra/*/" and "module/extra->" will be searched for, the first of them contains 3 parts, 2 of which are defined, and the 3rd part can be a variable or all templates starting with "module/extra" and they may have any number of parts.
  4. Then the URL will be validated for match with templates of "module/*/" (2 parts of URL, the first part is defined as "module", for example "products/14/", "articles/982/", "users/john-d12/").
  5. Then another template will be searched for as "module->" (all URLs, the first part of which is equal to "module", for example "products/23/", "catalog/cars/page/2/").
  6. If no one match to template URL is found, then it redirects to "default" template.
  7. If inside "default" template the page was not found, then "404" template will be called (page not found). Also, "404" template can be called in any other template when identification of page on requested URL was failed.

Samples of Router in use

Cases based on above mentioned "config/routes.php" file.

  1. URL: "about/" Template match: "default", included file: "view-default.php", (no match found).
  2. URL: "contacts/map/" Template match: "contacts/map/", included file: "view-location.php".
  3. URL: "contacts/google-map/" Template match: "default", included file: "view-default.php" (no match found).
  4. URL: "client/messages/" Template match: "client/messages/", included file: "client/view-messages.php".
  5. URL: "article/543/" Template match: "article/*/", included file: "view-article.php".
  6. URL: "article/nature/47/" URL: "article/nature/47/" Template match: "default", included file: "view-default.php" (no match found, to redirect to "view-article.php" you should extend the template to "article->").
  7. URL: "events/company/new-year/page/4/" Template match: "events->", included file: "company/live/view-events.php".
  8. URL: "article/special/92/" Template match: "article/special/*/", included file: "view-special.php".

Samples of 404 error call

if(...)
    $mv -> display404(); //Redirects to 404 page in any case
 
$content = $mv -> pages -> findRecord(array("url" => "index"));
$mv -> display404($content); //If $content is empty, then call of 404 
 
$content = $mv -> pages -> findRecord($mv -> pages -> defineUrlType($mv -> router));
$mv -> display404($content);
 
$content = $mv -> pages -> findRecord(array("url" => "events","active" => 1));
$mv -> display404($content);
 
$event_content = $mv -> events -> findRecord($mv -> events -> defineUrlType($mv -> router));
$mv -> display404($event_content);
 
$content = $mv -> products -> defineProductPage($mv -> router);
$mv -> display404($content);