Discovering Laravel 5

Laravel Montréal #7 - March 12th, 2015

Before we start ...

Thanks for comming !

Sponsors

Jobs

Get involved

Benjamin Gonzalez

  • Web Developer back-end and more recently front-end
  • Involved in PHP since 2006 with a few years off for good behaviour
  • CTO for InfoPrimes, founder of Laravel MontrĂ©al we meet every month, you are welcome :)
  • @BenjaminRosell at Github and Twitter … also on LinkedIn

Homestead

Artisan

Blade Templating

Unified API for database access

Migrations

Eloquent Models

Relationships

Caching

Queues

Queueable Mailer Class

Authentication

Events

Localization

Packages

What ?

Prerequisites

  • PHP >= 5.4
  • Mcrypt extension
  • OpenSSL extension
  • Mbstring extension
  • Tokenizer extension
  • JSON extension
  • Composer getcomposer.org and install it globally!
  • Don't worry too much PHP as binary package and relax...

New Project Structure

Project Folders

  • app < your application's logic
  • bootstrap < startup stuff
  • config < settings and config
  • database < seeds and migrations
  • resources < assets, language files and views
  • storage < stuff generated by Laravel
  • tests < ...
  • public < static stuff: img, js, css, etc.
  • composer.json < no changes here
  • vendor < other people's stuff

Project files

  • .env < all your environment settings
  • gulpfile.js < Laravel Elixir
  • phpspec.yaml < PhpSpec settings file
  • phpunit.xml < PHPUnit settings file

Application Folder

  • Commands < the core of your application's logic
  • Console < your artisans commands
  • Events < applications events
  • Exceptions < exception handlers
  • Handlers < Command and event's handlers
  • Http < Controllers, Middleare and Requests
  • Providers < Service Providers
  • Services < Other "helper" classes

Controllers are namespaced !

PSR-4, baby !

Application Folder

  • make:command < Create a new command class
  • make:console < Create a new Artisan command
  • make:controller < Create a new resource controller class
  • make:event < Create a new event class
  • make:middleware < Create a new middleware class
  • make:migration < Create a new migration file
  • make:model < Create a new Eloquent model class
  • make:provider < Create a new service provider class
  • make:request < Create a new form request class
  • event:generate < Generate the missing events and handlers

Starting a New Project


composer create-project laravel/laravel unicorn --prefer-dist

	

php artisan app:name Unicorn

	

Routing updates

Method Injection

Dependency Injection


namespace App\Handlers\Commands;

use Illuminate\Contracts\Mail\Mailer;

class PurchasePodcastHandler {

    protected $mailer;

    public function __construct()
    {
        $this->mailer = new Mailer;
    }

}


	

Dependency Injection


namespace App\Handlers\Commands;

use Illuminate\Contracts\Mail\Mailer;

class PurchasePodcastHandler {

    protected $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

}


	

Dependency Injection


namespace App\Handlers\Commands;

use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\OtherClass;
use Illuminate\Contracts\Validator;
use Illuminate\Contracts\SomethingElse;

class PurchasePodcastHandler {

	[...]

    public function __construct(Mailer $mailer, OtherClass $otherClass, Validator $validator, SomethingElse $somethingElse)
    {
        $this->mailer = $mailer;
        $this->otherClass = $otherClass;
        $this->validator = $validator;
        $this->somethingElse = $somethingElse;
    }

}


	

Dependency Injection


namespace App\Handlers\Commands;

use Illuminate\Contracts\Mail\Mailer;

class PurchasePodcastHandler {

    protected $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function handle(Validator $validator) {
        $validator->run();
    }

}


	

Form Requests

Self Validating forms !


namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Response;

class FriendFormRequest extends FormRequest
{
    public function rules()
    {
        return [
            'first_name' => 'required',
            'email_address' => 'required|email'
        ];
    }


	

Self Validating forms !


php namespace App\Http\Controllers;

use App\Http\Requests\FriendFormRequest;
use Illuminate\Routing\Controller;
use Response;

class FriendsController extends Controller
{
    public function getAddFriend()
    {
        return view('friends.add');
    }

    public function postAddFriend(FriendFormRequest $request)
    {
        $this->command('addFriend', \Request::input());
        return Response::make('Friend added!');
    }
}


	

Laravel Scheduler

Scheldule your artisans commands


$schedule->command('cache:clear')
     ->hourly()
     ->sendOutputTo($filePath)
     ->emailOutputTo('john@doe.com');

$schedule->command('foo')->dailyAt('15:00');

$schedule->command('foo')->thenPing($url);

$schedule->command('foo')->cron('* * * * *');

$schedule->call(function()
{
    // Do some task...

})->hourly();

	

Filesystem and cloud storage

Unified API for any filesystem


	  	$contents = Storage::get('file.jpg');

	Storage::put('file.jpg', $contents);

	Storage::delete('file.jpg');

	Storage::move('old/file1.jpg', 'new/file1.jpg');

	Storage::copy('old/file1.jpg', 'new/file1.jpg');

	$file = Storage::disk('s3')->get('file.jpg');

	

Eloquent Casting

Cast strings into specific types


/**
 * The attributes that should be casted to native types.
 *
 * @var array
 */
protected $casts = [
    'is_admin' => 'boolean',
    'friends' => 'array',
    'somethingHere' => 'integer',
    'somethingElse' => 'float',
];

	

Further Reading

That’s All Folks!