"how to create custom facade in laravel 4" Code Answer

2

first you need to go to app/config/app.php and in providers section add:

'laracmsproviderssettingsserviceprovider',

in the same file in aliases section you should add:

 'settings' => 'laracmsfacadessettings',

in your app/laracms/providers you should create file settingsserviceprovider.php

<?php

namespace laracmsproviders;

use illuminatesupportserviceprovider;

class settingsserviceprovider extends serviceprovider {

    public function register()
    {
        $this->app->bind('settings', function()
            {
                return new laracmssettings();
            });
    }

}

in your app/laracms/facades/ you should create file settings.php:

<?php

namespace laracmsfacades;

use illuminatesupportfacadesfacade;

class settings extends facade {

    protected static function getfacadeaccessor() { return 'settings'; }

}

now in your app/laracms directory you should create file settings.php:

<?php

namespace laracms;

class settings {
   public function get() {echo "get"; }

   public function set() {echo "set"; }
}

as you wanted to have your files in custom folder laracms you need to add this folder to your composer.json (if you used standard app/models folder you wouldn't need to add anything to this file). so now open composer.json file and in section autoload -> classmap you should add app/laracms so this section of composer.json could look like this:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/testcase.php",
        "app/laracms"
    ]
},

now you need to run in your console inside your project foler:

composer dump-autoload

to create class map

if everything is fine, you should now be able to use in your applications settings::get() and settings:set()

you need to notice that i used folders with uppercases because namespaces by convention starts with upper letters.

By Andreas Jung on March 29 2022

Answers related to “how to create custom facade in laravel 4”

Only authorized users can answer the Search term. Please sign in first, or register a free account.