简体   繁体   中英

How to add css and javascript files with custom laravel package

Hello every one I am working with laravel and developing my first package and I need to add some css and javascript files with my package can any one answer me what is the right way to do this task.

You cannot directly add the assets from Your Laravel Package for that you have publish() assets first.

In your Service Provider

/**
 *
 * @return void
 */
public function boot()
{
    $this->publishes([
        __DIR__.'/path/to/assets' => public_path('vendor/courier'),
    ], 'public');
}

Then use it in your views using asset()

Hope this helps

Whenever I write Laravel packages, I strive to keep the JavaScript inline.

When I can't do that, I will use the GitHub raw URL for the JavaScript/CSS asset and load that directly in the Laravel view.

That way whenever anyone uses my Laravel packages and forgets or doesn't know about doing the php artisan vendor:publish command (~85%), it still just 'works'.

As @laravel levaral suggested you need to publish assets first afterwards in your root directory you will find public folder. Inside public you can make 'js' and 'css' folders in which you can store those files.

In order to access your assets through out the whole laravel project you just need to call it from your blade file using asset()

For js file:- Eg:- <script src="{{ asset('js/app.js') }}"></script>

Likewise for css file:- Eg:- <link href="{{ asset('css/app.css') }}" rel="stylesheet">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM