Laravel is een opensource PHP-framework waarmee webapplicaties kunnen worden ontwikkeld. Achter Laravel staat een uitgebreide community en er is uitgebreide documentatie. Niet voor niets is het naast Symfony en Yii een van de meest populaire PHP-frameworks van dit moment. Woensdag is versie 5.5 van Laravel uitgekomen, een long term support-uitgave, en de release notes daarvan zien er als volgt uit:
Laravel 5.5 is the Next LTS Release
Laravel 5.5 is the next long term support (LTS) version of Laravel (the last being 5.1). LTS versions receive bug fixes for two years, and security fixes for three years.
General minor releases receive bug fixes for six months and security fixes for one year.Whoops Package
You might recall the filp/whoops package from Laravel v4 provided elegant stack traces for your most frustrating debugging moments. The whoops package returns to Laravel 5.5!Collection Dumping
Another great debugging feature (that I’ve seen previously as a package or macro) is collection dumping methods. Read our collection dumping post for more details.Exception Rendering
Exceptions now can render a response if they define a public “response” method. Typically, in earlier versions of Laravel you might add a check to the
App\Exceptions\Handler::render()method, and conditionally send back a response based on the exception type.
You can also implement theThe Responsable Interface
Responsableinterface in your exception classes, and Laravel will respond automatically.
The Responsable interface is another response addition to laravel that we’ve covered at Laravel news. A class implementing the interface can be returned from a controller method; the router now checks for an instance of
Responsablewhen preparing the response from
In this simple example, you could automatically respond with JSON if you make a request via AJAX, and by default response with a redirect theRequest Validation Method
In past versions of Laravel you would pass the request instance to theAnother nice benefit from this style of calling validation is that the return value acts like
$this->validate()method in a controller. Now, you can just call validate on the request object.
Request::only(), returning only the keys provided in the validation call. Returning only the validated keys is an excellent convention to use, avoiding
Custom Validation Rule Objects and Closures
My favorite feature in Laravel 5.5 is hands-down the new custom validation rule objects and closures. Creating a custom rule object is an excellent alternative to creating custom rules with Validator::extend (which you can still use), because it’s more clear where the rule logic is located at a glance.
The closure style takes the attribute and value, and a fail parameter that you call if the validation rule should fail. The closure is a nice way to experiment with custom validation before you extract it to a dedicated rule object, or for one-off custom validation needs.
To create custom validation rule objects, you can use the newWe have a dedicated post to custom validation rules here on Laravel News, be sure to check it out!
Auth and Guest Blade Directives
We have written about Blade::if() directives in 5.5. A few new conditional directives in 5.5 areFrontend Presets
When you are starting a new project, Laravel 5.5 provides Vue.js scaffolding by default. In Laravel 5.5 you can now pick from a few presets and remove all frontend scaffolding with the “preset” Artisan command in Laravel 5.5.
If you look at the help, you can see that it allows you to pick “none,” “bootstrap,” “vue”, or “react”.Separate Factory Files
Factory files were previously defined in one ModelFactory.php file. Now, you create different files for each model. You can create a factory file when you are creating a new model. You can also create a factory file directly with “make:factory”.The migrate:fresh Migration Command
The new “migrate:fresh” migration command 5.5 is a nice addition to creating a clean database in development. The
migrate:freshcommand drops all the database tables and then runs the migrations.
You might be familiar with the existingThe
migrate:refreshcommand, which rolls back migrations and then reruns them. Usually in development you want to just drop the tables, getting a fresh database, and running migrations.
On the testing front, the RefreshDatabase trait is the new way to migrate databases during tests. This new trait takes the most optimal approach to migrating your test database depending on if you are using an in-memory database or a traditional database. TheThe
DatabaseMigrationstraits are still available in 5.5, allowing you to upgrade without using the new RefreshDatabase trait.
The base test case inherits a methodAutomatic Package Discovery
withoutExceptionHandling(), which allows you to disable exception handling for a test. Disabling exception handling allows you to catch the exception in your test and assert the exception instead of the exception handler responding. It’s also a useful debugging tool when your test is doing something you don’t expect, and you want to see the actual exception.
The last feature we are going to look at is automatic package discovery. While Laravel packages aren’t usually hard to install, the package detection feature means you don’t have to set up providers or aliases. You can disable auto-discovery for specific packages.
De video die je probeert te bekijken is niet langer beschikbaar op Tweakers.net.