New Laravel 5.5 Features: Recap from Laracon US 2017
This is a series of posts on New Features in Laravel 5.5.
!Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.
Timeline: releasing August-ish; around Eu
Frontend presets
php artisan preset react
; default React componentsphp artisan preset none
; axis & blank Sass
New routing options
Route::view('/welcome', 'welcome')
returns welcome view- Works with route caching!
Route::redirect('home', 'dashboard')
returns redirect to another URI
Blade::if
- Simple creation of conditional Blade directives
- e.g.
Blade::if('public', function () { return app()->context()->isPublic(); });
- Generates:
@public
/@endpublic
Renderable mailable
- Return a mailable from a route to preview it
Route::get('preview', function () {
return MyMailable;
});
Renderable Exceptions
report()
method on exceptions define how to report itrender()
method on exceptions define how to render it
class MyException extends Exception
{
public function report()
{
// send wherever
}
public function render()
{
return view('error-or-whatever');
}
}
Responsable interface
- Implement Responsable interface; class must provide
toResponse()
method which shows how to convert this object to a response
class Thing implements Responsable
{
public function toResponse()
{
return 'This is a great response! ' . $this->id;
}
}
One-off Notifications
(a.k.a. Anonymous notifications)
// Easy way to notify people who aren't in your system as Notifiable
Notification::route('mail', 'taylor@laravel.com')
->notify(new App\Notifications\NotifyThingHappened);
Validation improvements
- Return validated data from
$this->validate()
- Custom validation "rule" classes;
php artisan make:rule
passes()
returns boolean; receives name and valuemessage()
returns error message if needed
Use:
$this->validate([
'myfield' => [
'string',
'required',
new App\Rules\MyValidationRule
]
);
TrustedProxy package brought internally
If you have a proxy in front of your app like a CloudFlare proxy or something else that provides the SSL which then at your proxy, Laravel currently cannot correctly detect that it's an HTTP request. It's getting requests from port 80 instead of port 443.
TrustProxies Middleware, extending Fideloper's TrustedProxy, teaches the system how to trust the proxy that their forwarded headers (including those identifying that it's SSL) are trustworthy.
$proxies
property on the middleware allows you to define which proxies are trusted.
Migrate:fresh
- Ignores the
down
migrations and just wipes the whole database before re-up
ing.
New database migration trait
- Previously we used
DatabaseMigrations
andDatabaseTransactions
; new combined way of doing it - Inspired by Adam Wathan (of course); migrates once at the start of your tests and then runs every test after that with transactions
- Benefit: We don't have to remember to migrate every time (solved by
DatabaseMigrations
) but it's faster because we're not re-migrating every time (solved byDatabaseTransactions
); now both in one world - New trait: named
RefreshDatabase
// Usage
class MyTest extends TestCase
{
use RefreshDatabase;
}
WithoutExceptionHandling
middleware
- If doing integration testing, in the past, exceptions would just get swallowed in your tests.
- Disabling exception handling allows exceptions to bubble up to PHPUnit so you can see what's going wrong
- Avoids integration calls failing silently and being only fixable via log tailing
Dusk improvements
- Headless by default—faster and doesn't pop up a browser every time
Package auto-discovery
- Inspired/originally created by Dries Vints of Laravel.io
- No longer have to register your service provider in the
config/app.php
provider array; now, set a block in each package'scomposer.json
that teaches Laravel to discover it without any manual work php artisan package:discover
runs the discovery process- "Most of the time I install a package I just do composer require packagename and then I'm good to go. Don't have to do anything else"
Vendor:publish
menu
- Nice readout of all the packages and tags you could pick instead of having to manually pass in providers every time
Auto-registered console commands
- No longer have to put commands in the
app/Console/Kernel.php
commands array - Instead, Laravel looks for all classes that are commands every time you try to run a command and finds it for you
Job chaining
- Dispatch a job and then queue up other jobs to be queued up after that one is completed
dispatch((new App\Jobs\PerformTask)->chain([
new App\Jobs\AnotherTask,
new App\Jobs\FinalTask($post)
]));
Better missing model handling in jobs
- If you have a job that's referencing a model and that model disappears before the job is handled, the job will fail out instantly if the model does not exist
- Previously it would try and try forever
- If you set
$deleteWhenMissingModels
to true it just deletes itself without even failing
Comments? I'm @stauffermatt on Twitter
Tags: laravel • laravel 5.5 • laracon
This is part of a series of posts on New Features in Laravel 5.5:
-
Jul 25, 2017 | laravel, laravel 5.5, laracon
-
Jul 25, 2017 | laravel, horizon, queues