Skip to content

Adding New Pages

Jack Grzechowiak edited this page Oct 18, 2017 · 9 revisions

Creating Pages

Important Information

Please note that all styling on the page uses Bootstrap 4. If you are currently using Bootstrap 3, use this migration guide to convert your HTML to Bootstrap 4.

Naming Convention

Type Convention Example
Directory Lowercase, Hyphenated agency-requests/
Pages Lowercase, Underscored curricula_view.php
Classes Upper Camel Case ViewLoader.php

Comment Header

/**
 * PEP Capping 2017 Algozzine's Class
 *
 * [short description]
 *
 * [long description]
 *
 * @author [your name]
 * @copyright 2017 Marist College
 * @version [version number]
 * @since [initial version number]
 */

HTML/PHP Page Content

Each page should follow the format below and placed in the view/ directory. If there are multiple pages you are creating that should logically be grouped together, add them into a sub-folder within the view/ directory.

<?php include('header.php'); ?>

    <!-- Put Page Content Here -->

<?php include('footer.php'); ?>

Routes

Each page has a specific route assigned to it. This is located in the routes.php file. To add a new route add a line to the file like the following:

$router->add('/new-page', 'new_page.php', 'New Page Title');
#                [1]           [2]              [3]

[1] - The URI to use that redirects to the given page

[2] - The path to the PHP file (the base directory is views/). So putting new_page.php really is views/new_page.php

[3] - The title displayed when the page is loaded


Route Parameters

A route parameter is anything after the main route in the URL. For example: if my route is /curricula/view/1, the main route is /curricula, the parameters are view and 1.

The Router handles the main route, but you individually handle the parameters.

Add Global Variables

At the top of the PHP file add the following:

global $params, $route, $view;
  • $params - this holds an array of the URL parameters (doesn't contain the '/')
    • If the URL is /curricula/view/1, then $params[0] is "view"
  • $route - the current route that Router determined was to be displayed (what you created in routes.php)
    • $route has attributes "file", "title", "params", and "url"
    • NOTE: $route['params'] is the same as $params
  • $view - the View object created in bootstrap.php

Use $params to alter page logic

The parameters can be useful for switching between pages. For example, the base page for curricula manages all of the other view, create, edit, and archive pages. It does this through the route $params.

if (!empty($params) && $params[0] == 'view') {          # /curricula/view
    $view->display('curricula/curricula_view.php');
}
else if (!empty($params) && $params[0] == 'edit') {     # /curricula/edit
    $view->display('curricula/curricula_modify.php');
}
else if (!empty($params) && $params[0] == 'create') {   # /curricula/create
    $view->display('curricula/curricula_modify.php');
}
else if (!empty($params) && $params[0] == 'archive') {  # /curricula/archive
    $view->display('curricula/curricula_archive.php');
}
else {                                                  # /curricula
    # HTML markup for base page
}

The $view->display('path/to/page.php'); function displays the PHP file at the given path. Just like the routes, this path is based from the views/ directory. So, page.php is really views/page.php.

You do not have to use the $view->display() function and can just put the HTML in the if-statements. This just makes the file look cleaner and easier to read.


Page Authorization

To require a page to have authorization for specific users, you'll have to update the page_permissions.ini file. This file contains all of the routes and their respective permissions.

The format of each entry is /route = (ALLOW | DENY) (permission-list | * | auth).

Examples

These examples show the different uses with ALLOW and DENY part.

Example Comment
/example = ALLOW * Allows all connections
/example = ALLOW auth Allows authenticated connections (logged in)
/example = ALLOW Administrator Superuser Allows both the Administrator and Superuser roles
/example = DENY * Denies all connections
/example = DENY Facilitator Denies the Facilitator role

These example show the different uses in the route-part of the permission.

Example Comment
/example = ALLOW * Match /example exactly
/example/* = ALLOW * Match /example/../, exactly 2 parts to URL
/example/** = ALLOW * Match /example/.., match first part, any other part doesn't matter

Check for Role

If you want to only display a chunk of HTML if the user has a specific role, use the hasRole() function. This example will only allow User and Admin:

<?php
if (hasRole(Role::User | Role::Admin)) {
    echo '<p>Has proper roles!</p>';
}
?>