For this project, we will be using the Whoops library to handle errors. The Whoops library is a tool for examining errors that may occur in your projects. This library is packaged and made available for other developers to use in their projects.
Using Whoops, when an error occurs in PHP, you will be able to see this display information as opposed to standard bland error reporting from the server:
Composer will manage the use of this dependency as it is considered among PHP developers a very widely used and a very popular package manager.
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. To install Composer, go to https://getcomposer.org/download/.
Imagine a scenario where you have to install a dependency for PHP, and for installing that dependency, you need to install other overhead dependencies. Composer helps you in handling this problem. It is used to handle all the work for you to install a library, as it downloads all the libraries and dependencies together.
We'll look at setting up Composer in this section. To do this, follow these steps:
app
holds the application filessystem
holds the core framework fileswebroot
will hold the publicly accessible filescomposer.json.
This file holds a JSON object that will autoload classes as we need them. We will be using PSR-4 autoloading.
composer.json
and create an App
and System
definition.App
or System
to look for the class in the app
or system
folders.Whoops.
We load this package by including it as a dependency in a require
block:{ "autoload": { "psr-4": { "App\" : "app/", "System\" : "system/" } }, "require": { "filp/whoops": "^2.1" } }
composer.json.
Now, inside webroot,
create two files: index.php
and .htaccess.
.htaccess
.index
file, we don't want its contents displayed in a browser. To disable directory browsing, enter:Options –Indexes
<IfModule mod_rewrite.c> //more code </IfModule>
RewriteEngine On RewriteBase /
#
below, but only do this on a server that has HTTP enabled.This is to ignore trailing slashes and folder and files that exist. Only dynamic files should be routed, for example, URLs that do not exist as physical files.
The last rule passes all requests to index.php?$1.
The $1
is the request after the first /
in the requested URL.
RewriteCond
basically means "execute the next RewriteRule
only if this is true".
The RewriteRule
basically means that if the request is done that matches ^(.+)$
(matches any URL except the server root), it will be rewritten as index.php?$1,
which means a request for contact will be rewritten as index.php?contact:
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
QSA means that this flag forces the rewriting engine to append a query string part in the substitution string to the existing one instead of replacing it.
The Secure Sockets Layer (SSL) creates an encrypted connection between your web server and your web browser. This stops any data being intercepted from your machine to the web server. It's recommended to use HTTPS.
The complete file should look like this:
# Disable directory snooping Options -Indexes <IfModule mod_rewrite.c> # Uncomment the rule below to force HTTPS (SSL) ……….. RewriteRule ^(.*)$ index.php?$1 [QSA,L] </IfModule>
index.php.
vendor/autoload.php
exists (it won't exist yet) and require the file.else
clause:if(file_exists('../vendor/autoload.php')){ require '../vendor/autoload.php'; } else { echo "<h1>Please install via composer.json</h1>"; echo "<p>Install Composer instructions: <a href='https://getcomposer.org/doc/00-intro.md#globally'>https://getcomposer.org/doc/00-intro.md#globally</a></p>"; echo "<p>Once composer is installed navigate to the working directory in your terminal/command prompt and enter 'composer install'</p>"; exit; }
ENVIRONMENT
and give it a value of development. When going into production,
set the environment
to production
.environment
constant, we can set the appropriate level of error reporting:if (defined('ENVIRONMENT')){ switch (ENVIRONMENT){ case 'development': error_reporting(E_ALL); break; case 'production': error_reporting(0); break; default: exit('The application environment is not set correctly.'); } }
The complete file looks like this:
<?php if(file_exists('../vendor/autoload.php')){ require '../vendor/autoload.php'; } else { …… error_reporting(0); break; default: exit('The application environment is not set correctly.'); } }
This means Composer is working, but we haven't yet requested anything to be loaded.
Errors in view when the Whoops package is turned on will display the errors on the screen with a full stack trace of how the framework has executed the code along the way. This can help developers isolate the issue by following the path that their code has traveled on.