Table of Contents

Creating a new page

Here are the steps to create a new web page :

  1. Create a new View class for your page in /src/Views/ :

Your class must implement ViewInterface.

Your class now has the execute method, this is where you'll put all your php logic and print out your content. If you're doing a standard page you'll want to store your content in the pageContent attribute, then call the parent execute method with

parent::execute($explodedURI);
. This will ensure that your content will appear at the right place, rather than behind the menus.

Here's an example of a simple page :

class ViewMyPage extends AbstractMainView
{
    public function execute($explodedURI) {
        if ($explodedURI[2] === "mypage") {
            $this->pageContent = "You're on my page!";
            return parent::execute($explodedURI);
        }
        $view404 = new View404($this->auth);
        return $view404->execute($explodedURI);
    }
}

Remember that if you have a function that directly echo stuff you can capture its output with ob_start() and ob_get_clean(). For example :

ob_start();
var_dump($someBuggyVariable);
$this->pageContent = ob_get_clean();

If you're making a somewhat complex page you might want to create a Template for it, in order to write down all the html properly. Create a new template in the /template/ folder and require it in your View class. Wrap the require instruction with ob_start() and ob_get_clean() like above if you're extending AbstractMainView. Even thought your template is going to be a php file, avoid putting any logic in it. Php instructions should only be used to echo out the content of variables, keep your logic in your View.

If you're making an api you can just echo your content out, since it's not going to be embedded in any other templates.

You also can (and probably want to) use the Auth class provided to ensure that only logged users can access the page :

$this->auth->authenticate()

if you want your page to be accessible to unrecognised users but still want to hide certain part accordingly, you can do the following :

$isLogged = $this->auth->checkAuthentication()
This will return you a bool telling you if the user is logged in or not, you can then incorporate it in your View logic.
if ($isLogged) {
    ob_start();
    require ROOT_PATH . '/templates/MyPageTemplate.php';
    $this->pageContent = ob_get_clean();
} else {
    $this->pageContent = "seems like you're lost";
}


Now that your View is done, you need to bind it to an URL.

open arrayOfPages.php in /src/ and add in your View, create a new entry in the associative array : The key must be the last element of the URL and the value must be your View class.

example, to bind your page to http://digiclear/mypage, add the line :

'mypage' => ViewMyPage::class,

If you want to simulate a folder structure, like http://digiclear/myfolder/mypage, create a nested array for each folder, like so :

'myfolder' => array(
    'mypage' => ViewMyPage::class,
    'myotherpage' => ViewMyOtherPage::class
),
You can also create a view for myfolder and handle the displayed content yourself according to what the rest of the URL is, since the $explodedURI argument passed to your execute method is an array containing the exploded URL.
'myfolder' => ViewMyFolder::class,
class ViewMyFolder extends AbstractView
{
    public function execute($explodedURI) {
        if ($explodedURI[2] === "mypage") {
            $myPage = new ViewMyPage($this->auth);
            return $myPage->execute($explodedURI);
        }
        $view404 = new View404($this->auth);
        return $view404->execute($explodedURI);
    }
}