___andrewk___
7 months ago

How can i include params on all blades?

Posted 7 months ago by ___andrewk___

im trying to generate a list of css/js files on all blades. I have create an array in config file assets.php

<?php

return [
    'css'   => [
        '/assets/vendor/fontawesome/css/font-awesome.min.css',
    ],
    'js'    => [

    ]
];

i have also created a controller to manage these assets:

<?php

namespace App\Http\Controllers;

class AssetsController extends Controller
{
    protected $css = [];
    protected $js = [];

    public function __construct()
    {
        $this->css = Config('assets.css');
        $this->js = Config('assets.js');
    }

    public static function addCss($cssSrc = '') {
        $this->css[] = $cssSrc;
    }

    public static function addJs($jsSrc = '') {
        $this->js[] = $jsSrc;
    }

    public static function renderCss() {
        $css = '';
        foreach ($this->css as $link) {
            $css .= '<link rel="stylesheet" href="' . $link . '">
';
        }
        return $css;
    }

    public static function renderJs() {
        $js = '';
        foreach ($this->css as $link) {
            $js .= '<script src="' . $link . '"></script>';
        }
        return $js;
    }
}

and im trying to use AppServiceProvider to pass the it to each view

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Http\Controllers;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer('*', function($view) {
            $view->with('css', AssetsController::renderCss());
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

im new to laravel so i appreciate help and any suggestion on maybe a better way of doing this. for now im just getting an error that the class is not recognized:

 Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Class 'App\Providers\AssetsController' not found

Please sign in or create an account to participate in this conversation.