07 Oct
2016

Shane Todhunter

By

Bam! Emeril

PHP, started by Rasmus Lerdorf back in 1994, has become one of the most popular server side scripting languages thus far The name actually came from “Personal Home Page” as it was originally conceived as a set of tools to help Rasmus track hits to his personal resume. I first started off as a Classic ASP developer back in the late 90’s and when one of my first employees introduced me to PHP, I was hooked; so many useful utilities came for free and it had a very quick learning curve. As a self taught web developer, it was an absolute no brainer to start using PHP.

php-med-trans

Apparently I wasn’t the only one who thought this, as PHP grew rapidly to power more than 100 million websites worldwide. PHP has gone through a number of iterations with the most current being version 7 (Note: Version 6 was skipped when version 7 showed so much promise) The big benefits of php7 have been the speed improvements, you can find Lots of info on this link here.

If you have worked with PHP before, I am sure you can attest to its ease of use and are likely well versed in it’s frustrations as well. It’s really easy to write bad, and I mean REALLY bad code using PHP. Think of a functions.php file containing 10,000 lines of code, and you know where I’m going!

Along came frameworks.

A framework, as the name implies, is a structure on which to build. In the case of php Zend, the company behind PHP, they have had an open sourced framework that has been available since 2010. A framework provides developers with a few important things.

First it provides a general structure for your application. Second, it provides some guidelines for how to write your application. And finally it provides lots of useful utilities, classes, template engines, etc.. Some frameworks are full featured with everything you could ever want (and then some more than you want) while others have carved out their niche by slimming down and offering just the basics. Most frameworks offer some method of bolting on new features or tools as required and many now include baked-in testing tools.

We have evaluated the use of frameworks for a number of years and have even gone so far as to create our own here at 14 Oranges. It was pretty bare bones and ended up being difficult to maintain. We have since opted for the use of two different frameworks. Slim framework and Laravel. I discussed the Slim framework in a previous blog here

https://www.14oranges.com/2016/05/creating-an-api-fast-with-slim-framework/

But today I would like to discuss what the modern frameworks are offering up to full application development. I will refer mostly to Laravel as it seems to be the most popular framework these days but much of what I will describe is likely true for other frameworks as well.

Barrier to Entry

The learning curve for php, as mentioned above, is pretty not steep. If you are a logical person with some basic knowledge on coding you can probably learn to write a simple database driven application in a few days. Now leverage a framework and you can certainly do the same in less time. That being said the first time I tried to use a php framework I banged my head against the wall for days just trying to figure out where to put stuff. This was a number of years ago and I can attest that things have definitely improved.

Modern popular frameworks have large active communities that will answer questions quickly and are have code bases that are well documented. Laravel has done one better and has added a large number of video tutorials that help make learning the basics a snap for a noob.

Naming Confusions

One of the things that does create a bit of a learning curve is the naming of the framework components. Many of the patterns used in frameworks may be commonly used names by hard core programmers, but for a lowly web developer these names can get confusing fast.

This is a cut and paste for what shows up when I search for “Creating a facade in Laravel” (bare in mind I had never heard the term facade used in coding before I started working with Laravel)

  1. Create PHP Class File.
  2. Bind that class to Service Provider.
  3. Register that ServiceProvider to Config\app.php as providers.
  4. Create Class which is this class extends to Illuminate\Support\Facades\Facade.
  5. Register point 4 to Config\app.php as aliases.

Ok so let’s step through this really short list and talk terminology.

Number 1, ok so this one shouldn’t require explanation of course.

Bind that to a Service Provider … eh.. Hmmm … ok. So Binding and Service Provider. Logical terms but again hadn’t run into this before … Uhh oh … Now that I’m bound to a service provider I have to register it to the App Config. OK then I have to extend Illuminate. Well you get where I am going with this.

Work with Laravel for a month and those 5 steps are easy-peasy but your first time through it can get confusing.
Framework Benefits

If you are still with me, it means the barrier to entry stuff above didn’t scare you off, so thanks for sticking it out. As mentioned above, Frameworks help you organize your application and give you guidelines and tools to keep it well written and manageable. Frameworks provide this organization following application design patterns one of which is MVC (Model View Controller). MVC has become the defacto standard for PHP frameworks and offers the separation of data (Models) from logic (Controllers) from markup (Views). Laravel takes this a few steps further with the use of an Object Relational Mapping system (or ORM) and Template Engine.

ORM (Object Relational Mapping)

From Laravel’s website.

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Eloquent is the ORM that comes with Laravel and basically it creates an interface between your database tables and the objects you work with in your application. Creating these relationships allows your to write no SQL at all to interact with your database. It allows you switch databases easily and also implement multiple databases into one application. Ie mysql and mongodb.

Interacting with your models is as easy as wiring up your model then using things like.

$user = User::findOrFail($userId);

This gets an instance of your User object, gets all the related data for the user and lets you interact with it.

Want to change it? Easy

$user->name = ‘New Name’;
$user->save();

Bam (did that in my best Emeril Lagasse impression) database record for that user is updated.

bam

Creating a CRUD (Create Replace Update Delete) interface is easy to say the least.

Routing and Resource Routing

CRUD interfaces to a given resource can be implemented so efficiently it’s almost silly.

Check this out..

Laravel utilizes routing. Basically you define what url endpoints your application will listen for and what http methods it will listen to.

So this:

Route::get(‘users/{user}/password-reset’, ‘UsersController@resetPassword’]);

Means this:

Listen for a normal get request to domain.com/users/{userid}/password-reset

And:

Use the method resetPassword from the UsersController

So typically in a CRUD interface you need a bunch of endpoints resembling:

Show a list of all
Show one
Create One
Edit One
Insert One
Update One
Delete One.

So that would be a bunch of routes right..

Nope

Just do this.

Route::resource(‘/users’, ‘Backend\UsersController’);

And it creates them all for you.

Use Laravel’s command line tool Artisan and you can make a controller with all of the endpoints already created for you.

Now don’t get too excited! This is really just boilerplate code with correctly named methods inside a class. But add that to a properly related Eloquent model, and throw in the easy to use out of the box Validation, and you will be creating a CRUD interface that is well structured, readable and, best of all, fast.
What does it look like?

Right right right … Web developers want to see what things look like too.

Ok, so you need to do a little bit of work on this side of things. View files are just html files that allow you to compartmentalize code. But Laravel comes bundled with the Blade template engine. I won’t get into this too much but it’s easy to learn, easy to use and provides an easy way of templating the site.

Bootstrap, Sass, less, gulp, bower and the myriad other things front end developers need can all be implemented providing a great end-to-end user experience. Letting Laravel do all the heavy lifting and providing the application backbone really lets you work well in teams as well. Have a front end developer get familiar with the framework and rolling out an application is a breeze. Have your backend developers use composer and your front end guys use bower along with Laravel’s built in tools to deployment make this all a snap and maintainable.

Would I do it all over again.

Short answer: Yes! PHP frameworks have come a long way in the past few years and the toolset they can provide make code readable, well organized, stable and maintainable. There is very little overhead with getting these frameworks up and running and the little bit of extra work that goes in at the beginning is saved tenfold as the project rolls out into the real world.

Do it. Learn a framework.