I'm using Laravel to build a package which will include some Admin functionality and will include some other packages.
I have included a package which has migrations and a config file, But how do I copy those to the correct folder in Laravel. The package has it's own ServiceProvider but How do I call that from my package?
I'm using the following to register the package in my package.
class CldServiceProvider extends ServiceProvider {
public function register()
{
$this->app->register(MediaLibraryServiceProvider::class);
}
}
I've tried
php artisan vendor:publish
But it says there's nothing to publish. Because it's only looking at the packages for this Laravel installation, and not the nested packages.
In my case I found I needed to include MY package in the application I was working on. This step was not included in the original tutorial I followed.
So in composer.json in the main app:
"repositories": [
{
"type": "path",
"url": "packages/namespace/name",
"options": {
"symlink": true
}
}
],
"require": {
// ...
"namespace/name": "dev-master"
},
And then we run this command from main folder:
composer update
After that running php artisan vendor:publish will include the option to publish the sub packages.
NB: The source for this info is: https://laraveldaily.com/how-to-create-a-laravel-5-package-in-10-easy-steps/
Try to publish the config separately and use the force switch
php artisan vendor:publish --tag=config --force
As #scrubmx mentioned, you need to include the code that defines what can be published, though this code shouldn't really be in your own service provider, but rather the other package you're including. If it doesn't seem to have this code, it's best to list it as an issue on that package's repository or create a pull request to add it.
Create database/migrations folder in your package root folder. Then add publish details in boot method in your service provider.
final class MyPackageServiceProvider extends BaseServiceProvider
{
public function boot(): void
{
$this->publishes([
__DIR__ . '/../database/migrations/' => database_path('migrations/my-package'),
], 'my-package-migrations');
}
}
Second part could be like database_path('migrations') or database_path('migrations/my-package'). I advise using with subfolder because if there are many packages migrations folder grows too much.
The part of 'my-package-migrations' is our tag. And run publish command with tag.
php artisan vendor:publish --tag=my-package-migrations
If you are using the subfolder php artisan migrate command won't work because we need to specify to exact migration folder like this,
php artisan migrate --path=/database/migrations/my-package
Yes, this looks a little bit dirty but organizes cables. And don't forget the using composer dump-autoload. If the service provider is registered this solution works. Also, you can use this method for config but config folder does not support subfolders.
I think you have publish the config files yourself
$this->publishes([
'other-package/absolute/path/some-config.php' => config_path('my-package.php'),
]);
Solution
I included the /packages/ folder in my current project where I was working on.
The problem is that when including the package in my composer.json
It is now reachable from 2 places inside my project, in the vendor folder and in my packages folder. This was causing issue's
I moved my packages outside my current project, this fixed the problem.
Publish vendor migration :
php artisan vendor:publish --tag=migration
Publish vendor config :
php artisan vendor:publish --tag=config
Related
Wanted to install Laravel-Excel (Maatwebsite) package manually without composer, but I dont know how.
Why? Because I have a laravel project in a free hosting server setup by other guy, and I can only access using Filezilla to edit/download/upload the codes.
If only Filezilla allow a command prompt that could use "composer update", then it will be easier.
I got solution!
I cant use composer on my company because of secure network. But i can download zip form github and install it manual. The below is my example for HTMLPurifier:
download and extract library mews/purifier to vendor directory https://github.com/mewebstudio/Purifier
add below line in vendor/composer/autoload_psr4.php
This sentence will load all of file from vendor/mews/purifier/src and autoload in namespace Mews\Purifier\
'Mews\\Purifier\\' => array($vendorDir . '/mews/purifier/src'),
Sometime you need add library into autoload_namespaces.php intead of, please read in
https://getcomposer.org/doc/04-schema.md#autoload
You got Mews\Purifier\Facades\Purifier not found if public config before finish step 3
$ php artisan vendor:publish
--provider="Mews\Purifier\PurifierServiceProvider"
add below json in vendor/composer/installed.json
This for composer history, providers and aliases will be load in config/app/php for register new provider
{
"name": "mews/purifier",
"version": "v2.0.12",
"type": "library",
"extra": {
"laravel": {
"providers": [
"Mews\\Purifier\\PurifierServiceProvider"
],
"aliases": {
"Purifier": "Mews\\Purifier\\Facades\\Purifier"
}
}
},
"autoload": {
"psr-4": {
"Mews\\Purifier\\": "src/"
}
}
},
Now you run this config, then vendor/mews/purifier/config will be move to config folder
$ php artisan vendor:publish
--provider="Mews\Purifier\PurifierServiceProvider"
Add the package to the vendor folder. You can upload it using filezilla
Add a reference in \vendor\composer\autoload_namespaces.php
Add a reference in \vendor\composer\autoload_psr4.php
Source laravel.io
it's easy to do it by following this
download the package and set the files in app folder
YourProject/app/Laravel-Excel/
and then add the path to composer.json in autoload
"autoload": {
...
"classmap": [
"database/seeds",
"database/factories"
"app/Laravel-Excel"
],
...
},
Run the composer dump-autoload
the solution refs to this question referance answer
download the package locally and then upload the package folder (found under vendor) along with the updated composer.json
Depending on how strict the server is, you could SSH into your Server. But doing it locally then uploading the required files is usually the way to go.
You might need to run composer autodump if you don't wipe the cache.
If everything works on local environment then copy your package and composer folder to server which is located at vendor
upload \vendor\maatwebsite
copy \vendor\maatwebsite\excel\src\config\excel.php to \config\excel.php
I created folder name - Birds and in this folder i created class Birds.php then add this to composer.json,
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Birds\\": "Birds/",
}
},
To load i have to run - composer dump-autoload -o and that's fine for first time to take that folder, But then i created new interface class like this :
namespace Birds\Validator;
interface BadgeInterface
{
public function test();
}
Interface class not working until in run composer dump-autoload,
My question is why i need to run this every time ? i am using laravel as framework.
Thanks
That's just how composer works. Laravel is just a PHP framework, so it is not able to run composer or other tool all the time for you.
If you need to automatically add files all the time, you can just use cron or Laravel's schedule command. It will do all the work for you every 5 minutes, for example.
This is cause when you create any class composer folder autoload.php can't update . that cause you need to run it manually . it clear you when you install any package at last command there run is composer autoload to update all reference in autoload.php file
I can't use composer to handle my dependencies due to the corporate firewall. At the moment I'm trying to use Barry vd Heuvel's DomPDF wrapper for Laravel and tried to:
Download the zipfile from Github (master)
Updated composer.json (not sure if its needed, but did it anyway) and added "barryvdh/laravel-dompdf": "*" in the require container.
Create the folder structure: vendor/barryvdh/laravel-dompdf
Place all files from the package in there (config-folder, src-folder and the files .gitignore, composer.json and readme.md)
Add the service provider and facade in my app.php. Service provider is listed as Barryvdh\DomPDF\ServiceProvider::class and the facade is aliased like 'PDF' => Barryvdh\DomPDF\Facade::class
Ran composer dump-autoload
After refreshing the browser I'm getting Class 'Barryvdh\DomPDF\ServiceProvider' not found. I also tried to run php artisan cache:clear and php artisan dump-autoload but the last one fails over the fact it can't find Barryvdh\DomPDF\ServiceProvider.
What have I forgotten to do to make it work?
Update
I've tried the suggested answer from Wouter J and the composer.json now looks like:
..
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Barryvdh\\DomPDF\\": "vendor/barryvdh/laravel-dompdf/src"
},
..
I've verified if the composer dump-autoload had any effect but I think it had. Because the entry is now also listed in vendor/composer/autoload_psr4.php like:
return array(
// more entries
'Barryvdh\\DomPDF\\' => array($vendorDir . '/barryvdh/laravel-dompdf/src'),
'App\\' => array($baseDir . '/app'),
);
I believe at this point this is working, but the Facade isn't responding. When I try to call something like PDF::loadView(...) and I let PhpStorm import the class (vendor/barryvdh/laravel-dompdf/src/PDF.php) it throws an error I can't call the method loadView statically. According to the documentation I should be able to call it like this:
$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
But that results in Non-static method Barryvdh\DomPDF\PDF::loadView() should not be called statically, assuming $this from incompatible context on my end.
Suggestions?
Composer autoload still doesn't know anything about how to download the package. You have to configure autoloading like this:
{
"autoload": {
"psr-4": { "Namespace\\Of\\The\\Package\\": "vendor/the/package" }
}
}
I know this question is old, but since I don't see any solution, here is mine: I had exactly the same problem. Was able to solve by running composer dumpautoload on my localhost and then copied the folder 'vendor/composer' to the shared hosting without an SSH access. Hope this will help someone in similar situation.
The problem addresses that application don't know about DomPDF\ServiceProvidor or anything related to DomPDF because barryvdh/laravel-dompdf itself depends dpmpdf/dompdf package which is still missing and does not comes with barryvdh/laravel-dompdf
the package dompdf/dompdf also depends on several extension i.e.
PHP version 5.3.0 or higher
DOM extension
GD extension
MBString extension
php-font-lib
php-svg-lib
have a look at package here https://packagist.org/packages/dompdf/dompdf download this and put in your vendor directory then update your composer dumpautoload it should be working
I'm trying to create a custom package for Laravel 5.0 based on this tutorials
The folder structure and service providers are exactly same, but some how the Serviceprovider is not updating autoload_namespace.php.
I already added my service provider in app/config.php
'Walkswithme\Users\UsersServiceProvider',
In my root composer.json I have following code.
"psr-4": {
"App\\": "app/",
"Walkswithme\\Users\\": "packages/walkswithme/users/src"
}
Under my packages folder files struture is as follows.
walkswithme
users
src
models
controllers
views
UsersServiceProvider.php
routes.php
composer.json
I can't use Laravel 5.1 it requires php 5.5.9 , Other wise I can user artisan packager command.
The error is getting is as follows.
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Walkswithme\Users\UsersServiceProvider' not found
Any help will be appreciated last 3hrs I'm digging on it.
I solved it myself ,
I tried composer dumpautoload -o so it works for me.
Also some time needs composer clearcache too.
Hope it helps someone else..
For me it was
Class 'Jubeki\LaravelCodeStyle\ServiceProvider' not found
And I just run
composer require jubeki/laravel-code-style --dev
In another folder (not where my common composer.json is located)
Maybe it also can help someone :)
Just check the place of the new bundle installation folder.
I'm currently writing an install script for our platform, and it needs to be able to create a composer.json file, and then run Composer. I've done that and it works fine, for the most part. The issue I'm having is that one of our dependencies runs a task upon installation of every subsequent package (for copying over config yaml files to the main project's directory), using Composer's script option. So for instance, in the generated Composer.json file is a bit that looks like this:
"scripts": {
"post-package-install": [
"Super\\Cool\\Task::postInstall"
],
"pre-package-update": [
"Super\\Cool\\Task::preUpdate"
],
"post-package-update": [
"Super\\Cool\\Task::postUpdate"
]
},
If I delete the vendor folder and then run Composer manually within the directory, the events work fine, but running it from my install script leaves me with an error message of
Class Super\Cool\Task is not autoloadable, can not call post-package-install script.
My only feeling is that perhaps Composer is trying to run its autoloader from the location of within my script, rather than the location of the composer.json file, but that's just a hunch
Does anyone know a way around this? Or is this a bug within Composer?
Just an update, it turns out this was an issue that has already been fixed. Running composer self-update fixed it