Как очистить кэш php artisan

Laravel 5 – Clear Cache in Shared Hosting Server

Is there any workaround to clear the cache like the above command but without using CLI. I am using a popular shared hosting service, but as per my plan, I don’t have control panel access. I want to clear the views cache. I saw a question almost the same like this, but it doesn’t help me.

Running Laravel on shared hosting is insane, IMO, for precisely this sort of reason. How are you running migrations?

@ceejayoz .. Actually I just started this project, and its my first laravel project as well. I didn’t come to this migration thing yet..

«Running Laravel on shared hosting is insane» @ceejayoz . But this is the real world. Sometimes you have to because there’s no choice.

20 Answers 20

You can call an Artisan command outside the CLI.

Route::get('/clear-cache', function() < $exitCode = Artisan::call('cache:clear'); // return what you want >); 

There is no way to delete the view cache. Neither php artisan cache:clear does that.

If you really want to clear the view cache, I think you have to write your own artisan command and call it as I said before, or entirely skip the artisan path and clear the view cache in some class that you call from a controller or a route.

Читайте также:  Method calling another method in java

But, my real question is do you really need to clear the view cache? In a project I’m working on now, I have almost 100 cached views and they weight less then 1 Mb, while my vendor directory is > 40 Mb. I don’t think view cache is a real bottleneck in disk usage and never had a real need to clear it.

As for the application cache, it is stored in the storage/framework/cache directory, but only if you configured the file driver in config/cache.php . You can choose many different drivers, such as Redis or Memcached, to improve performances over a file-based cache.

What cache are you trying to clear? The cache:clear command works with the application cache, the one that is accessed from the Cache facade.

I want to clear the view cache, there are a lot of pages saved in view cache folder. One more question, when says application cache, which are all the directories it targets.

Thanks for the explanation!. I am not worried bout disks space 🙂 But one question if view cache is not clearing, then how the new changes in view get affect in the website, is that any I/O check happening in laravel ?

«There is no way to delete the view cache» This is simply not true. You can use php artisan view:clear to do just that.

Go to laravelFolder/bootstrap/cache then rename config.php to anything you want eg. config.php_old and reload your site. That should work like voodoo.

Confirmed it wipe the pain in the a** while hosting laravel 5.4 from windows to shared hosting. THANK YOU.

It seems that in a few scenarios, this is the only solution that works. If you have a cached config, and then you remove a Facade or Service Provider, when you run the command to create the new cached config, it runs using the existing cached config, and tries to reference the Facade and/or Service Provider classes that no longer exist and fails. The other option would be to remove the references from the config file, regenerate your cached config, then remove the actual Facade and/or Service Provider classes.

For Laravel 6.6, To remove caching, I just removed cache folder & in .env file default cache value. But after deleting all this getting problem again.. when php artisan optimize . Finally this one solved my problem, not by renaming but changing the key value in config.php file. Thanks @DeadGuy

is it possible to use the code below with the new clear cache commands:

//Clear Cache facade value: Route::get('/clear-cache', function() < $exitCode = Artisan::call('cache:clear'); return '

Cache facade value cleared

'; >); //Reoptimized class loader: Route::get('/optimize', function() < $exitCode = Artisan::call('optimize'); return '

Reoptimized class loader

'; >); //Route cache: Route::get('/route-cache', function() < $exitCode = Artisan::call('route:cache'); return '

Routes cached

'; >); //Clear Route cache: Route::get('/route-clear', function() < $exitCode = Artisan::call('route:clear'); return '

Route cache cleared

'; >); //Clear View cache: Route::get('/view-clear', function() < $exitCode = Artisan::call('view:clear'); return '

View cache cleared

'; >); //Clear Config cache: Route::get('/config-cache', function() < $exitCode = Artisan::call('config:cache'); return '

Clear Config cleared

'; >);

It’s not necessary to give the possibility to clear the caches to everyone, especially in a production enviroment, so I suggest to comment that routes and, when it’s needed, to de-comment the code and run the routes.

I have problem with config:cache its doesn’t work in route or controller but others are worked. what would be problem?

Config caching The laravel config spreads across dozens of files, and including every one of them for each request is a costly process. To combine all of your config files into one, use:

Keep in mind that any changes to the config will not have any effect once you cache it. To refresh the config cache, run the above command again. In case you want to completely get rid of the config cache, run

Routes caching Routing is also an expensive task in laravel. To cache the routes.php file run the below command:

Mind that it doesn’t work with closures. In case you’re using closures this is a great chance to move them into a controller, as the artisan command will throw an exception when trying to compile routes that are bound to closures instead of proper controller methods. In the same as the config cache, any changes to routes.php will not have any effect anymore. To refresh the cache, run the above command everytime you do a change to the routes file. To completely get rid of the route cache, run the below command:

Classmap optimization

It’s not uncommon for a medium-sized project to be spread across hundreds of PHP files. As good coding behaviours dictate us, everything has its own file. This, of course, does not come without drawbacks. Laravel has to include dozens of different files for each request, which is a costly thing to do.

Hence, a good optimization method is declaring which files are used for every request (this is, for example, all your service providers, middlewares and a few more) and combining them in only one file, which will be afterwards loaded for each request. This not different from combining all your javascript files into one, so the browser will have to make fewer requests to the server.

The additional compiles files (again: service providers, middlewares and so on) should be declared by you in config/compile.php, in the files key. Once you put there everything essential for every request made to your app, concatenate them in one file with:

php artisan optimize --force 

Optimizing the composer autoload

This one is not only for laravel, but for any application that’s making use of composer.

I’ll explain first how the PSR-4 autoload works, and then I’ll show you what command you should run to optimize it. If you’re not interested in knowing how composer works, I recommend you jumping directly to the console command.

When you ask composer for the App\Controllers\AuthController class, it first searches for a direct association in the classmap. The classmap is an array with 1-to-1 associations of classes and files. Since, of course, you did not manually add the Login class and its associated file to the classmap, composer will move on and search in the namespaces. Because App is a PSR-4 namespace, which comes by default with Laravel and it’s associated to the app/ folder, composer will try converting the PSR-4 class name to a filename with basic string manipulation procedures. In the end, it guesses that App\Controllers\AuthController must be located in an AuthController.php file, which is in a Controllers/ folder that should luckily be in the namespace folder, which is app/ .

All this hard work only to get that the App\Controllers\AuthController class exists in the app/Controllers/AuthController.php file. In order to have composer scanning your entire application and create direct 1-to-1 associations of classes and files, run the following command:

Keep in mind that if you already ran php artisan optimize —force, you don’t have to run this one anymore. Since the optimize command already tells composer to create an optimized autoload.

Источник

Laravel caches and all ways to clear them

Tinkerwell background image Tinkerwell background image

Laravel has different caches for different parts of your application and so there are multiple ways to clear the Laravel cache.

Before you actually clear your cache, check out Tinkerwell. Tinkerwell will be your favorite compagnion to your IDE and as a Laravel developer, you’ll use it every day.

Application cache

The application cache is the primary cache in Laravel. It stores everything that you manually cache in your application. You can clear only specific elements of the cache if you use tags or different cache stores. The easiest way to clear the Laravel cache is via artisan:

Clear Laravel cache via artisan command

If you use multiple caches and you want to clear a specific store, you can pass this as a parameter to the command:

php artisan cache:clear --store=redis 

You can clear cached items with specific tags with the command:

php artisan cache:clear --tags=tag1,tag2 

Clear Laravel cache programmatically

Removing items from the cache programmatically is as easy as clearing the cache via the artisan command. In addition, you can use the cache facade to access the cache or use the cache helper.

Clearing cached items with the tag awesome-tag is as easy as purging a specific cache store:

cache()->store('redis')->tags('awesome-tag')->flush() 

Whenever I want to check if there is an item in the cache or remove it from the cache, I start Tinkerwell and run the commands above.

View cache

Another part of the application that has a cache is the view cache. The view cache stores rendered Blade templates to speed up your application. You can manually render all views to increase the performance by using the artisan command for it:

If you use this optimization, you have to clear the cache if you deploy new code, otherwise, Laravel uses your old views and you’ll try to debug this forever. You can clear the view cache of Laravel with the command:

Config cache

Laravel recommends caching your configuration files so that the application doesn’t need to go through all config files while it bootstraps the framework.

You can combine all config files into one large file and optimize the performance with the command:

Make sure to clear this cache if you change a configuration, for example, during a production deployment process:

Event cache

When running in production, caching the Events and their Listeners allows for efficient event handling. Laravel recommends to cache events and listeneners during your deployment process – and this means that you have to clear the event cache too.

To cache events and listeners, run the event:cache command during your deployment:

The event:cache command automatically clears all event caches, but if you have to run it manually, you can do it like this:

Route cache

The route cache is an additional performance cache that you only want to use in production and as part of your deployment process. Caching your routes drastically decreases the amount of time to register your application’s routes. You can cache the routes via:

In case you change a route or tried the cache command during development, you have to clear the route cache or your application won’t find new routes. You clear the route cache with the command:

Tinkerwell: The code runner for PHP

The must-have companion to your favorite IDE. Quickly iterate on PHP code within the context of your web application.

Tinkerwell

The code runner for PHP.
Loved by developers like you.

Источник

Clear all cache in Laravel with artisan [duplicate]

I removed my provider but it still exists in the cache. I don’t want to remove manually directory /bootstrap/cache . Does exist any artisan command which will take care of flushing all cache without any error? I have already tried without any effect:

artisan config:cache artisan config:clear artisan cache:clear artisan optimize 

6 Answers 6

php artisan optimize:clear 
Cached events cleared! Compiled views cleared! Application cache cleared! Route cache cleared! Configuration cache cleared! Compiled services and packages files removed! Caches cleared successfully! 

Use composer dump-autoload to clear the providers cache

The same error and more Script @php artisan package:discover —ansi handling the post-autoload-dump event returned with error code 1

Proper way

Check if /projectroot/app/Providers/TranslationServiceProvider.php class exists with the minimum content of:

Create this file if you don’t have it. Then run php artisan config:cache to check if the error has been gone.

Hacky way

Open bootstrap/services.php file and search for App\Providers\TranslationServiceProvider and remove the full line where you see it.

Finally after both ways

Go to config/app.php , find the line App\Providers\TranslationServiceProvider.php in providers and aliases arrays, where it exists and remove them.

Then run php artisan config:cache again to cache the services without having that reference. If everything goes correct, you may safely delete the file which you’ve created at the first step (if you’ve used the proper method).

Источник

Оцените статью