Developing application with Laravel 5 - Part 1

Laravel is a PHP framework that has been taking the PHP development world by storm, its one of the most "Starred" repositories on GitHub and it has a huge and growing worldwide community, all this in just four years. One of the primary reasons is the use of the best quality features from other frameworks that enables you to rapidly build and deploy working cloud based applications.

For this series of articles we will use a CentOS 7 virtual instance to get started with developing a Laravel application. The latest version of Laravel at time of writing is 5.2 and the virtual instance of CentOS has PHP 7 from the Remi repository.

Installing composer and Laravel.

Laravel uses Composer to instal its packages, it works nicely and if you have never used composer you will pick it up easily and quickly. Installing Composer is well documented on the Internet, in brief, you can instal it simply using:

Our framework is now installed and we can now begin the next step. For any useable project you will need to access a database, this will require the Laravel environment to be configured, for simplicity a file called .env exists in the "testapp" directory that holds the key parameters for accessing a database.

Change into the testapp directory and open up the .env file with your favourite editor, I use vi.

The default database values are for "homestead", configure these to use you own database, it is assumed you know how to do this. For our example, I am going to use a database called "test", with a "test" user and "test" password as a pure example and not for any type of external production deployment. It is highly advised you use a strong username/password combination for you database configuration for any production systems and for any Internet connected development servers.

The last thing to do is set the permissions of the files and directories, they need to be accessible as the web server user, in our case "apache" and writeable where needed (storage and logs).

Our Test Application

For this example, we are going to create a few database tables, some Models to manipulate them and a simple View to show the table contents prior to diving into the full Model View Controller (MVC) design cycle that Laravel is designed to provide. With a default instal of Laravel 5.2 we have a basic working empty framework that should render in a web browser, to achieve this we need our hosting to point to the testapp/public directory.

Configure a virtual host entry that looks similar to the file below. I keep all my vhost config files in /etc/httpd/vhost.d, there is one file for each web site, your hosting plan might be preconfigured to just httpdocs, you might need to discuss this with your hosting provider.

After the file is configured, a reload of the web server will be required, with a DNS entry in place, we can navigate to testapp.mydomain.com and up comes the Laravel framework's default welcome page. The key point to note is the "public" directory is where your application runs from at all times, so any file references for CSS or Javascript code will be loaded from the public directory and below it.

Database Tables

Laravel provides a simple, clean and manageable solution to creating, extending and managing database tables, its called migrations. The migration files live in testapp/database/migrations.

Rather than raw SQL definition files as used by old school project development, the migration files are PHP programs that get run to create and or alter DB tables. By default two tables files are created during the instal of the project, one is called users, the other is called password_resets. You will also see the format of the file names, its designed that way to enable the code to manage both the order of the table builds as well as tracking what has been built.

To create the database tables there is a PHP program called "artisan" that lives in your projects home directory, this php application has many uses and automates a lot of the project build process which is another reason Laravel is so popular. The artisan application will build any required directories for some commands and assist in making sure you don't damage your project as you develop.

The basic functions are up() and down() wrapped inside a class called CreateUsersTable. The Class name is embedded in the file name so the artisan tool can create an instance of the class from the file name and the run the up() method to perform the table creation.

I'm not overly happy with this table for my use, so I am going to change it and build the newer version:

In rebuilding the table my way I eliminated the timestamp field and just used dates, added some more details and am now ready to build a model that will manipulate the table and a simple view. I also need to add a route so I can call the view.

Route, Model or View?

Lets start by adding an entry to our routes.php file. This is how Laravel routes URL's to code and it makes building apps very easy, clean and elegant. You will find routes.php in the testapp/app/Http directory.

The contents of an newly created routes.php file is:

<?php
Route::get('/', function () { return view('welcome'); });

Lets add a route for our test app view of the users table. I will be using the bootstrap framework to format up the page and a Laravel blade template to render the output. If you don't know what bootstrap is, take some time to review the link above, in short bootstrap provides a robust responsive environment that simplifies the web GUI significantly. If you are not familiar with Laravel's Blade Templates, don't panic, these will get covered in a future article in this series.

Normally you would add a route to point to a controller and pass in the method name but for our first example we will just add a route to the view file, this just requires us to open routes.php with an editor and add a GET request line:

I also add the following to the end, a "catch all" to show routes I have not yet coded for, you don't need it now but you will find it handy down the track when you are building an app with dozens of routes:

The views are located in testapp/resources/views. You can place sub-directories in here to segment up your app into "modules" and each time you refer to a view, you use the "directory.directory.file" notation. A file in the "view" directory is just a PHP file. Under a Users directory in the testapp/resources/views directory it becomes "Users.filename".

Below is the test file showusers.blade.php with all markup included, this code includes the bootstrap Javascript and CSS as well as the JQuery library needed by bootstrap, its all hosted from content delivery networks so I don't need anything local:

The head section is pretty straight forward, it has components to load for the bootstrap framework, including some Javascript and CSS libraries.

The body consists of a 'container' object and two 'row' objects.

One 'row' is a page header and the other 'row' is a table which we will populate from our controller.

The blade template bits are the {{ $variable }} construction... this will be obvious when we look at the Controller code.

If you try and run this in your web browser you should get some output, using my local server http://testapp.mydomain.com/users/show I get a Laravel Framework error, briefly shown here:

Whoops, looks like something went wrong.
2/2 ErrorException in b318075f96ee7243846c71ec13cf0e23 line 37:Undefined variable: users (View: /var/www/vhosts/mydomain.com/laravel/drs/resources/views/Users/showusers.blade.php)
in b318075f96ee7243846c71ec13cf0e23 line 37
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44

The error is the undefined variable "$users", its not yet been defined anywhere. So this presents a good time to outline Laravel's, MVC design concept as I understand it.

A brief intro to the MVC Concept

MVC is an old software engineering concept that has been used in Frameworks for years, as a result its a mature and well tested architectural design pattern that is often used. There are numerous deviations of MVC but essentially I see it as:

Views - Contain all the markup to render the data which is passed in. There should generally be no code to access a data base or make business decision logic. From time to time, some support code to render the output might be needed but this is rare.

Models - Contain the code needed to access a database table, no business logic should be present, just basic methods to access data and return it to the caller. Think of it this way, if you have 55 tables then you will have 55 models in most application designs. Models can also be non-database related.

Controllers - These contain the business logic that calls the Models to get the data and then manipulates the data for presentation by the View. The Controllers should also handle form posts and if your building other web services then Controllers will do the back end work of those as well.

The MVC concept is simple and works well in the Laravel Framework, I have only just barely touched on the subject but we will now change the routes.php file to include a controller and add more code. This is where the artisan tool comes in handy, lets add a controller called UserController and include some code to render our view.

From the testapp base directory run the artisan script using the following command line:

This shows a few important steps, our route now causes the Framework to create our UserController object, and our ShowUsers() function is being called, the dd() method just dumps the controller object and exits.

If we now change the function ShowUsers() to actually create a user, add it to a container object such as an array and pass this to the view as a "$users" variable, our view can then render the data. Remove the dd($this); line and replace the function with:

Now refresh the browser and you should have a working bootstrap responsive table (with row highlighting) of our single user. If we create more "user" objects and add them to the array being passed to the view, then we get those additional rows displaying as well.

So far so good. Now lets build a Model and fetch data from the database. This will exercise a few aspects of the Framework, we will use artisan to create a Model and a database "seeder". The Seeder is a mechanism to populate our database tables with constants that are needed for the application. In our case we need some semi-real data as we don't (yet) have a data entry form.

The 'Model' "Users.php" is created in the testapp/app directory, later I will show you how to put this in a directory called Models under the 'app' directory, just by reconfiguring the environment. For the moment it's current location will suffice. Now lets edit the Model to do a DB table fetch from our "users" table and return that data back to the Controller.

Now we need to change our controller file so instead of using the fixed data, we create an instance of the 'Model' and call the method to get data from the Database. Open the UserController file and edit the ShowUsers() function to look like:

There is actually a lot missing from this file, so add in our user, this time we can leave the 'default' fields out as the insert will add those. Using an editor, open the UserTableSeeder.php file located in testapp/database/seeds and add the lines below:

Now if we run the seeder from artisan to cause the population of the database table we should see something?

# php artisan db :seed
#

So now NO output shows, that's easy to solve, by default Seeders are disabled!. We need to enable them by editing a file in the seeds directory. The file is called DatabaseSeeder.php. it should look like this:

The table has seeded our user into the users table, and we can now run our web app and see the same output as before, this time the data came from the Database via the Model, it was fetched by our Controller and passed to the View for rendering.

That's a lot accomplished in one tutorial but it shows a simple easy way to get started with the Laravel Framework.

Sid has 35 years experience in IT & Electronic Engineering. His extensive skill set including; Systems Engineering, Networking, Virtualization & SAN support. Spending most of his day developing Systems/Virtualization Engineering of a large Cloud Infrastructure & developing software applications.