Last Updated:

Creating your library in Laravel

In this article, I will describe the process of creating your own library in Laravel 5.
 

Since this is a lesson that should demonstrate the process of creating your own library, we will dispense with the complexity and agree that our library will return only a line ("Work").
  1. Navigate to the app folder of your framework.
  2. create a new library folder in it
  3. navigate to the library folder and create a new helpers folder again in the library folder
  4. Now in this folder you need to create a file with the name and extension .wws.php

In total, having done this manipulation, the created file will have the following directory:


The contents of the wws file.php will be as simple as possible:


Now in the file autoload_classmap.php it is necessary to initialize our class.
This file can be found here:


Last line, before closing parenthesis and semicolon ); you need to add the line:


For quick access to our library, you need to create an alias.
Navigate to the config:

Open app folder.php

Search for the "aliases" key.

And add at the end of this line:


The list of aliases will look something like this:
app/library/Helpers/wws.php
<?php
namespace App\library\Helpers;

class wws
{
    public static function test()
    {
        return exit('work');
    }
}
vendor/composer/autoload_classmap.php
'Helpers\\wws' => $baseDir . '/app/library/Helpers/wws.php',
app/config
'wws' => App\library\Helpers\wws::class,

 
    'aliases' => [

        'App'       => Illuminate\Support\Facades\App::class,
        'Artisan'   => Illuminate\Support\Facades\Artisan::class,
        'Auth'      => Illuminate\Support\Facades\Auth::class,
        'Blade'     => Illuminate\Support\Facades\Blade::class,
        'Cache'     => Illuminate\Support\Facades\Cache::class,
        'Config'    => Illuminate\Support\Facades\Config::class,
        'Cookie'    => Illuminate\Support\Facades\Cookie::class,
        'Crypt'     => Illuminate\Support\Facades\Crypt::class,
        'DB'        => Illuminate\Support\Facades\DB::class,
        'Eloquent'  => Illuminate\Database\Eloquent\Model::class,
        'Event'     => Illuminate\Support\Facades\Event::class,
        'File'      => Illuminate\Support\Facades\File::class,
        'Gate'      => Illuminate\Support\Facades\Gate::class,
        'Hash'      => Illuminate\Support\Facades\Hash::class,
        'Lang'      => Illuminate\Support\Facades\Lang::class,
        'Log'       => Illuminate\Support\Facades\Log::class,
        'Mail'      => Illuminate\Support\Facades\Mail::class,
        'Password'  => Illuminate\Support\Facades\Password::class,
        'Queue'     => Illuminate\Support\Facades\Queue::class,
        'Redirect'  => Illuminate\Support\Facades\Redirect::class,
        'Redis'     => Illuminate\Support\Facades\Redis::class,
        'Request'   => Illuminate\Support\Facades\Request::class,
        'Response'  => Illuminate\Support\Facades\Response::class,
        'Route'     => Illuminate\Support\Facades\Route::class,
        'Schema'    => Illuminate\Support\Facades\Schema::class,
        'Session'   => Illuminate\Support\Facades\Session::class,
        'Storage'   => Illuminate\Support\Facades\Storage::class,
        'URL'       => Illuminate\Support\Facades\URL::class,
        'Validator' => Illuminate\Support\Facades\Validator::class,
        'View'      => Illuminate\Support\Facades\View::class,
        'wws' => App\library\Helpers\wws::class,
    ],


This completes the creation of your own library!
Now to call the library function test() you need to register a line anywhere in your web application:

wws::test();