This is the fifth day of my participation in the August Wen Challenge.More challenges in August

Before, I wrote an article about Laravel improving the efficiency of DB query. After forwarding it to the group, some people questioned me and said, “Laravel is the framework he used several years ago, I didn’t expect that there are still people using it now.”

Nani, what do you mean? Don’t forget PHP is the best language!

Personally, Laravel is a very elegant development framework: elegant design patterns, powerful feature implementations, easy extensions, constant release updates, and most importantly, the best technology development community to date, in my opinion.

I have to make a Call for Laravel.

Laravel released version 8.0 on September 8, 2020. Laravel is scheduled to release version 9.0 on January 25, 2022.

Here I would like to introduce the new features of the latest Laravel release (8.0) :

Laravel 8 with the introduction of Laravel Jetstream, model factory classes, Migration compression, queue batching, improved rate limiting, queue improvements, dynamic Blade components, Tailwind paging view, Time test Assistant, artisan Serve improvements, Improvements to the event listener, along with various other bug fixes and usability improvements, continue to improve Laravel 7.x.

Today we will focus on dynamic Blade components, event listener optimization, and event test assistant

Dynamic Blade component

Sometimes we need to render a component, but are not sure which component should be rendered at run time. In this case, we can now use Laravel’s built-in dynamic-Component to dynamically render components based on a value or a variable at runtime:

<x-dynamic-component :component="$componentName" class="mt-4" />
Copy the code

Event listener optimization

You can now simply register a closure based Event listener by passing a closure function to the Event:: Listen method. Laravel checks the closure to determine the type of event handled by the listener.

use App\Events\PodcastProcessed;
use Illuminate\Support\Facades\Event;

Event::listen(function (PodcastProcessed $event)     {
    //
});
Copy the code

In addition, closure based event listeners can now be marked as Queueable using the Illumate\Events\Queueable function:

use App\Events\PodcastProcessed;
use function Illuminate\Events\queueable;
use Illuminate\Support\Facades\Event;

Event::listen(queueable(function (PodcastProcessed $event) {
    //
}));
Copy the code

Like queue tasks, we can customize the execution of queue listeners using the onConnection, onQueue, and Delay methods:

Event::listen(queueable(function (PodcastProcessed $event) {
    //
})->onConnection('redis')->onQueue('podcasts')->delay(now()->addSeconds(10)));
Copy the code

If we want to handle listener failures for anonymous queues, we can provide a closure for the catch method when we define queueable listeners:

use App\Events\PodcastProcessed;
use function Illuminate\Events\queueable;
use Illuminate\Support\Facades\Event;
use Throwable;

Event::listen(queueable(function (PodcastProcessed $event) {
    //
})->catch(function (PodcastProcessed $event, Throwable $e) {
    // The queued listener failed...
}));
Copy the code

Time test assistant

When testing, we may sometimes need to modify the time returned by functions such as now or Illuminate\Support\Carbon::now(). Laravel’s basic functional test class now includes time test helper functions that we can use to manipulate the current time:

Public function testtimecanbeererhave () { $this->travel(5)->milliseconds(); $this->travel(5)->seconds(); $this->travel(5)->minutes(); $this->travel(5)->hours(); $this->travel(5)->days(); $this->travel(5)->weeks(); $this->travel(5)->years(); // Time travel to the past... $this->travel(-5)->hours(); // Go to definite time... $this->travelTo(now()->subHours(6)); // Return to the current time... $this->travelBack(); }Copy the code

Gorgeous dividing line

For more on Laravel, check out my column: Server Development from Beginner to Master

Recommended reading

  1. I’m still working on Laravel? Don’t forget PHP is the best language. (1) How does Laravel elegantly set global variables

  2. I’m still working on Laravel? Don’t forget PHP is the best language. (2) Laravel Jetstream and model Factory classes

  3. I’m still working on Laravel? Don’t forget PHP is the best language. (3) Migration compression, queue batch processing, improve rate limit

  4. I’m still working on Laravel? Don’t forget PHP is the best language. (4) Optimization of maintenance mode

Last but not least

Technical group please come here. Or add my wechat account wangzhongyang0601 to learn together.