A
A
Andrej Kopp2021-04-16 15:14:43
Laravel
Andrej Kopp, 2021-04-16 15:14:43

How to correctly specify the public folder from vendor in the service provider of your package?

Hello. Started developing my package. Set up a symlink to my package in composer.json (which is at the root):

"repositories": {
        "package-name": {
            "type": "path" ,
            "url": "packages/sequelone/sone" ,
            "options": {
                "symlink": true
            }
        }
    },

Then, in the folder with the package along the path /packages/sequelone/sone/src , the service provider SoneServiceProvider.php added the loading of routes and everything else to the boot() function:
namespace Sequelone\Sone;

use Illuminate\Support\ServiceProvider;

class SoneServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->publishes([
            __DIR__.'/../config/sone.php' => config_path('sone.php'),
        ], 'config');
        
        $this->mergeConfigFrom(
            __DIR__.'/../config/sone.php', 'sone'
        );
        
        $this->loadRoutesFrom(__DIR__.'/../routes/web.php');

        $this->loadViewsFrom(__DIR__.'/../resources/views/', 'sone');
        
        $this->publishes([
            __DIR__ . '/../../views' => base_path('resources/views/vendor/sone')
        ]);
        
        $this->publishes([
            __DIR__ . '/../../vendor/sequelone/sone/public' => public_path('vendor/sone'),
        ], 'public');
        
        $this->publishes([
            __DIR__.'/../public' => public_path('/'),
        ], 'public');
    }
}

only here, by default, the public folder (which does not want to be loaded in the package, the files have a status of 404 errors).

As I understand it, you need to configure this:
$this->publishes([
            __DIR__ . '/../../vendor/sequelone/sone/public' => public_path('vendor/sone'),
], 'public');

PS If you execute the command:
php artisan vendor:publish --tag=public --force
then the content is published to the main public folder. How can I still configure this path so that files are given from the vendor folder?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrej Kopp, 2021-04-22
@sequelone

Apparently nothing will work without publications in the main folder. The solution is this.
In the service provider of your package, write the path to publication:

$this->publishes([
            __DIR__ . '/../public' => public_path('vendor/sone'),
        ], 'Sone-public');

The public folder of the package contains all files and folders (for example: /img/, /js/, /css/).
To automate all this, add the following entries in composer.json to the scripts section: " post-install-cmd " and " post-update-cmd " sections:
"scripts": {
    "post-install-cmd": [
          "...",
          "php artisan vendor:publish --tag=public --force"
    ],
    "post-update-cmd": [
          "...",
          "php artisan vendor:publish --tag=public --force"
    ],
},

As you can see from the names of the sections, one is performed when installing a package through Composer, and the other when updating. Logically, other artisan commands can be added there to automate processes.

S
Sergey delphinpro, 2021-04-16
@delphinpro

Have you looked here?
https://laravel.demiart.ru/package-development/
https://laravel.demiart.ru/build-your-own-laravel-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question