Web Programming TutorialsNew Features For Laravel 5.7 Version

New Features For Laravel 5.7 Version

Every web application framework of PHP has its own version history and it is always being updated and maintained which is accessible in official documentation. Every latest version brings new functionality and features which are either changed or deprecated, so it is important that a developer should have knowledge of which version will be suitable for your projects.

Laravel framework includes two active versions as given below:

  • Laravel 4- released in May 2013
  • Laravel 5- released in February 2015
  • Laravel 5.7 – released in September 2018

Laravel 5 also includes various releases with the latest version of Laravel 5.5 which includes all the robust features for web development. The roadmap of Laravel of the version release is shown in the snapshot below:

Laravel release process

The following points are important to be noted in the context of understanding the release process of Laravel:

  • The old directory of app/models is completely removed with Laravel 5.
  • All the controllers, middleware and requests are grouped within a directory under the app/Http folder.
  • A new folder namely Providers directory is replaced with the app/start files with respect to the previous versions of Laravel 4.x.
  • All the language files and views are kept in the resource directory.
  • New artisan command route:cache is used for registration of new routes for the routinh procedure and is included with the release of Laravel 5 and further versions.
  • Laravel supports HTTP middleware and includes CSRF tokens for the procedure of authentication and authorization.
  • All the authentication models and logic are located under one directory namely resources/views/auth. It includes various modules such as user registration, authentication and password controllers.

Laravel Releases

Version

Release

Bug Fixes Until

Security Fixes Until

V1

June 2011

V2

September 2011

v3

February 2012

v4

May 2013

5.0

Feb 4th, 2015

Aug 4th, 2015

Feb 4th, 2016

5.1 (LTS)

Jun 9th, 2015

Jun 9th, 2017

Jun 9th, 2018

5.2

Dec 21st, 2015

Jun 21st, 2016

Dec 21st, 2016

5.3

Aug 23rd, 2016

Feb 23rd, 2017

Aug 23rd, 2017

5.4

Jan 24th, 2017

Jul 24th, 2017

Jan 24th, 2018

5.5 (LTS)

Aug 30th, 2017

Aug 30th, 2019

Aug 30th, 2020

5.6

Feb 7th, 2018

Aug 7th, 2018

Feb 7th, 2019

5.7

Sep 4, 2018

Feb 4th, 2019

Sep 4th, 2019

The highlighted version marks the latest release.

Guest User Gates:

This feature is add-on feature from the latest version 5.7 which was released in September 2018. This feature is used to initiate the authorization process for specific users. In Laravea l 5.6, there was procedure where it used to return false for unauthenticated users. In Laravel 5.7, we can allow guests to go authorization checks by using the specific nullable type hint within the specified controller within the specified controller is given below:

<?php

Gate::define('view-post', function (?User $user) {
    // Guests 
});

Explanation:

By using a nullable type hint the $user variable will be null when a guest user is passed to the gate, and you can then make decisions about authorizing the action. If you allow nullable types and return true, then the guest will have authorization. If you don’t use a nullable type hint, guests will automatically get this beautiful 403 response for Laravel 5.7 which is displayed below:

image output

The difference between 403 and 404 error is that 404 is displayed when user tries to access the unknown resource or URL and 403 error as mentioned in the snapshot above is displayed if unauthorized user accesses the website.

Artisan Commands:

Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below:

class ArtisanCommandTest extends TestCase
{
    public function testBasicTest()
    {
        $this->artisan('nova:create', [
            'name' => 'My New Admin panel'
        ])
            ->expectsQuestion('Please enter your API key', 'apiKeySecret')
            ->expectsOutput('Authenticating...')
            ->expectsQuestion('Please select a version', 'v1.0')
            ->expectsOutput('Installing...')
            ->expectsQuestion('Do you want to compile the assets?', 'yes')
            ->expectsOutput('Compiling assets...')
            ->assertExitCode(0);
    }
}

Explanation:

1. Here a new class named “ArtisanCommandTest” is created under test cases module.
2. It includes a basic function of testBasicTest which includes various functionalities of assertions.
3. The artisan command of expectsQuestion includes two attributes. One with question and other with an apiKeySecret. Here, the artisan validates the apiKeySecret and verifies the input sent my user.
4. The same scenario applies for question “Please select a version” where a user is expected to mention a specific version.

Pagination Customizations:

Laravel includes a feature of pagination which helps a user or a developer to include a pagination feature. Laravel paginator is integrated with the query builder and Eloquent ORM. The paginate method automatically takes care of setting the required limit and the defined offset. It accepts only one parameter to paginate i.e. the number of items to be displayed in one page.

Laravel 5.7 includes a new pagination method to customize the number of pages on each side of the paginator. The new method no longer needs a custom pagination view.

The custom pagination view code demonstration is mentioned below:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show all of the users for the application.
     *
     * @return Response
     */
    public function index()
    {
        $users = DB::table('users')->paginate(15);

        return view('user.index', ['users' => $users]);
    }
}

The new pagination customization as per Laravel standards is mentioned below:

<?php

User::paginate(10)->onEachSide(5);

onEachSide refers to the subdivision of each pagination records with 10 and subdivision of 5.

Dump Server:

Laravel dump server comes with the version of Laravel 5.7. The previous versions do not include any dump server. Dump server will be a development dependency in laravel/laravel composer file.

With release of version 5.7, you’ll get this command which includes a concept out-of-the-box which allows user to dump data to the console or an HTML file instead of to the browser. The command execution is mentioned below:

php artisan dump-server

# Or send the output to an HTML file
php artisan dump-server --format=html > dump.html

Explanation:

The command runs a server in the background which helps in collection of data which is sent from the application that sends the output through the console. When the command is not running in the foreground, the dump() function is expected to work by default.

Callable Actions:

Laravel 5.7 introduces a new feature called “callable action URL”. This feature is same like the one in Laravel 5.6 which accepts string in action method. The main purpose of the new syntax introduced Laravel 5.7 is to directly enable you access the controller.

The syntax used in Laravel 5.6 version:

<?php

$url = action('UserController@profile', ['id' => 1]);

The similar action called in Laravel 5.7 is mentioned below:

<?php

$url = action([PostsController::class, 'index']);

One benefit of the new callable array syntax format is the feature of ability to navigate to the controller directly if a developer uses a text editor or IDE that supports code navigation.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -