Laravel artisan optimize Best Practices - php

I'm trying to fully understand the Laravel (5.1) artisan optimize command and best practices, but the documentation seems lacking. I don't have Composer installed on the production server so, specifically, I want to know what files are modified or created when running artisan optimize --force on development that must get pushed to production. The goal being not to blow up the app in production! After running the command, I see the following files have been modified:
\bootstrap\cache\compiled.php
\vendor\composer\ - the entire directory
\vendor\autoload.php
Am I overthinking this, or do I just push these files to production and I'm good to go? Also, what is the best practice regarding when to run artisan optimize? Each time a new model is created? What about controllers, routes and helper classes?
Lastly, I see the \bootstrap\cache\compiled.php file is a whopping 548KB and almost 17K lines! Is that really considered optimal?

[edit - As #crishoj says, as of Laravel 5.5, php artisan optimize is no longer needed]
Normal Laravel practice is to have composer installed on your production server.
These are the steps Envoyer (made by Laravel's creator) takes to deploy an app on production -- I've annotated them below:
# Install application dependencies, such as the Laravel framework itself.
#
# If you run composer update in development and commit the `composer.lock`
# file to your repository, then `composer install` will install the exact
# same versions in production.
composer install --no-interaction
# Clear the old boostrap/cache/compiled.php
php artisan clear-compiled
# Recreate boostrap/cache/compiled.php
php artisan optimize
# Migrate any database changes
php artisan migrate

As of Laravel 5.5, php artisan optimize is not longer required.

You can also take benefit of laravel packages to easily optimize your application by caching page partials
https://github.com/imanghafoori1/laravel-widgetize

Related

Laravel 8 UI issue

I have installed Laravel 8 as a new project. When I run the project I get the "In order to use the Auth::routes() method, please install the laravel/ui package." But Laravel installation doc says that laravel ui is depreciated and that we should use Jetstream. So I installed Jetstream with the Livewire stack. Back to test the project and I get the same error msg.
I have run a composer dump-autoload.
What do I have to do to get a Laravel 8 project to run?
You will need to clear and recreate cache. Run the following php artisan commands:
php artisan cache:clear
php artisan config:cache
php artisan route:cache
I had this problem as well on many 'upgrades' from earlier versions of Laravel. I don't want to change the entire auth mechanism, as it works, so I will keep the code I have.
#Elisha Senoo's answer is correct, and solved it most of the time. However, if you find yourself unable to clear the cache (because it has the same error message on the artisan command), the solution is to manually clear the .php files within the bootstrap/cache folder and then php artisan config:cache.

Heroku - View Not Found - FileFinderView InvalidArgumentException

This is consuming the last 2 days of my life. Locally everything is working fine but when my code is deployed to Heroku, Laravel can't find the index view. It's my understanding this is some caching issue, so I have tried most of Artisan's commands to clean the cache:
php artisan config:cache
php artisan route:cache
php artisan view:clear
php artisan clear-compiled
This is my route, I'm serving a SPA:
Route::get('/{any}', 'HomeController#main')->where('any', '.*');
And this is the stack trace on my view:
I have cleaned the bootstrap/cache folder and honestly I'm out of ideas. Does someone have a light on this one? Thanks
Please use the all around cache clearing command :
php artisan optimize:clear
I've found the solution to the same issue:
The issue itself seems to be caused by the official php buildpack, which does php artisan optimize at the end of the build process inside the build environment (aside from other things it clears and recreates the configuration cache). This causes the build directory /tmp/build_xxxxxxxx/ to be saved into the configuration cache.
The solution is to run php artisan config:clear, but it won't take effect on the already built and running app because of how heroku works: when you access their shell you perform operations on the filesystem that's generated from a project build and is destroyed as soon as you exit the shell. The operations you perform on files in that shell don't effect the running app instances. php artisan config:clear must be ran at the end of the build process.
One way to do this is the gerardbalaoro/laravel buildpack which allows adding custom hooks after the build process. Keep in mind that it also does php artisan views:clear after the build while the official php buildpack doesn't. Using it and adding LARAVEL_COMMANDS='php artisan config:clear' to heroku config solved the issue for me.
tl;dr:
heroku buildpacks:set gerardbalaoro/laravel
heroku config:set LARAVEL_COMMANDS='php artisan config:clear'
git commit --allow-empty -m 'force rebuild'
git push heroku master

Laravel with App Engine Standard Class 'Facade\Ignition\IgnitionServiceProvider' not found

This is driving me crazy second day. I'm new to Laravel and trying to get Laravel 6 work on Google App Engine Standard.
Tried: This tutorial and other ones, but it still fails to load Laravel index page
What I have done:
Created new Laravel project
Checked that Laravel is working on local host with php artisan serve
Generated new key with php artisan key:generate --show
Created app.yaml file with:
runtime: php72
env_variables:
APP_KEY: iktbUa2quYPV2av3zDx0XAuEVjwzVQY/oMfyI2PQNKk=
APP_STORAGE: /tmp
VIEW_COMPILED_PATH: /tmp
SESSION_DRIVER: cookie
Modified bootstrap/app.php by adding $app->useStoragePath(env('APP_STORAGE', base_path() . '/storage'));
Rancomposer remove --dev beyondcode/laravel-dump-server
Also ran:
php artisan cache:clear,
php artisan route:cache,
php artisan config:clear,
php artisan view:clear (as other tutorials suggested)
And gcloud app deploy
And I get error: "Class 'Facade\Ignition\IgnitionServiceProvider' not found"
If you're facing this issue after upgrading composer 1.0 to 2.0. Then you can follow this step:
Go to the project directory
Run composer install
Run composer dump-autoload
Try to run php artisan serve
It solved for me
Please Try
composer dump-autoload
It solved for me
App engine install dependency with "--no-dev" flag. If your application is debug mode and there is an error application is try to render error page by "facade/ignition" packages.
Solve-1: You can move "facade/ignition" dependency from "require-dev" to "dev" in composer.json file
Solve-2: You can run as production environment and with false APP_DEBUG flag
I have experienced other errors as well, most of them gave me Error 500 and
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /tmp/google-config/nginx.conf:3 in Stackdriver Logger
Also I was not aware that App Engine was executing composer.json on build (see Specifying Dependencies). And it seems the issue was that it cached that.
Everything was resolved by replacing command in step 7. :
gcloud app deploy with gcloud beta app deploy --no-cache
It seems that if I would have done all steps as in tutorial from the start I would not have any issues.
Try
php artisan route:clear
and you're good to go.
I try
composer update
and its working fine
Try running
composer update
or
composer dump-autoload
Go to your project terminal and run :
composer install
composer dump-autoload
php artisan serve
And your problem will go away.
I am also new to Laravel and I have experienced with this issue, and I searched solution which was
first run this command on your terminal
composer update
second run the command on below
composer dump-autoload
and then see the result :)
just uncomment soduim line from your ini file
before
;extension=sodium
after
extension=sodium
If you are facing this issue while running phpUnit tests then try to clear cache of bootstrap folder.

How to cache configuration in Laravel inside Heroku? i.e. build in a different path from runtime

In the Laravel docs it's advised to run ./artisan config:cache in production to speed things up. That's great with Heroku since every build brings up a new filesystem state, so we don't even have to bother with clearing the cache between deploys.
BUT: if you add that command to your deployment procedure (via Composer for instance) your Laravel app will start crashing, since it'll be looking for files in the now-gone build paths (something like /tmp/random_string). If you run heroku run pwd you'll notice the runtime app lives on /app.
It seems ./artisan config:cache stores the temporary build path in the cached settings, while the app runs in another path. Is it possible to change the path used in the resulting config cache?
You'd best do this at boot and not at build time.
In order to do so you need to modify you composer.json to add:
"warmup": [
"php artisan config:cache",
"php artisan route:cache"
],
And then modify your procfile to something like web: composer warmup && $(composer config bin-dir)/heroku-php-apache2 public/
Credits for the tip goes to David from the Heroku support!

Laravel 5.0 - Shared Hosting - Artisan not working as it should

I actually wanted to follow up on this question, but I guess It's better to start a new question.
I installed a fresh copy of my own laravel(5.0), and I tried running php artisan route:list, which works.
Now I have downloaded the compressed repository of a project I want to deploy on a shared hosting enviorment, but when I run php artisan route:list nothing happens.(No error message, nothing). Using this method for hosting the application
The actual problem is php artisan migrate, which also outputs nothing!
Is there a good method for troubleshooting this ?
Could you provide me we some points of failure that I can check ?
Worth mentioning:
I'm no Laravel developer and I have limited time reading up on it.
As LittleFinger suggested, it's possible that artisan is not actually yet installed. When deploying from a repo, Laravel's packages of which the entire framework core is composed, are not included. You should run composer install to install the packages. If you don't have composer installed that can be difficult on shared hosting, but it's usually possible to install it.
You will need to run composer install before you run composer update. Running composer update is not required, unless you want to update to the newest versions of your packages which are allowed by the rules in your composer.json file. This should be done with care in a production environment as new versions of packages could break your app.
Once you've installed the packages, you'll need to set your environment variables (database credentials etc.) by copying the .env.example file to .env and editing it. Once you've done this you'll be able to run php artisan key:generate to generate an encryption key.
After this, your app should work (assuming you've pointed a domain to the /public directory).
I am facing the same issue when I try to run
php artisan migrate or php artisan cache:clear
nothing happen just a blank screen no success no error see the screenshot
after debugging I found a message in error_log in root directory which says.
Fatal Error: Allowed Memory Size
after increasing the memory php artisan commands works fine

Categories