phplaravel

How to create aliases in laravel?


I want to create aliases in laravel like Auth so that i can use it in view just like

Auth::user().

For example, i want to return data from my setting table and want to use it like

Setting::method()->value. in view and use Setting in controllers.

What should i create for this Facades Or Service Providers? Provide me some procedures. I tried using Service Providers but i am confused how to call database there.


Solution

  • This will be a long answer, using Laravel 5.4.

    1. You need a service class (in this case, it's a concrete class behind the facade). I made it inside app/Services folder:

    namespace App\Services;
    
    use Illuminate\Database\DatabaseManager;
    
    class Setting
    {
        protected $db;
    
        public function __construct(DatabaseManager $db)
        {
            $this->db = $db;
        }
    
        public function method()
        {
            return;
        }
    }
    

    As you see, I inject the database inside that concrete class. This is the magic of Laravel IoC. It will automatically resolve any dependency into your concrete class.

    1. Next, create a facade. I made it inside app/Facades folder:

    namespace App\Facades;
    
    use Illuminate\Support\Facades\Facade;
    
    class Setting extends Facade
    {
        /**
         * Get the registered name of the component.
         *
         * @return string
         */
        protected static function getFacadeAccessor()
        {
            return 'setting';
        }
    }
    

    Notice the setting string returned from getFacadeAccessor. You need to register this service name later to the Container (IoC). The magic behind this facade is, it's automatically call (proxy) any static method to the instance method of concrete class, in this case you can call:

    Service::method()
    
    1. Register the service to the container. Inside your AppServiceProvider

    namespace App\Providers;
    
    use App\Services\Setting;
    use Illuminate\Support\ServiceProvider;
    
    class AppServiceProvider extends ServiceProvider
    {
        // ...
    
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            // ...
    
            $this->app->singleton(Setting::class);
            $this->app->alias(Setting::class, 'setting');
        }
    }
    

    In this Service Provider, I declare that Setting service is a singleton service. Which means, you can use Setting service in another place without re-initialize class, in another words you are using the same instance across file. Last, I tell the container that Setting service has another alias, name setting, so Container can figure out which Concrete Class behind App/Facade/Setting.

    1. For aliasing, as mentioned by apokryfos. Register your facade alias inside config/app.php, find the section aliases and add this line:

    'aliases' => [
    
        // ...
    
        'Setting' => App\Facades\Setting::class,
    
    ],
    

    After this you can call your facade like this:

    use Setting;
    
    Setting::method();
    

    Hope this helps.