In your database, create a table named
configurations with columns
id (primary key),
name (string), and
value (string). Create this table with a migration.
Insert a record into this table with
name set to “maintenance-mode” and
value set to 0. You can do this a few different ways:
- Interact with your SQLite database directly
- Interact with your SQLite database with Tinker. Currently there is a bug with PHP 7.3 (maybe 7.2 too) where Tinker doesn’t work. A workaround is to set
php.ini. If you installed PHP on a Mac via https://php-osx.liip.ch/, the
php.iniis located in
- Create a dummy route where you insert the record, similar to what we were doing with
/eloquentwhen we learning Eloquent.
You are going to create middleware that redirects users to a maintenance mode page (that you design yourself) at the URL path
/maintenance if there is a record in the
configurations table with a
name of “maintenance-mode” and its
value is 1. If that record’s
value is 0, users should not be redirected to that page. Name the middleware class
MaintenanceMode. This middleware should only apply to unauthenticated routes, except for the following so that users can login and logout even if the site is in maintenance mode. To create middleware, use
php artisan make:middleware MaintenanceMode.
Route::get('/login', 'LoginController@index'); Route::post('/login', 'LoginController@login'); Route::get('/logout', 'LoginController@logout');
Create a settings page at
/admin/settings. This page will have a form with one checkbox that will toggle the
configurations record with a
Make sure that there is a registered account with an email of
firstname.lastname@example.org and a password of
Next, create a migration that adds a
role column to the
email@example.com user, set the
Next, create an
admin middleware that is attached to all admin routes. Only authenticated users with a role of admin should be allowed to visit the
/admin/settings page. Otherwise, redirect to Laravel’s 404 page using
Lastly, if a user is authenticated and is an admin, show a link to the Settings page in the navigation.
Make a commit with the commit message “Assignment 5 - Laravel 4 completed”. Push your code up to GitHub and verify that it was deployed to Heroku.