LARAVEL 5.0


Whats new?


  • New directory structure
  • Method injection
  • Route caching
  • Annotations
  • Middleware

NEW directory structure


NEW DIRECTORY STRUCTURE

Default namespacing -- yay!



$ php artisan app:name Acme 


Method injection

The "old" way
<?php
namespace Something\Awesome;

class UserController extends BaseController
{
    public function __construct(
        UserRepository $userRepository, 
        Authenticator $authenticator
    ) {
        $this->userRepository = $userRepository;
        $this->authenticator = $authenticator;
    }

    public function getAll() {...}
    public function isLoggedIn() {...}
}


METHOD INJECTION

The "new" way

<?php
namespace Something\Awesome;

class UserController extends BaseController
{
    public function getAll(UserRepository $userRepo..) {...}
    public function isLoggedIn(Authenticator $authen..) {...}
}

ROUTE CACHING

New artisan command
$ php artisan route:cache 
serializes the results of routes.php file,
store in a file on the system.

The upside
The application gets much faster:
This is primarily useful on applications with 100+ routes and typically makes 
this portion of your code 50x faster.
The downside
any changes to routes.php? re-cache!

Route caching

Tip
Only enable route caching in a production environment.

Fx. only run
$ php artisan route:cache 
as a post-deploy hook in Git.

annotations - routes

Like we know them from fx. phpunit

@codeCoverageIgnore
@expectedException

etc.
<?php 
namespace App\Http\Controllers;

class AwesomeController 
{
    /**
     * @Get("/awesome-sauce/{id}", as="sauce")
     */
    public function sauce($id) {...}

}

ANNOTATIONS - ROUTES

Add your controller to a $scan array in App/Providers/RouteServiceProvider.php

Run
$ php artisan route:scan 
and routes will be saved in a routes.scanned.php file.

fancy.

ANNOTATIONS - EVENTS

Same shit, different a**hole.

<?php 
namespace App;

class Intercom
{
    /**
     * @Hears("user.signup")
     */
    public function addUser(User $user)
    {
        return $this->apiWrapper->sendSomeAddThing(
            $user->email,
            $user->name
        );
    }
}

Annotations - events

Add to a $scan array in App/Providers/EventServiceProvider.php
and run
$ php artisan event:scan 
the event will now be bound to a class & method in events.scanned.php

ANNOTATIONS

  1. You like it / You don't like it
  2. Framework-specific
  3. Only double quotes allowed

MIDDLEWARE

Decorating and filtering HTTP requests/responses

MIDDLEWARE

StackPHP Onion
Decorating requests/responses
(decorator pattern)

MIDDLEWARE




Bedre kendt som 
before & after filter

MIDDLEWARE

"before"
...
class BeforeMiddleware implements Middleware {

    public function handle($request, Closure $next)
    {
        // Do Stuff
        return $next($request);
    }

}
operates and then passes on the request

MIDDLEWARE

"after"
...
class AfterMiddleware implements Middleware {

    public function handle($request, Closure $next)
    {
        $response = $next($request);
        // Do stuff
        return $response;
    }

}
process the request and then operate on it

OTHER COOL STUFF

Laravel 5.0 uses PHP dotenv, a 3rd party lib 

that loads from a single .env file.


Laravel 5.0 ships with a default .env.example file,
which looks something like this:
APP_ENV=local
APP_KEY=SomeRandomString
DB_USERNAME=homestead
DB_PASSWORD=homestead
$env = $app->detectEnvironment(function()
{
    return getenv('APP_ENV') ?: 'production';
});

OTHER COOL STUFF


Validation into request objects/classes,
extending
 Illuminate\Foundation\Http\FormRequest


  • method injecting validation
  • automatically validates data
  • automatically sends HTTP redirect

OTHER COOL STUFF

Cloud file drivers / multiple filesystems

Should you upgrade?

You can still maintain your old structure if using 
Illuminate\Foundation\Providers\LegacyStructureServiceProvider.php 
and deleting a few new service providers.

LARAVEL 5.0

By tbsatordbogendotcom

LARAVEL 5.0

Introduction to what the new version of Laravel brings.

  • 127