Why use Laravel?

What is Laravel? Why should any PHP developer care about it?

A few days ago I had to present at a local PHP Meetup in Fullerton, CA. I spoke about Laravel framework and why it will be the future of PHP frameworks (and that future is already here).

Below I have that presentation from my talk and also the content of the slides typed up in text. Enjoy!

laravel

Clean and classy PHP framework 


Presentation by Maks

What do I do

Breakdancer (4 years)
IT support (8 years)
Web Development (3 years)
Created Bootsnipp.com in 4 days with Laravel

WHY Another Framework?

  • Easy to start with
  • Free from spaghetti code
  • Eloquent syntax
  • Modularity (Bundles, now Composer)
  • Testability
  • Clean 
  • Dependent
  • Eloquent ORM 
  • Easy routing
  • Caching
  • Combines best of many worlds
  • Flexible MVC
  • MIT license



As @Rutger says

“With Laravel we can do it in one line.”



Eloquent ORM

Uses prepared statements (SQL injection defense)
works with MySQL, PostgreSQL, SQLite, SQL Server
Example of actual usage: 
class Company extends Eloquent {}

$company = Company::create(array(
           'address' => '155 Harbor Blvd',
           'city' => 'Fullerton'
));
$company = Company::find(1);

echo $company->address; // 155 Harbor Blvd 
$company = Company::where('city', '=', "Fullerton")->first();
$companies = Company::order_by('city', 'desc')->take(10)->get();

Relationships

One to One
class Company extends Eloquent {
     public function phone()
     {
          return $this->has_one('Phone');
     }
}
$phone = Company::find(1)->phone;
Many to One
class Company extends Eloquent {
     public function customers()
     {
          return $this->has_many('Customer');
     }
}
$customers = Company::find(1)->customers;

Many to Many

Routing

Super easy
Route::get('/', function()
{
    return "Hello PHP!";
});

Route::post('user', function()
{
    //
});

Route::put('user/(:num)', function($id)
{
    //
});

Route::delete('user/(:num)', function($id)
{
    //
});

Routing #2

Controller Routing

    
Route::controller('company');

Route filters

Route::get('create', array('before' => 'auth|csrf', function()
{
    //
}));

Authentication 

Out of the box
$credentials = array('username' => 'example@gmail.com', 'password' => 'secret');

if (Auth::attempt($credentials))
{
     return Redirect::to('user/profile');
}

Get user's info : 
return Auth::user()->email;

Logout:
Auth::logout();

Blade templating engine

Kinda like PHP-ied HTML, ex:
@if ($tags)
	<p class="lead">Tags matching the search:</p>
	<ul class='nav nav-list'>
@foreach ($tags as $tag)
	  <li><a href="{{action('tags@view', array($tag->slug))}}"><span class="label">{{$tag->snipps()->count()}} snipps</span> {{$tag->name}} </a></li>
@endforeach

Environments

$environments = array(
    'local'      => array('http://localhost*', '*.dev'),
    'staging'    => array('http://dev.mywebsite.com*'),
    'production' => array('http://mywebsite.com*'),
);

Then add environment specific config files under 

application/config

application
  - config
     - staging
        - database.php
        - session.php
     - production
        - database.php
...

Done!

 

Bundles (now composer packages)

Don’t reinvent the wheel…
Installation of bundles easy as :
php artisan bundle:install Mybundle

SOme cool packages:

  • Bootstrapper
  • Former
  • Oauth2 (12 service providers ready to go)
  • Mailgun
  • Mandrill
  • Hashids
  • Cartify (shopping cart ready to go)

Validation

$input = Input::all();

$rules = array(
	'name' => 'required|min:3|max:32|alpha',
	'age' => 'required|integer'
);

$v = Validator::make($input, $rules);

if( $v->fails() )
{
	// code for validation failure :(
}
else
{
	// code for validation success!
}

Artisan CLI

# Application Configuration

key:generate        Generate a secure application key.

# Database Tables

session:table       Generate a migration for the sessions database table.

# Migrations

migrate:install     Create the Laravel migration table.
migrate:make        Create a migration.           
migrate             Run outstanding migrations.   
migrate:rollback    Roll back the most recent migration.
migrate:reset       Roll back all migrations.     

# Bundles

bundle:install      Install a bundle.             
bundle:upgrade      Upgrade a bundle.             
bundle:publish      Publish all bundles' assets.  

Artisan CLI #2

# Unit Testing

test                Run the application's tests.  

# Routing

route:call          Call a route.                 

# CLI Options

--env=              Set the Laravel environment.  
--database=         Set the default database connection.

Community



IRC channel #laravel

@laravelphp


Near future

Laravel 4 hotness (PHP 5.3, 5.4, 5.5)
Composer packages
Unit tested everything

Thanks!


Liked it? Take a second to support Maks Surguy on Patreon!
Become a patron at Patreon!

You may also like

13 comments

  • Set Kyar Wa Lar March 11, 2013  

    Yes the best one for me! I am waiting your new laravel posts!

  • phaoris March 20, 2013  

    i must admit, i’m addicted to your post dude, keep posting bro 🙂

  • Luis May 16, 2013  

    Hi, i’m starting with frameworks, i see first Zend Framework, then Symfony, but Laravel is amazing, very easy to learn

  • Kevin October 1, 2013  

    Good post. I guess Laravel is exactly what I need. Thanks!

  • Shashi November 18, 2013  

    Thanks for the information.. i am convinced enough to go ahead with Laravel.

  • silverdr July 11, 2014  

    Why use Laravel? Because if you want (or have) to stick with PHP, that’s the best Rails copycat you can put your hands on.. at last!

  • Christof Coetzee September 7, 2014  

    We’ve seen frameworks/platforms come and go over the past decade: http://www.christofcoetzee.co.za/laravel-sucks/

  • maxsurguy September 7, 2014  

    While I don’t agree with most points you present in your article, it is indeed to have a view of both sides of the coin 🙂

  • Christof Coetzee September 7, 2014  

    In my post http://www.christofcoetzee.co.za/laravel-sucks/ I only mention 5 Laravel “cons”, there are more.
    Many senior software engineers agree with me on these points – anyway, I don’t want to start a flame war, I just want developers to have an open mind when evaluating technologies and not just piggyback on the mainstream hype, which tend to be short-lived, we’ve seen the same pattern with Codeigniter, CakePHP etc.

  • maxsurguy September 8, 2014  

    Cool.

  • silverdr September 8, 2014  

    Many junior software engineers will keep that in mind. Even if they disagree on some. And even if you don’t publish any more links to your post. Really 😉

  • Christof Coetzee September 8, 2014  

    Reason why I published my link here is because it would be stupid and rude to paste my whole article here

Leave a comment