Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Publishing Assets

I'm using Laravel 5 and I want to publish twitter bootstrap CSS and JS to public directory. I did used Composer to get twitter/bootstrap package and that part went OK, so files I want are now in vendor/twitter/bootstrap/dist

But I can't get it to work. I get 'Nothing to publish.' message every time I try.
Here are the steps I took:

  • use artisan to generate class: php artisan make:provider BootstrapServiceProvider

  • edit the boot() method in that class

  • register service provider in config/app.php
    providers => [ ...
    'App\Providers\BootstrapServiceProvider',
    ],

Bellow is my generated file with the class and it's edited boot() method.

BootstrapServiceProvider.php

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class BootstrapServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     * Move twitter bootstrap CSS and JS into public directory
     *
     * @return void
     */
    public function boot()
    {
        $this->publishes([
                     __DIR__ . 'vendor/twitter/bootstrap/dist' => public_path('vendor/bootstrap'),
        ], 'public');
    }

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

}

I have tried:

php artisan vendor:publish --tag=public
php artisan vendor:publish --tag=public --force
php artisan vendor:publish
php artisan vendor:publish --force

And also tried with removed second 'public' parameter from boot() method.

Every time I get Nothing to publish..

boot() method doesn't even get called. What do I have to do to get it to working?
Maybe put something in the register() method? Right now is just empty.
Or some other step?

like image 639
toni rmc Avatar asked Apr 04 '15 07:04

toni rmc


People also ask

How do I publish a package to Packagist?

To submit a package to Packagist, first create an account open in new window and then use the Submit link open in new window and specify the public repository URL to the git repository of your package. Packagist will now host a so-called dev-master version of your package.

What is publishing in Laravel?

The vendor:publish command is used to publish any assets that are available from third-party vendor packages. It provides a few options to help specifically choose which assets should be published. The following table lists and describes each of the options that are available for use: Option. Description.


2 Answers

To answer my own question.

After reinstalling Laravel from 5.0.26 to 5.0.25 everything worked.
Even after updating it again to 5.0.26 and higher it still works.

I don't know why it didn't work before composer update.

Everything else is the same as above except boot() method has to be fixed to point to the right vendor path when calling publishes().

Fixed version of boot():

public function boot()
{
    $this->publishes([
        __DIR__ . '/../../vendor/twitter/bootstrap/dist' => public_path('vendor/bootstrap'),
    ], 'public');
}

To automate it all add this entries in composer.json in scripts: section under "post-install-cmd" and "post-update-cmd":

"scripts": {
    "post-install-cmd": [
          "...",
          "php artisan vendor:publish --tag=public --force"
    ],
    "post-update-cmd": [
          "...",
          "php artisan vendor:publish --tag=public --force"
    ],
},
like image 153
toni rmc Avatar answered Oct 11 '22 17:10

toni rmc


How to install Bootstrap in Laravel using composer

Although it has been a year since this question was posted, I believe my response is going to help someone out there. This works perfectly for me.

I am using Laravel Framework version 5.2.31 (do php artisan --version to check your Laravel version). Follow the steps below:

  1. Open the terminal and run composer require twbs/bootstrap 3.3.6. If you want a different version from 3.3.6, check the latest distribution version on https://packagist.org/packages/twbs/bootstrap.
  2. Create a Bootstrap service provider class using artisan command: php artisan make:provider BootstrapServiceProvider
  3. Open your Bootstrap service provider class in app/Providers/BootstrapServiceProvider.php. Edit the boot() method as follows:

    public function boot() { $this->publishes([base_path('vendor/twbs/bootstrap/dist') => public_path('vendor/bootstrap')],'public'); }

    The code above will instruct artisan to copy directory vendor/twbs/bootstrap/dist to public/vendor/bootstrap.

  4. register service provider in config/app.php by adding DealerPortal\Providers\BootstrapServiceProvider::class, below the list of Application Service Providers

  5. Run vendor:publish --tag=public --force to complete this task.

Bonus step:

If you want to automate the process of publishing Bootstrap all add php artisan vendor:publish --tag=public --force to post-install-cmd and post-update-cmd section of your composer.json file.

like image 29
ceekays Avatar answered Oct 11 '22 17:10

ceekays