Laravel Digest (October 2013)

Welcome to the second instalment of Laravel Digest, the series where I give a regular rundown of important changes, fixes and additions to Laravel’s master branch. Below are the changes up to the end of October 2013:

  • Blade braces can now be escaped using @ which is useful for people using clientside templating languages that also use the braces (cb93bf3):

The following code will output hello

1
{{ 'hello' }}

The following code will output {{ $variable }}

1
@{{ $variable }}

Use case, using handlebars.js:

1
<div class="name">@{{ user.name }}</div>
  • Queued cookies – because cookies can only be attached to a request, using Cookie::set() before the request was instantiated previous lost the cookie (or simply wasn’t allowed). Now Cookie::queue() can be used to queue cookies for eventual sending with the request.

  • Route::controller is back and appears slightly cleverer than before in that it first seeks out all callable methods on the given controller and adds each route individually, then sets up a ‘default’ route for anything it can’t detect (98c14a9)

    • Route::controllers can also be used to hook multiple controllers up in one go by passing an associative array of uri to controller FQCN
  • App::env() can now be used to check the environment against a list provided (array or multiple arguments) (66e5a0b)

  • Remote tail command (artisan tail) allows tailing of a remote log file (assuming the remote connection is set up correctly in the config and the remote project uses a single log file) (25b1cac)

    • As a consequence of this work, the logging system was simplified down to a single log file by default (no daily rotation and no SAPI name, this looks like it may generate rather large log files to me, I’d have preferred to keep daily rotation) (laravel/laravel@088f4b6)
    • Also as a consequence, the live debugging through sockets was removed. Now the tail command will tail a remote log file if there’s a remote connection specified, or tail the local log file if there’s no remote connection specified (f62d1a7/763a0265)
  • New Router::input() method that allows you to get the value of a named route parameter (28e36bb)

  • Controller filters can now be added as controller functions using the @ syntax as a shorthand (see issue 2432) (d0e0c63):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

class UserController extends Contoller
{
    public function __construct()
    {
        // before:
        $this->beforeFilter(function ($route) {
            return $this->hasPermission($route);
        }, ['only' => 'edit']);

        // after, shorthand
        $this->beforeFilter('@hasPermission', ['only' => 'edit']);
    }

    public function hasPermission($route)
    {
        $id = $route->getParameter('id');

        if (! Auth::user()->ownsPost($id)) {
            return Response::make('You are not authorised to edit this post', 401);
        }
    }
}
  • Can now pass DateTime and Carbon instances to Cache method that accept a duration (in this case, the time difference between now and the time passed is calculated in minutes) (977f803)

  • Now uses Stack for middleware (multiple commits). Repercussions:

    • close application hook is now deprecated in favour of using a stack middleware (18e1aec)
    • Presumably more…
  • Should now use Input::cookie for cookies, not Cookie::get (e0fe79e removed Cookie::get() and 58ec2fe for temporary deprecation code in the facade)

  • If using MySQL, UPDATE queries can now have orderBy and limit specified (d9d61e1)

  • As with overriding packages’ config and views, translations can now be overridden using a similar convention (5483042)

    • It should be noted that the convention is not the same: it is not app/lang/packages/vendor/package/ but uses the namespace (i.e. package name) only, like when referencing views/config (i.e. View::make('namespace:path.view') and Config:get('namespace:config.key'))

    • Files for a package vendor/package in locale en should go in app/lang/packages/en/package/ — see how it’s just package not vendor/package like it would be for config and views

    • So to reiterate, for a package called vendor/package:

      • Views: View::make('package::some/view')app/views/packages/vendor/package/some/view{.blade}.php
      • Config: Config::get('package::some.key')app/config/packages/vendor/package/some.php, then key key
      • Translations: Lang::get('package::some.key')app/lang/packages/en/package/some.php, then key key
  • You can now use Auth::viaRemember() to determine whether the logged-in user was authorised through the ‘remember me’ cookie or not (2184ad4)

  • New bindShared method on the IoC container which should now be used in place of the old share syntax (07233b3):

1
2
3
4
5
6
7
8
9
// this
$this->app['my.key'] = $this->app->share(function ($app) {
    return new MyClass();
});

// becomes this
$this->app->bindShared('my.key', function ($app) {
    return new MyClass();
});
  • ServiceProvider no longer uses very weird logic to guess a package’s namespace. This probably won’t affect many people but I’m pointing it out as it has affected me and it’s good to know for future reference (6089525)

  • You can now pass a view to Paginator::links([$view]) in order to override the default view for that call (9d1150c)

  • New hasManyThrough relation type (0925d86)

    • There’s no real documentation on this yet and the tests are a little cryptic, so I’ll talk more about this in the next update.
  • Validator::make can now take a fourth parameter that specified the custom parameters for the validator. Presumably to be used by custom validators. (93977e4)

  • IoC container old resolving method is now renamed to resolvingAny and now there’s a new resolving method in its place that accepts an abstract object type to only be fired when that abstract type is being resolved (aa6df0a)

  • Any alaises bound to a given IoC key will now be removed when rebinding the key (8f240f8)

  • Queue workers will no no longer run if the application is in maintenance mode (a8cdb68)