Blog


How to do a simple Audit System in Laravel 5.5

Well, in this post I will show how I made a simple audit system using the jenssegers/agent package to get us the stats of system.

To begin, we will need to define what our system will track, in my case I needed log every access to system and track:

  • user
  • method / verb
  • path / uri
  • querystring
  • userAgent
  • ip
  • device
  • platform
  • browser
  • isDesktop
  • isMobile
  • isTablet
  • timestamps



Laravel web routes

Well you already understand how MVC works (however I don’t wrote about Laravel Lifecycle, uhrr… I’m planning a dedicated post about it) and you also know how artisan works but now it’s time to understand more about routes 😉

We have 4  types of routes:

  • api
  • channels
  • console
  • web

The routes are defined in routes/type.php, in this post we will talk about web routes.




Artisan, our best friend

Well, to start, artisan is based on Console component of Symfony. Let’s see what the documentation says about the Artisan.

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

In this post we will focuses on most used commands from artisan. Artisan is nothing more than a php script that runs in console and can assist us in some boring and repetitive tasks.

If you use the list commands, you will see the complete list of available commands in artisan.

Like documentation says:

Every command also includes a “help” screen which displays and describes the command’s available arguments and options. To view a help screen, simply precede the name of the command with help:

Important notes

  • I advise you to see all commands with help to understanding more about the artisan;
  • You can create your own commands;
  • Commands are grouped by context;



Understanding the Laravel Configuration

Laravel offers us a great diversity possibilities out of box. Like as environments of production and development, and knowing about config is essential to start understanding how framework works.

It is often helpful to have different configuration values based on the environment where the application is running. For example, you may wish to use a different cache driver locally than you do on your production server.

By default the main configuration file is .env and it’s in main folder. How I told in a past post, Laravel uses the DotEnv Library to get these variables inside this file. The .env file is ignored by GIT because each environment can have a different configuration.




Tasks CRUD with Laravel 5.5

If you read the last posts you already understand how MVC is applied in Laravel, but our example can be more powerful and next to real world. What if we made a little CRUD demonstration with Laravel 5.5.

Let’s do that!

Well,  start a new project called task. And let’s remember some things.

Initial config –  Homestead

I’m supposing that you have Homestead installed, if not see the documentation:
https://laravel.com/docs/5.5/homestead

First configuration steps, remember change hosts file /etc/hosts to add the line:

Make some changes in Homestead.yaml (Usually in /Users/YOURUSER/Homestead/Homestead.yaml) to map new site.

Only to understand, we are creating the map (links) and the database for it project.

Now restart your homestead

Is everything fine? Get to work!

First steps in Laravel 5.5

The .env file
Remember to change these variables in your .env file

Please attention in DB_HOST=192.168.10.10. I like to use the IP of homestead because in this way we can do php artisan migrate out of vagrant ssh. The default password of MySQL Homestead is secret.

First step, creating a model with migration




MVC in Laravel – Part 3

* Bug fix

Before continue, please change your config/database.php and change your line 38 to:

Now you have your database with some records that I supposed that you been added, you have a Model and connection to database (SQLite) but you don’t have a Controller and View.

Let’s do that!

Firstly, we need a controller

Now look the file  app/HTTP/Controllers/NewsController.php

When we use –resource parameter in artisan make:controller Laravel creates the needed methods with comments for us.

Well, in this case we only use the index method. So, you can remove the others.

Now we have a controller but, we don’t defined a route and view.

Defining the route

Start by defining the route. Remember, the HTTP Requests are requesting something to application and your application will send something to user, these paths or ways are called as routes and will be defined on your routes/web.php

In routes/web.php we need to add a Route to listen verb GET and search for a path and associates it to a controller, how we do that? simple add it to your route/web.php file .

Now when some one request for /news path, the application defines that route is associated to NewsController AT index method.

Use php bult-in server to test it

Well, if you visit your http://127.0.0.1:8000/news you don’t see any error, however you can see that a blank page was returned to us. We need the news not a blank page. Relax, it’s because we don’t returned any view or response.

But, how we could get ALL the News from your database and pass it to a view ?

Simple, the model has a static method called all!!! 🙂

For a while your controller may be like it

Now you can see something similar to it at the page.

dd()

The dd function dumps the given variables and ends execution of the script:

That’s cool, but we still need to pass it to a view!

Create the news.index view

Create a new file resources/views/news/index.blade.php

Note .blade.php means that it is a view and will use the Blade. Blade is the default template engine of Laravel.

Remember in controller you can use the helper view(). The first argument is the name of view but you don’t need to specifies the full extension if you’re using the standards. Simple use view(‘news.index’) the function will understand that you are looking for folder news and view index.blade.php.

Are many ways to pass data to your views, but we will see it after. For now you can use a second argument of function, it receives a array of your data.

Here’s your final NewsController.php

After it, your view receives the $news with the collection that we saw before.

Here’s your news/index.blade.php view file

Note, the @foreach and @endforeach are directives of Blade.

Done!!!

Well, is it. It’s done, now you understand how Laravel applies the MVC.

Other parts of it demonstration




MVC in Laravel – Part 2

Still talking about routes

Behind every request has a pre-defined route for it action.

Laravel uses the HTTP Verbs in your routes, however by default HTML forms do not support PUT, PATCH or DELETE actions. To get around you will need to add a hidden _method field to the form.

Let’s see the default actions of a resource controller

Back to our default example

We will create a simple page with news from database to demonstrate how it works. In future we will see more about each topic of this first example, remember it’s only to demonstrates how is MVC in Laravel. Don’t focuses on little details because we will review each topic with more details.

Artisan the power of console

Some tasks in development are repetitive and boring, it’s cool have I friend in these times, yes we have, it’s artisan, the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

Type php artisan in your application root directory to see a lot of commands that you can do with it.




MVC in Laravel

If you didn’t read the previous post about Directory Structure in Laravel, you may be thinking that Laravel have specific folders for each layer in MVC, but isn’t like that.

To try to explain how the MVC works in Laravel we will do little and basic CRUD application together.

Getting started

To understand more about MVC in Laravel, we need to install a fresh installation of Laravel, let’s do it

Only to remember, we are creating a new project ( laravel/laravel ) in folder laravel with default params.

To certify that everything goes right, start server in your terminal with artisan. Type the command below in the laravel folder in your terminal.




A break in Laravel to understanding MVC architecture

Why MVC Design pattern?

The Model-View-Controller (MVC) software architectural pattern separates an application into three main logical components: the model, the view, and the controller, it design pattern decouples these major components allowing for efficient code reuse and parallel development.

How MVC Works?

Think in these logical components as layers of your application.




Knowing the Laravel directory structure, commented

Sometimes read of documentation is boring and you don’t need to know everything when you’re starting learn something new. Because it, I made a summary about the most important things about Laravel Directory structure.

Folders and main files

I think that before we getting started development is very important knowing the directory structure of your applications, it can show us where put some file or how something works for example. See the default directory structure of Laravel (except the dep folder).

Important! The dep directory isn’t of Laravel fresh installation, i’ve created it to store some files.