Laravel 2020 Logo
me@grafxflow

Written by me@grafxflow

13 Feb, 2020

0

4,797

Adding Global Custom Class with Facades in Laravel 5.8

Now and then you may want to use your own Custom Global Class anywhere in your Laravel app.

With this simple step by step guide we are going to add a custom class and call it in an example controller like the below example code.

CustomGlobalClass::customGlobalFunction()

So lets start with 'Step 1'.

Step 1

First create an example laravel app called 'exampleApp' using v5.8.

composer create-project --prefer-dist laravel/laravel exampleApp "5.8.*"

Now lets create 2 folders inside the app folder.

cd /exampleApp/app/

mkdir Facades

mkdir CustomClasses

Step 2

Now we need to create our example custom Class.

cd /exampleApp/app/CustomClasses  

sudo vim CustomGlobalClass.php

And add the following.

<?php

// app/CustomClasses/CustomGlobalClass.php

namespace App\CustomClasses;

class CustomGlobalClass
{
    public function customGlobalFunction()
    {
         echo "Test customGlobalFunction output...";
    }
}

Then save the file in your editor - here I am using the vim built in commands.

// Save vim command
:w

Step 3

Now we need to create a Facade file for our custom Class.

cd /exampleApp/app/Facades  

sudo vim CustomGlobalClassFacade.php

And add the following code.

<?php

// app/Facades/CustomGlobalClassFacade.php

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class CustomGlobalClassFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'CustomGlobalClass';
    }
}

Then save the file in your editor.

:w

Step 4

Now we want to create our custom Service provider for the Class.

cd  /exampleApp/app/Providers

php artisan make:provider CustomGlobalClassesServiceProvider

sudo vim CustomGlobalClassesServiceProvider.php

And then add the following making sure to also include the CustomClass being called.

<?php

// app/Providers/CustomGlobalClassesServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use App\CustomClasses\CustomGlobalClass;

class CustomGlobalClassesServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
        $this->app->bind('CustomGlobalClass', function () {
            return new CustomGlobalClass();
        });
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

Then again save the file in your editor.

:w

Step 5

Now open the config app to add the provider and facade.

cd  /exampleApp/config 

sudo vim app.php

Then add these in their appropriate sections.

<?php

// config/app.php

'providers' => [
    ...
    App\Providers\CustomGlobalClassesServiceProvider::class,
];

'aliases' => [
    ...
    'CustomGlobalClass' => App\Facades\CustomGlobalClassFacade::class,
];

Save it.

// Save vim command
:w

Step 6

Create an example Controller to use the custom class with.

cd  /exampleApp 

php artisan make:controller ExampleController

cd /exampleApp/app/Http/Controllers

sudo vim ExampleController.php

And add the following - it contains 2 examples of usage.

<?php

// app/Http/Controllers/ExampleController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

// Use for Example 1.
use CustomGlobalClass;

class ExampleController extends Controller
{
    //
    public function getGlobalClass() {

        // Example 1. Call our custom Class
        CustomGlobalClass::customGlobalFunction();

        // Example 2. or without the need for 'use CustomGlobalClass;' in the code
        \CustomGlobalClass::customGlobalFunction();
    }
}

Again save the file.

// Save vim command
:w

Step 7

Now lets add our controller to a web route.

cd  /exampleApp/routes

sudo vim web.php

And add the following.


// routes/web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', 'ExampleController@getGlobalClass');

Save the file.

// Save vim command
:w

Now start up the server.

cd /exampleApp

php artisan serve

Laravel development server started: <http://127.0.0.1:8000>

Now open your browser http://127.0.0.1:8000 and see if this is output.

Test customGlobalFunction output...

Hope this is handy.

Add comment

Smart Search

133 Following
50 Followers

me@grafxflow

Hull, United Kingdom

I am a Full-stack Developer who also started delving into the world of UX/UI Design a few years back. I blog and tweet to hopefully share a little bit of knowledge that can help others around the web. Thanks for stopping by!

Follow