After long time waiting, Laravel 5.5 LTS has officially released today.
This release contains many features and updates to the framework, for example:
Exception Rendering
Instead of providing conditional check for each exception and make an appropriate call to App\Exceptions\Handler::render()
, in Laravel 5.5, you can just provide render()
method inside your YourAnyException
class to tell what to do for the response.
<?php
namespace App\Exceptions;
class WrongIdFormatException extends \Exception
{
/**
* @param \Illuminate\Http\Request $request
*/
public function render($request)
{
return response()->json(['message' => 'Wrong ID format']);
}
}
Custom response class with Responsable interface
In previous versions, you need to use Response
facade or response()
helper to response to requests. However, to make it easier and more dynamic, Laravel 5.5 provides a simple Responsable
interfact for response. It’s basically like this:
<?php
namespace App\Http\Responses;
use App\Models\Employee;
use Illuminate\Contracts\Support\Responsable;
class ShowEmployeeViewResponse implements Responsable
{
/**
* @var \App\Models\Employee
*/
protected $employee;
/**
* @param \App\Models\Employee $e
*/
public function __construct(Employee $e)
{
$this->employee = $e;
}
/**
* This method will be called to response via controller.
*/
public function toResponse($request)
{
return view('pages.employee.show', ['employee' => $this->employee]);
}
}
Usage is very simple, just return the Responsable
instance from controller.
class EmployeeController extends Controller
{
public function show(Employee $e)
{
return new ShowEmployeeViewResponse($e);
}
}
New Blade directives: @auth, @guest
Often, to check if user is authenticated or not in Blade, this is how we do it.
@if(auth()->check())
render view for authenticated user
@endif
@if(auth()->guest())
render view for guest user
@endif
It doesn’t look quite fluent on Blade rendering, so Laravel 5.5 provides too new directives to handle this condition check @auth
and @guest
, so things look simpler.
@auth
render view for authenticated user
@endauth
@guest
render view for guest user
@endguest
Of course, you can create your own custom Blade directives with Blade::directive()
as usual.
…
There are more features and updates on Laravel 5.5 LTS version. You should visit Laravel 5.5 documentation to explore more of them or quickly have a look at the release notes.
To upgrade to Laravel 5.5 LTS, please follow the official guide.