CultureMethod

Laravel – PHP’s hope for a brighter future

By April 13, 2016 No Comments

This is a CI day post, for more information on CI days click here.

 

TL;DR

Laravel is a PHP version of Ruby on Rails. PHP on Laravel.

What is Laravel?

“The PHP Framework For Web Artisans” reads the headline at https://laravel.com/. Kinda pretentious, sure, but seeing what the Laravel team (open source contributors) have done with PHP will make you forgive their cockiness. They’ve created a powerful and scalable web framework and have brought honor to their families.

It’s almost impossible not to compare Laravel to Rails. They’re both server side MVC frameworks that do loads of things for the developer so applications can be built quickly. I’ll be comparing the two because of my experience with Rails, but I really want to highlight what makes Laravel interesting on its own.

Laravel Tools

Laravel has many tools that come out of the box to make Laravel easy to get up and running fast. It has a prebuilt Vagrant box called Homestead so you can quickly get a dedicated server running your program, helping teams get moving quickly. If you don’t want to run on a virtual machine and want to move even FASTER, you can use artisan, Laravel’s CLI, to serve your project locally. But artisan doesn’t stop there. It can create boilerplate for MVC components, create and run migrations, seed the database, list routes, and many other actions for configuring and viewing app information. The only thing it can’t do is explain why kids love Cinnamon Toast Crunch.

Elixir is a Laravel tool for configuring Gulp. It essentially waters down the syntax for simple gulp settings so it’s easier to set up your project. Just add the module you want to package.json and “mix” it in. Done.

Elixir is a Laravel tool for configuring Gulp.

Other notable tools:

  • Flysystem – drivers for working local files and cloud storage (S3, Rackspace)
  • Cashier – interface for Stripe subscription billing services
  • Eloquent – ActiveRecord like ORM
  • Socialite – interface for OAuth with all your favorite time-wasting network hubs

Blade

PHP is ugly. We all know this. It’s verbose, it forgoes common programming notation, and it makes us all sick.

Blade is PHP. Blade is not ugly. Blade is ???

Blade is a template language used by Laravel. It allows you to insert data and logic into your PHP. I’m sure your thinking, PHP already allows me to insert data and logic into my PHP. You are correct. And because Laravel views are PHP files, you can use the same PHP you’ve been using forever to output data. Blade just makes it faster and easier.

The following is a template written with blade. You tell it which layout to use and create sections of content. This example shows a foreach loop, but you can also do most other loops, control flow with if/else and unless, and the double {{ brackets }} echo data for you.

Laravel template written with blade

The following is the accompanying layout file. You can create multiple “yield” sections with different names, and set them independently in the template files. A simple example of this would be adding “@yield(‘title’);” in the title tag. Then on each template you can add a section for the title so it changes on every page. Quick and dirty way of adding dynamic titles. Done.

Laravel blade layout

Other Things That I Think Are Cool Enough To Write About But Aren’t Necessarily Groundbreaking Or Even That Useful

Automatic JSON building: When a GET request is made, if the corresponding controller action just returns data, the data will automatically be sent as JSON. No need to create a JSON file or change any settings. Neat.

Composer: Laravel uses a package manager called Composer. By package manager I mean like Bundler for Ruby or local node modules; project specific. I like it better than Bundler because it uses a JSON file like package.json. It’s more common and has more configuration options. Very nice.

Type Hinting: This one is a little weird. Basically, you can tell Laravel what class you’re going to be querying for data, and it will do it for you. Let me explain.

Set a route like this. The brackets specify a parameter (a card ID in this example).

route

There are a couple ways to get the card data for the view. The following is a common way used by many frameworks. The parameter is passed as an argument, you query the database(using Eloquent in this example), and set it to a variable.

query-1

BUT BUT BUT… Laravel gives you another way. You can add a class name before the arguments and Laravel will make the query for you. The argument variable will be set to the queried object. Look about one inch below for an example.

query-2

While I find this pinteresting, I think I’d prefer making the query myself. It allows for more control, it’s easier to read, and it doesn’t require some oddball syntax that you’d never use again.

PHP on Laravel

Laravel is clearly very powerful and impressive. I have no doubt that a skilled PHP developer can create large and complex applications with it. However, if you already have experience with something similar like Django or Rails, I don’t see anything advanced enough to make it worth switching. If your team does a lot of WordPress and is looking at making custom APIs, I think Laravel is a great option. If you’ve already had success with Rails, I’d stick with what you know.

Further Reading

https://laravel.com/ – Information and Documentation for Laravel

http://builtwithlaravel.com/ – Sites built using Laravel

https://getcomposer.org/ – Information and Documentation for Composer

https://laracasts.com/ – Video tutorials for Laravel

https://lumen.laravel.com/  – a Micro-framework by Laravel

Leave a Reply