LARAVEL

How to handle logs and reports in your app using Laravel 5 – Part 1


Logging is a very important part of every application.

Introduction

When building an app, it’s always good to keep track of virtually every activity on the application. It gives you an overview of what your users  are doing and what mistakes they make more often. From a security perspective, web application logs can be very useful in:

1. Detecting Attacks

2. Detect application misuse.

3. Detect errors

Laravel takes care of this so easily by taking advantage of the popular PHP Monolog library.

We’ll create a simple web application called ShoutPad and I’ll demonstrate how to handle logging and report in this application. These are the features of the application:

1. Users will be able to create an account.

2. Users will be able to log in after creating an account.

3. Users will be able to give a shoutout.

4. Users will be able to delete their accounts.

Code Time

1. Install a fresh copy of Laravel

2. Fire up your vagrant and setup a database

3. Create a migration file like so:

shout_table migration file

Now, run the migrations

4. Set up routes, authentication and frontend

Add this to the routes.php like so:

Create a Welcome Controller like so:

WelcomeController.php

Go to resources/views/welcome.blade.php and replace it with this:

Create a layouts directory and app.blade.php in it

app.blade.php

Open the routes.php file and add this:

Create a partials folder in the views directory, add signup-form.blade.php, signin-form.blade.php, shoutout-form.blade.php, shoutouts.blade.php and errors.blade.php in it.

partials/signup-form.blade.php

partials/error.blade.php

partials/sign-in.blade.php

partials/shoutout-form.blade.php

partials/shoutouts.blade.php

Create an auth folder in the views directory, add login.blade.php and register.blade.php in it.

auth/register.blade.php

Add this property to AuthController.php. This is where a user is directed to when he/she registers and logs in.

5. We are going to need to store and retrieve shoutouts, let’s create a ShoutOutController like so:

Now, that the controller has been created, let’s stub out some routes.

Before we go further, Let’s creat the Shout Model like so:

It creates a Shout.php class in the app directory.

Shout.php

User.php

Now that we have established the relationships, let’s move ahead.

Create a shoutouts directory and index.blade.php in it.

index.blade.php

Create a shoutout-form.blade.php in the partials directory

shoutout-form.blade.php

Head over to the ShoutoutController.php. Make sure you have this:

ShoutoutController.php

Now, we can add shouts. Let’s retrieve all shouts for all users and display it.

In your ShoutoutController.php, the index method will change to this:

Let’s quickly Add the Logout route in routes.php like so:

Let’s add the login routes too like so:

So, test the application and make sure it works properly.

Conclusion

This is the sample application for our logging tutorial.

The source code is on github. Check it out https://github.com/goodheads/shout-pad .

We just prepped for the logging here by going through the sample application together. In the next post, we’ll handle Logging :smile:

Please, if you have any questions or observations, let me know in the comments section.

 

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.