Publishing custom package files using Laravel with composer - laravel

I have created new package and i want to avoid publish command separately and i need to include publish command inside package composer.json file. So when package is install it should move the files according to service provider path.
I tried below method but assets/files are not published.
"scripts": {
"post-install-cmd": [
"php artisan vendor:publish --provider=\"<vendorname>\\<packagename>\\<Serviceprovider>\" --tag=public --force",
"php artisan migrate"
],
"post-update-cmd": [
"php artisan vendor:publish --provider=\"<vendorname>\\<packagename>\\<Serviceprovider>\" --tag=public --force",
"php artisan migrate"
]
}
But it works fine when directly run the command like below
php artisan vendor:publish --provider="<vendorname>\<packagename>\<Serviceprovider>"
Please provide solution for it.

Did you try with below code manually. If it returns the thing you need, then you are on the right track. Else some typo is on command of script.
composer run-script post-install-cmd

Composer does not support automatically running scripts other than those at root level. This is somewhat contested, but doesn't look like it's going to change any time soon.
Your best bet is probably to manually run the command / instruct users to manually run the command in your readme: composer run-script post-install-cmd -d ./vendor/[name]/[package]

Maybe you should try to use # before command, and also I suggesting to you to try write command in "post-autoload-dump" section. It is work for me.
"scripts": {
"post-autoload-dump": [
"#php artisan vendor:publish --provider='<vendorname>\\<packagename>\\<Serviceprovider>' --tag=public --force",
"#php artisan migrate"
]
}

Related

Laravel 8 on App Engine: "Please provide a valid cache path"

I upgraded a Laravel project from version 7 to 8. When I attempt to deploy it on App Engine, it fails saying "Please provide a valid cache path":
Updating service [***]...failed.
ERROR: (gcloud.app.deploy) Error Response: [9] Cloud build [***] status: FAILURE
Error type: UNKNOWN
[...]
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover --ansi
In Compiler.php line 36:
Please provide a valid cache path.
Part of my composer.json:
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi",
"#php artisan vendor:publish --force --tag=livewire:assets --ansi"
],
"post-create-project-cmd": [
"#php artisan key:generate --ansi"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"#php artisan ide-helper:generate",
"#php artisan ide-helper:meta"
],
"post-install-cmd": [
"composer dump-autoload",
"php artisan config:clear",
"php artisan cache:clear",
"php artisan view:clear",
"php artisan cache:clear",
"php artisan optimize:clear"
]
A snippet from app.yaml:
env_variables:
APP_STORAGE: /tmp
VIEW_COMPILED_PATH: /tmp
APP_SERVICES_CACHE: /tmp/services.php
APP_PACKAGES_CACHE: /tmp/packages.php
APP_CONFIG_CACHE: /tmp/config.php
APP_ROUTES_CACHE: /tmp/routes.php
CACHE_DRIVER: database
SESSION_DRIVER: database
I do have /storage/framework/views folder along with the other standard folders under /storage as well as bootstrap/cache.
If I remove this line from composer.json (under "post-autoload-dump"):
"#php artisan vendor:publish --force --tag=livewire:assets --ansi"
I am able to deploy the app but it fails on pages that use Livewire components with the following error:
The /workspace/bootstrap/cache directory must be present and writable. (View: /workspace/resources/views/users/edit.blade.php)
ErrorException
in /workspace/vendor/livewire/livewire/src/LivewireComponentsFinder.php (line 58)
in /workspace/vendor/livewire/livewire/src/CompilerEngine.php -> handleViewException (line 41)
in /workspace/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php -> handleViewException (line 60)
in /workspace/vendor/livewire/livewire/src/LivewireViewCompilerEngine.php -> evaluatePath (line 36)
in /workspace/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php -> evaluatePath (line 61)
in /workspace/vendor/laravel/framework/src/Illuminate/View/View.php -> get (line 139)
This happens even though I added the following line to bootstrap/app.php:
$app->useStoragePath(env('APP_STORAGE', base_path() . '/storage'));
following the guide.
Prior to upgrading Laravel, I had no problems deploying the app on App Engine.
This is a really annoying error. It happened every now and then and I still haven't found a fix. I'm sure that there is something weird happening in Cloud Build, I just don't know what. Anyone have any ideas other that the mentioned above please share.
Edit
I believe the issue lies complied views path as explained here
Now, setting VIEW_COMPILED_PATH to /tmp is necessary for the app to run BUT, the error occurs during build. During build, the compiled views cache path is read not from app.yaml but from the config/view.php or maybe .env where the value usually is realpath(storage_path('framework/views')). Now, that is absolutely fine under normal circumstances. The last piece of the puzzle is gcloud app deploy which will for some reason neglect to deploy empty directories or directories with only .gitignore in them, hence, storage/framework/views will not be deployed and during build the error will occur.
Possible fixes:
Add some random file (other than .gitignore) in 'storage/framework/views' before deploying to make sure the directory is available during build.
Change the default value in config/views to a directory that is present during build.
Any other way to ensure that storage/framework/views is not ignored (present during build) should do.
My working solution was to edit config/view.php:
'compiled' => env(
'VIEW_COMPILED_PATH',
isset($_SERVER['GAE_SERVICE']) ?
'/tmp'
: realpath(storage_path('framework/views'))
),
This will make sure that the default location for compiled views when the app is running on GAE machine is located to /tmp directory instead of storage/framework/views.
The solution was to update the Livewire dependency.
Before:
"livewire/livewire": "^1.1",
After:
"livewire/livewire": "^2.1",
Any version beginning with 2.0 seems to work.

Laravel run package migration by composer update automatic

I have a package and my ServiceProvider copies the migrations. This works fine after php artisan migrate. But now i want to execute the package migration when i do composer update name/name-package.
So, php artisan update name/packagename and directly automaticly migrate.
How can i achieve that?
$this->publishes([
__DIR__ . '/database/migrations' => $this->app->databasePath() . '/migrations'
], 'migrations');
in application (not package) composer.json
"scripts": {
"post-update-cmd": [
"php artisan optimize",
"php artisan migrate"
]
}
the order of commands is up to you. Also, you may create a command or use php artisan vendor:publish --provider=... to move the migrations

Class App\Http\Controllers\Auth\AuthController does not exist

i followed these , laravel virsion 5.4.16
https://www.youtube.com/watch?v=bqkt6eSsRZs&t=29s
You need to regenerate namespaces autoload
composer dump-autoload
"php artisan make:auth" produces all the authentication scaffolding required for the authentication process.
Type "php artisan make:auth" in your terminal right in the directory of your project. You will need to tidy up your install by deleting the "layouts folder" folder within the "resources folder", and also change the "#extends('layouts.app')" to "#extends('main')" in the login.blade.php and register.blade.php views within the "resources/views/auth" folder. Also you should then run "php artisan route:list" to see the names and locations of the auth routes.
I hope this helps

PHPStorm: How to setup running terminal commands instead of trying open it

lately I start to learning Laravel using PHPStorm.
Anyway I set up composer and all that stuff, but still have problem with one very annoying issue.
Every time I type "composer update" or something similar that need to fire few commands "from under the hood" I receive popup saying:
"Cannot find file" "/Application/.../optimize" - as an example of
php artisan optimize
It's the same for all commands that's in composer.json file:
(...)
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"pre-update-cmd": [
"php artisan clear-compiled"
],
"post-update-cmd": [
"php artisan optimize"
],
"post-root-package-install": [
"php -r \"copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
(...)
Which I need to run by myself.
And funny thing - when I type "php artisan optimize" I get (before I get massages as above):
Please help me to set PHPStorm working correctly.

How do I set up Bootstrap after downloading via Composer?

I'm a beginner with Composer, so I know little about it and have little experience with web application development.
I was reading the Nettuts+ Tutorial, and have a basic question about Composer.
{
"require": {
"laravel/framework": "4.0.*",
"way/generators": "dev-master",
"twitter/bootstrap": "dev-master",
"conarwelsh/mustache-l4": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"mockery/mockery": "0.7.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-update-cmd": "php artisan optimize"
},
"minimum-stability": "dev"
}
If I set up my composer.json file as above, after executing composer install --dev, how do I make Bootstrap available for use in the Laravel project?
I mean, I can see the Bootstrap package is downloaded to the vendor directory. Before I only used to download Bootstrap from its official website and manually put the files in the public directory of Laravel, but what is the right way to do this here? Can I leave the Bootstrap files where they are, because I want to update the Bootstrap package to its latest version periodically?
Thanks.
We have artisan command to publish the assets(CSS, JS,..). Something like this should work.
php artisan asset:publish --path="vendor/twitter/bootstrap/bootstrap/css" bootstrap/css
php artisan asset:publish --path="vendor/twitter/bootstrap/bootstrap/js" bootstrap/js
i am not sure about the path.. But this should work.
As the tutorial says, you have to copy it to your public directory:
cp vendor/twitter/bootstrap/docs/assets/js/html5shiv.js public/js/html5shiv.js
cp vendor/twitter/bootstrap/docs/assets/js/bootstrap.min.js public/js/bootstrap.min.js
EDIT:
You really have copy them, because your assets files should lie in the public folder only and Composer is all about putting packages on your vendor's folder, which must not be visible to the outside world.
But you can create a Composer post-install-cmd:
{
"scripts": {
"post-update-cmd": "MyVendor\\MyClass::postUpdate",
}
}
And make it copy those files for you every time an update happens. It can be written using PHP, bash or any other language you can run on your host. Docs: http://getcomposer.org/doc/articles/scripts.md.
Just realised that php artisan asset:publish --path="vendor/twbs/bootstrap/dist/" bootstrapor php artisan vendor:publish --path="vendor/twbs/bootstrap/dist/" bootstrap do not work anymore.
What worked for me is editing the composer.json to add the following under scripts, post-update-cmd:
"scripts": {
"post-update-cmd": [
"php artisan optimize",
"mkdir -p public/bootstrap",
"cp -R vendor/twbs/bootstrap/dist/ public/bootstrap/"
]}
Just symlink the folder like said above by LeviXC:
{
"scripts": {
"post-update-cmd": "ln -sf vendor/twitter/bootstrap/dist/ public/vendor/bootstrap/"
}
}
Or multiple commands:
{
"scripts": {
"post-update-cmd": [
"php artisan optimize",
"ln -sf vendor/twitter/bootstrap/dist/ public/vendor/bootstrap/"
]
},
}
The solution with composer post update script (post-update-cmd) in Windows environment could be:
{
"require": {
"twbs/bootstrap": "4.3.1"
},
"scripts": {
"post-update-cmd": [
"RMDIR public\\assets\\bootstrap /S /Q" ,
"XCOPY /E /I vendor\\twbs\\bootstrap\\dist public\\assets\\bootstrap"
]
}
}
You will have the bootstrap files inside public\assets\bootstrap folder ready to be imported in HTML.
This worked better for me
"scripts": {
"post-update-cmd": [
"mkdir -p html/vendor/",
"ln -sfr vendor/twbs/bootstrap/dist html/vendor/bootstrap"
]
},
The "r" flag made the symlink relative, thus pointing to the real folder
I am new to Laravel and Bootstrap (and to using them together) and I stumbled across this thread when having the same issue. I created a new Laravel project, then added Bootstrap by running the following command from within the root directory of the Laravel project:
%composer require twbs/bootstrap
Then, in my view file, I included the following code:
<link href="css/app.css" rel="stylesheet">
It appears that composer (or bootstrap) adds app.css which includes the Bootstrap css files (which are located in the non-public vendor folder) by reference. Adding the reference to app.css worked, and I was able to use Bootstrap components in my view.

Resources