The purpose of the section is to implement the general styling that we have implemented which shows the inclusion of bootstrap and the HTML markup:
An issue that has not yet been addressed in paths. So far, we've been using relative paths for including files such as views in system/View.php.
Let's fix that:
webroot/index.php
and add these lines after line 9:defined('DS') || define('DS', DIRECTORY_SEPARATOR); define('APPDIR', realpath(__DIR__.'/../app/') .DS); define('SYSTEMDIR', realpath(__DIR__.'/../system/') .DS); define('PUBLICDIR', realpath(__DIR__) .DS); define('ROOTDIR', realpath(__DIR__.'/../') .DS);
These are constants that can be called anywhere in the framework. The first line defines a directory separator, for example, /
or a depending on the machine:
APPDIR
– points to the app
folderSYSTEMDIR
– points to the system
folderPUBLICDIR
– points to the webroot
folderROOTDIR
– points to the root
project pathEach one creates an absolute path to its endpoint.
View
class. Open system/View.php,
and on line 24, replace:$filepath = "../app/views/$path.php";
With:
$filepath = APPDIR."views/$path.php";
layouts
inside app/views.
Create the following files inside app/views/layouts
:errors.php
footer.php
header.php
nav.php
errors.php
errors.php
and enter the following code:<?php use AppHelpersSession; if (isset($errors)) { foreach($errors as $error) { echo "<div class='alert alert-danger'>$error</div>"; } } if (Session::get('success')) { echo "<div class='alert alert-success'>".Session::pull('success')."</div>"; }
This includes a Session helper, which we will create shortly.
The first if
statement checks whether $errors
exists, and if so, exit the loop and display an alert. The classes are Bootstrap
classes (we will have this in header.php
).
The next if
statement checks for the existence of a session called success,
and if it exists, displays its contents. This is used to provide feedback to the user.
header.php
and enter the following code:<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title><?=(isset($title) ? $title.' - ' : '');?> Demo</ title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <link rel="stylesheet" href="/css/style.css"> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> </head> <body> <div class="container">
footer.php
and close the container div
and the body
and html
tags:</div> </body> </html>
nav.php
and enter the following code:<nav class="navbar navbar-default"> …… </div><!--/.nav-collapse --> </div><!--/.container-fluid --> </nav>
app/views/404.php
and include the layout files:<?php include(APPDIR.'views/layouts/header.php');?> 404! <?php include(APPDIR.'views/layouts/footer.php');?>
This brings in the header and shows the page content, and ends with the footer included.
Don't include the nav
here. The 404 can be shown even when the user is not logged in.
This makes a very clean way of organizing common layouts into your views so that when you need to change a global element, the layout views are where they were stored.
php –S localhost:8000 –t webroot
view page source
'. You should see the following output:<!doctype html> <html lang="en"> <head> …… 404! </div> </body> </html>
These layouts will become more visible as we go further into this chapter.
In this section, we have covered how to set up file paths correctly. We covered how to set up Bootstrap properly, and we finally set up views for errors and global elements like header, footer, navigation, and errors.
In the next section, we will cover how to add security to our application and setting up a password recovery.