How to create custom Facade in Laravel 4

后端 未结 2 1749
一生所求
一生所求 2021-01-02 22:46

Looked up a few tutorials on facades and laravel 4... tried some... not liked the way they work.

For instance, they don\'t all provide a way of defining where to sto

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 23:09

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

    'Laracms\Providers\SettingsServiceProvider',
    

    In the same file in aliases section you should add:

     'Settings' => 'Laracms\Facades\Settings',
    

    In your app/Laracms/Providers you should create file SettingsServiceProvider.php

    app->bind('settings', function()
                {
                    return new \Laracms\Settings();
                });
        }
    
    }
    

    In your app/Laracms/Facades/ you should create file Settings.php:

    Now in your app/Laracms directory you should create file Settings.php:

    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.

提交回复
热议问题