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 ReleaseLaravel 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 PackageYou 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 DumpingAnother 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 RenderingExceptions 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 the
The Responsable InterfaceResponsable
interface 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
Responsable
when preparing the response fromIlluminate\Routing\Router
.
In this simple example, you could automatically respond with JSON if you make a request via AJAX, and by default response with a redirect the
Request Validation Methodsongs.show
route.In past versions of Laravel you would pass the request instance to the
Another 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, avoidingRequest::all()
.
Custom Validation Rule Objects and ClosuresMy 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 new
We have a dedicated post to custom validation rules here on Laravel News, be sure to check it out!make:rule
command.
Auth and Guest Blade DirectivesWe have written about Blade::if() directives in 5.5. A few new conditional directives in 5.5 are
Frontend Presets@auth
and@guest
.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 FilesFactory 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 CommandThe new “migrate:fresh” migration command 5.5 is a nice addition to creating a clean database in development. The
migrate:fresh
command drops all the database tables and then runs the migrations.You might be familiar with the existing
Themigrate:refresh
command, 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.RefreshDatabase
TraitOn 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. The
TheDatabaseTransactions
andDatabaseMigrations
traits are still available in 5.5, allowing you to upgrade without using the new RefreshDatabase trait.withoutExceptionHandling()
methodThe base test case inherits a method
Automatic Package DiscoverywithoutExceptionHandling()
, 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.
Learn more about this feature from the Taylor Otwell’s article on this feature, and on our post.