Today was the entrance of the new version of the PHP framework Laravel - 5.4! The new version added features such as support for Markdown markup for generating emails and notifications, improved support for Redis, added new features for Blade template engine, and many others. But first things first.
With Markdown support, Laravel can now generate beautiful and responsive HTML templates for emails, as well as generate a plain text version. For example, now your code for generating a letter might look like this:
@component('mail::message') # Order Shipped Your order has been shipped! @component('mail::button', ['url' => $url]) View Order @endcomponent Next Steps: - Track Your Order On Our Website - Pre-Sign For Delivery Thanks,<br> {{ config('app.name') }} @endcomponent
It will look like this:
Laravel Dusk is a new utility for end-to-end application testing. By default, to use it does not require the installation of JDK or Selenium. Instead, Dusk uses the standalone version of ChromeDrive, but you can also use any other driver that is compatible with Selenium.
Since Dusk uses a real browser for testing, you can easily test and interact with applications that use JavaScript:
/** * A basic browser test example. * * @return void */ public function testBasicExample() { $user = factory(User::class)->create([ 'email' => 'taylor@laravel.com', ]); $this->browse(function ($browser) use ($user) { $browser->loginAs($user) ->visit('/home') ->press('Create Playlist') ->whenAvailable('.playlist-modal', function ($modal) { $modal->type('name', 'My Playlist') ->press('Create'); }); $browser->waitForText('Playlist Created'); }); }
Using the new features of the Blade template engine - components and slots - allows you to do everything that you used to do with sections and templates. However, for some developers, the component-slot model may seem much easier to understand. To begin with, imagine that we have an "alert" component that we want to use in our application in all templates:
<!-- /resources/views/alert.blade.php --> <div class="alert alert-danger"> {{ $slot }} </div>
The variable {{ $slot }}
will contain the content that we want to pass to the component. To declare this component, we use the new @component @component
:
@component('alert') <strong>Whoops!</strong> Something went wrong! @endcomponent
Named slots allow you to use multiple slots in one component:
<!-- /resources/views/alert.blade.php --> <div class="alert alert-danger"> <div class="alert-title">{{ $title }}</div> {{ $slot }} </div>
They are passed to the component using the @slot
directive. Any content outside this directive will be passed to the component as $slot
variable:
@component('alert') @slot('title') Forbidden @endslot You are not allowed to access this resource! @endcomponent
As well as HTTP routes, routes for channels of wiretapping of events can now also use implicit binding to the model. For example, instead of a string or numeric ID, you can pass an instance of the Order model (class):
use App\Order; Broadcast::channel('order.{order}', function ($user, Order $order) { return $user->id === $order->user_id; });
The collection now supports higher order messages that allow you to perform common operations with collections. The following messages are now available:
Each message can be accessed as a dynamic property of a collection instance. For example, use the message each
to apply a method to each object within the collection:
$users = User::where('votes', '>', 500)->get(); $users->each->markAsVip();
Similarly, you can use sum
to, for example, count the number of votes in a collection with users:
$users = User::where('group', 'Development')->get(); return $users->sum->votes;
Event handlers in Eloquent can now be bound to event objects. This makes it much easier to handle Eloquent events, and also makes testing these events easier. To use this innovation, you need to declare the $events
property in the class of your model, which will allow you to use your own classes to handle basic Eloquent events:
<?php namespace App; use App\Events\UserSaved; use App\Events\UserDeleted; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * The event map for the model. * * @var array */ protected $events = [ 'saved' => UserSaved::class, 'deleted' => UserDeleted::class, ]; }
Previously, the "retry" and "timeout" settings were set globally for all command line processes. In Laravel 5.4, these settings can now be set for each process individually, by defining them directly in the process class:
<?php namespace App\Jobs; class ProcessPodcast implements ShouldQueue { /** * The number of times the job may be attempted. * * @var int */ public $tries = 5; /** * The number of seconds the job can run before timing out. * * @var int */ public $timeout = 120; }
In Laravel 5.4, two new middlewares (query filters) appeared: TrimStrings
and TrimStrings
/** * The application's global HTTP middleware stack. * * These middleware are run during every request to your application. * * @var array */ protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, ];
These two filters automatically remove spaces at the beginning and end of values, and also convert empty lines to null
, which allows you to normalize the data of each query and forget about the constant use of the trim
function in your code.
Previously, only Laravel services supplied by default provided facades that made it possible to quickly and succinctly use their methods through the service container. In Laravel 5.4, it was possible to easily convert any of your classes into a facade in real time. Imagine that we have the following class:
<?php namespace App\Services; class PaymentGateway { protected $tax; /** * Create a new payment gateway instance. * * @param TaxCalculator $tax * @return void */ public function __construct(TaxCalculator $tax) { $this->tax = $tax; } /** * Pay the given amount. * * @param int $amount * @return void */ public function pay($amount) { // Pay an amount... } }
To make it a facade, you just need to add the Facades prefix:
use Facades\ { App\Services\PaymentGateway }; Route::get('/pay/{amount}', function ($amount) { PaymentGateway::pay($amount); });
You can also write interaction tests using the facade simulation function:
PaymentGateway::shouldReceive('pay')->with('100');
In Laravel 5.3, all the models of the "connecting" tables for the "many-to-many" links used the same instance of the built-in Pivot
model. In Laravel 5.4, you can define your model for a pivot table after a link is declared as follows:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Role extends Model { /** * The users that belong to the role. */ public function users() { return $this->belongsToMany('App\User')->using('App\UserRole'); } }
Previously, it was not possible to define Redis connections to the same host and clusters in the same application. With Laravel 5.4, you can define multiple connections to hosts and clusters. More information in the documentation for Redis .
In Laravel 5.4, utf8mb4 is used as the default encoding that supports emoji storage in the database. If you are upgrading from Laravel 5.3, you do not need to change the encoding.
If you still manually change it or use MySQL versions earlier than 5.7.7, you need to manually configure the default string lengths that generate migrations using the Schema::defaultStringLength
in your AppServiceProvider
:
use Illuminate\Support\Facades\Schema; /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); }
In version 5.4, the ability to use JSON format for compiling language files in applications was added. They are still stored in the resources/lang
directory.
For example, if you have a Russian translation of your project, you can create ru.json
in the resources/lang
directory, and its contents might look something like this:
{ "I love Laravel!": " Laravel!" }
Source: https://habr.com/ru/post/320010/
All Articles