What's New features and improvements in Laravel 7
Laravel 7 released officially on 3rd March 2020 and this is not the LTS update so it will provide 6-month bug fixes until 3rd September 2020.
Laravel 7 came with lot of new features and previous fixes.
Below are the list of new features that you will get in Laravel 7 :
- Laravel Airlock
- Better routing speed
- Blade Component Tags & Improvements
- Fluent string operations
- Zttp for HTTP Client
- New artisan commands
- Multiple Mail driver
- CORS support
With this new Airlock feature in Laravel 7, you can implement featherweight authentication system for mobile applications, single page applications and simple token based APIs applications. You can generate multiple API tokens for an user's account with this Airlock feature.
Better routing speedWith Laravel 7, you will get 2x faster speed than Laravel 6 using cached routes. You can also bind your model in your route as this is another best feature with Laravel 7 which is defined as Route Model binding.
By default, it works with id field but we can customize it :
use App\Post; Route::get('posts/{post:slug}', function (Post $post) { return $post; });Blade Component Tags & Improvements
Laravel 7 allows us to define our own components and use them in our blade files.
Define an Alert component in following path App/View/Components
<?php namespace App\View\Components; use Illuminate\View\Component; class Alert extends Component { /** * The alert type. * * @var string */ public $type; /** * Create the component instance. * * @param string $type * @return void */ public function __construct($type) { $this->type = $type; } /** * Get the class for the given alert type. * * @return string */ public function classForType() { return $this->type == 'success' ? 'alert-success' : 'alert-warning'; } /** * Get the view / contents that represent the component. * * @return \Illuminate\View\View|string */ public function render() { return view('components.alert'); } }
Now we will define component's Blade template.
<!-- /resources/views/components/alert.blade.php --> <div class="alert {{ $classForType }}" {{ $attributes }}> {{ $heading }} {{ $slot }} </div>
Now you can render it in another Blade view using the component's tag:
<x-alert type="error" class="mb-4"> <x-slot name="heading"> Alert content... </x-slot> Default slot content... </x-alert>
Please consult the full Blade component documentation to learn about this feature.
Fluent string operationsLaravel 7 now provides a more object oriented stuff and fluent string manipulation library.
Earlier llluminate\Support\str
class was there but now you can create a fluent llluminate\Support\Striangable
object using the Str::of
method.
Multiple method can be chained in order to perform string manipulation.
return (string) Str::of(' Laravel Framework 6.x ') ->trim() ->replace('6.x', '7.x') ->slug();
Sometime, you need to call 3rd party API endpoints in your application so in that case it will help you to request to API endpoints.
Zttp is a Guzzle wrapper which provides a much simple and nicer syntax with REST method:
use Illuminate\Support\Facades\Http; $response = Http::post('url', [ 'name' => 'ExpertPHP', ]); echo $response['foo']; $response->body() $response->json() $response->status() $response->ok() $response->successful() (>= 200 && < 300) $response->serverError() $response->clientError()
$response = Http::withHeaders(['X-API-KEY' => 'xxxxx'])->post('url', [ 'name' => 'ExpertPHP', ]);
If you want to authenticate token based API then :
$response = Http::withToken('token')->post('url', [ 'name' => 'ExpertPHP', ]);
You can also use basic authentication like :
$response = Http::withBasicAuth('username', 'password')->post('url', [ 'name' => 'ExpertPHP', ]);
You can handle error in following way:
if (! $response->successful()) { $response->throw(); }
Laravel 7 has added a new artisan command which is :
php artisan testMultiple Mail Drivers
With Laravel 7, you can now configure multiple Mail drivers in a single application. Each mailer configured within the mail configuration file may have its own unique transport and its own options in order to allow applications to use different email services to send specific email.
By default, Laravel will use default mailer which is configured in your mail configuration file if there are multiple then you can use mailer method to send email messages.
Mail::mailer('postmark') ->to($request->user()) ->send(new OrderShipped($order));
Laravel 7 now supports CORS (Cross-Origin Resource Sharing) which automatically respond your OPTION request with configured value and you can configure all CORS settings in your CORS configuration file but OPTIONS request will automatically be handled by the HandleCors middleware.
For more information about CORS support with Laravel 7, you can go through with its Documentation