0
votes

This is how I create helper (App\Helpers\Settings.php)

namespace App\Helpers;

use Illuminate\Database\Eloquent\Model;

class Settings {

    protected $settings = [];

    public function __construct() {

        $this->settings['AppName'] = 'Test';
    }

    /**
     * Fetch all values
     *
     * @return mixed
     */
    public function getAll () {
        return $this->settings;
    }
}

Creating facade (App\Helpers\Facades\SettingsFacade.php)

namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Settings extends Facade {

    protected static function getFacadeAccessor() {
        return 'Settings';
    }
}

Creating Service Provider (App\Providers\SettingsServiceProvider.php)

namespace App\Providers;

use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;

class SettingsServiceProvider extends ServiceProvider {

    /**
     * Bootstrap the application events.
     *
     * @return void
     */
    public function boot() {
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register() {
        App::bind( 'Settings', function () {
            return new \App\Helpers\Settings;
        });
    }  */
}

Registering provider (App\Providers\SettingsServiceProvider::class)

Creating alias: 'Settings' => App\Facades\Settings::class

Running composer dump-autoload

Trying to use facade Settings::getAll();

Getting error Class 'App\Http\Controllers\Settings' not found

Can’t figure out why I cannot create facade and getting that error

1
it's because of namespaces, try: \Settings::getAll();RainDev
Class 'App\Facades\Settings' not foundAndrew Skochelias
rename App\Helpers\Facades\SettingsFacade.php to App\Helpers\Facades\Settings.php helps to call call \Settings::getAll(); but how do I call like Settings::getAll();Andrew Skochelias
if you wan't to call it like Settings::getAll(); you need to add use Settings at the top of file.RainDev

1 Answers

0
votes

try this one.

App\Helpers\Settings.php

namespace App\Helpers;
use Illuminate\Database\Eloquent\Model;
    class Settings {

        protected $settings = [];

        public function __construct() {

            $this->settings['AppName'] = 'Test';
        }

        /**
         * Fetch all values
         *
         * @return mixed
         */
        public function getAll () {
            return $this->settings;
        }
    }

App/Http/Controllers/XyzController.php

use Facades\App\Settings;
class XyzController extends Controller
{
     public function showView()
     {
         return Settings::getAll();
     }
}

web.php

Route::get('/','XyzController@showView');

use Facades\App\Helpers\Settings;

Route::get('/direct',function() {
    return Settings::getAll();
});

use laravel Real time facades