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!


The following two tabs change content below.

Maks Surguy

Full stack web developer, speaker and writer.
Maks is young and energetic breakdancer turned into web developer who lives in Seattle area with his wife. He is well-versed in three languages (English, Russian, Ukrainian) and a dozen of programming languages.