Top 10 packages for extending Laravel

30,394

Laravel is a free and open source PHP framework for creating web applications. At the time of writing, it is undoubtedly the most popular PHP framework. It offers a lot of features such as authentication, routing, model-view-controller architecture, eloquent and much more.

In this article, we will list down the PHP libraries that we can use to extend Laravel. You will be able to add them to your Laravel project very easily. These packages provide great functionality that you will be required to add in various projects.

Laravel Debugbar

Laravel Debugbar is a package that allows you to add a developer toolbar to your Laravel application for debugging purposes. Debugbar is updated for Laravel 5, so you can use it easily in your projects. Debugbar gives you a lot of options such as showing debug messages, exceptions, currently used view and its partials, currently used route and all of the database queries that your application does.

// Messages
Debugbar::info($info);
Debugbar::warning($warning);
Debugbar::error($error);

<span class="hljs-comment">// Measure render time.</span>
start_measure(<span class="hljs-string">'render'</span>,<span class="hljs-string">'Rendering time'</span>);
stop_measure(<span class="hljs-string">'render'</span>);
add_measure(<span class="hljs-string">'now'</span>, LARAVEL_START, microtime(<span class="hljs-keyword">true</span>));

//Exceptions
try{
    throw new Exception('Some Exception');
} catch(Exception $e) {
    Debugbar::addException($e);
}

Laravel User Verification

This package allows you to handle user verification and validates email. It generates and stores a verification token for the registered user, sends or queue an email with the verification token link, handles the token verification, set the user as verified. This package also provides an optional isVerified route middleware.

public function register(Request $request)
{
    $this->validator($request->all())->validate();

    $user = $this->create($request->all());

    event(new Registered($user));

    $this->guard()->login($user);

    UserVerification::generate($user);

    UserVerification::send($user, 'My Custom E-mail Subject');

    return $this->registered($request, $user)
        ?: redirect($this->redirectPath());
}

Laravel Mix

Laravel Mix provides a clean and rich API for defining webpack build steps for your project. Mix supports several common CSS and JavaScript pre-processors that can be chained together to build your assets through webpack. You can also use Laravel mix in a non-laravel application.

mix.js(['resources/assets/js/app.js', 'resources/assets/js/custom.js'], 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');

Laravel IDE Helper

This package does not extend Laravel in any way but it is very useful when developing Laravel applications with IDE’s like PhpStorm. These IDE’s do not type-hint Laravel facades. This package generates a file that your IDE can understand, so it can provide auto-completion. It generates a file based on the files in your project, so it is always up-to-date.

// PhpDoc generation for Laravel facads.
php artisan ide-helper:generate

// Configuring your composer.json to generate and optimize after commit
"scripts":{
    "post-update-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postUpdate",
        "php artisan ide-helper:generate",
        "php artisan ide-helper:meta",
        "php artisan optimize"
    ]
},

Eloquent Sluggable

A slug is a URL-friendly version of a string. Slugging involves converting letters to one case, removing non-URL friendly characters ( accented letters, ampersands) and replacing spaces with hyphens. Output string can then be used as an identifier for a particular resource. This URL is also SEO-friendly. By using this package you can easily create slugs for your models in Laravel.

<?php

namespace App;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Sluggable;

    protected $fillable = ['title'];

    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}

$post = new Post([
    'title' => "A random Hamza's post"
]);

// $post->slug = "a-random-hamzas-post"

Laravel Backup

This Laravel package creates a backup of all of your files in your application. It creates a zip file that contains all files in the directories you specify along with a dump of your database. Backup can be stored on any file-system. To create a backup all you need to do is to run the following command:

php artisan backup:run

Laravel Generators Extended

A library by Jeffrey Way that extends the core file generators that are included in Laravel 5. Generators can save you a lot of time when developing your project. It allows you to quickly set up new views, seeds, and much more.

Artisan View

This package allows you to create views in your Laravel application through artisan. It adds a number of artisan commands for working with views.

# Create a view 'admin.blade.php' in the default directory
php artisan make:view admin

# Create a view 'admin.blade.php' in a subdirectory 'auth'
php artisan make:view auth.admin

# Create a view with different file extension ('admin.html')
php artisan make:view admin --extension=html

# Extend an existing view
php artisan make:view admin --extends=master

# Add multiple sections to the view
php artisan make:view admin --section=title --section=content

Socialite

Socialite provides a very fluent interface to handle OAuth authentication with Facebook, Twitter, Google, LinkedIn, Github and Bitbucket. It handles almost all of the boilerplate for social authentication.

$user = Socialite::driver('github')->user();
// OAuth Two Providers
$token = $user->token;
$refreshToken = $user->refreshToken; // not always provided
$expiresIn = $user->expiresIn;

// All Providers
$user->getId();
$user->getNickname();
$user->getName();
$user->getEmail();
$user->getAvatar();

No Captcha

No Captcha is a package for implementing Google reCaptcha validation and protecting forms from spamming. First, you need to obtain a free API key from reCaptcha.

// prevent validation error on captcha
NoCaptcha::shouldReceive('verifyResponse')
    ->once()
    ->andReturn(true);

// POST request, with request body including g-recaptcha-response
$response = $this->json('POST', '/register', [
    'g-recaptcha-response' => '1',
    'name' => 'John',
    'email' => 'john@example.com',
    'password' => '123456',
    'password_confirmation' => '123456',
]);

Laravel Permissions

This package allows you to manage user permissions and roles in a database. It is a Laravel 5 package for working with role-based permissions to your project.

use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;

    // ...
}
You might also like
Comments