Muhammad Asfund
Muhammad Asfund

Reputation: 97

How to create Facades in Laravel 5.0 without using composer?

Thanks to all in advance.

I am trying to create Facades for my custom and common functions in laravel 5.0 also I don`t want to create controller for that so I am using Facades.

I have tried almost every tutorial but it do not help me.

Please help me to create facade without using Composer in Laravel 5.0.

Thanks again.

Upvotes: 0

Views: 110

Answers (1)

Filip Koblański
Filip Koblański

Reputation: 10008

First of all you're creating a class of facade like this:

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class SomeFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'someService';
    }
}

then You create a service class that hold your functionalities:

namespace App\Services;

class SomeService { ... }

Finally you have to register it and set an alias (not required) for it:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProivider extends ServiceProvider
{
    (...)
    public function register()
    {
        $this->app->singleton('someService', function () {
            return new \App\Services\SomeService();
        });
        $this->app->alias('SomeServiceFacade', \App\Facades\SomeFacade::class);
    }
}

Now you can call your methods from SomeService with:

SomeServiceFacade::someMethhod();

or

app('someService')->someMethhod();

Upvotes: 2

Related Questions