Clear views cache on Lumen - laravel

Few weeks ago, I had the same problem in Laravel 5.1, which I could solve with this solution.
However, now I'm facing the same issue in Lumen, but I can't call php artisan view:clear to clear the cached files. There is any other way?
Thanks!

There's no command for the view cache in lumen, but you can easily create your own or use my mini package found at the end of the answer.
First, put this file inside your app/Console/Commands folder (make sure to change the namespace if your app has a different than App):
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ClearViewCache extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $name = 'view:clear';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Clear all compiled view files.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$cachedViews = storage_path('/framework/views/');
$files = glob($cachedViews.'*');
foreach($files as $file) {
if(is_file($file)) {
#unlink($file);
}
}
}
}
Then open app/Console/Kernel.php and put the command inside the $commands array (again, mind the namespace):
protected $commands = [
'App\Console\Commands\ClearViewCache'
];
You can verify that everything worked by running
php artisan
inside the project's root.
You will now see the newly created command:
You can now run it like you did in laravel.
EDIT
I've created a small (MIT) package for this, you can require it with composer:
composer require baao/clear-view-cache
then add
$app->register('Baao\ClearViewCache\ClearViewCacheServiceProvider');
to bootsrap/app.php and run it with
php artisan view:clear

Related

why cron job stoped in new year 2021 in laravel

every thing works fine till 31 decemeber 2020 and in new year my cron job stoped working i dont know what happend i am using spatie laravel package to take db backup an i also have one other cron job in console and command
kernal.php
`<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
'App\Console\Commands\UpdateUserNotNew',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('cron:update-user-not-new')->everyMinute();
$schedule->command('backup:run')->everyMinute();
$schedule->command('backup:clean')->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}`
my issue is resolved byhttps://stackoverflow.com/a/65890784/14913109
only my php version was not integrated with php multimanager
The problem is most likely not coming from spatie/laravel-backup package. To run Laravels cronjob you need to a have a central call in your crontab to Laravels scheduler
* * * * * cd /path-to-your-project && php artisan schedule:run
You can also check all the scheduled tasks locally by running
$ php artisan schedule:work
and if they run fine locally then there is an issue with your server, but not with Laravel.
Also always have a look at storage/logs/laravel.log if there are any errors showing up.

Generate Artisan command instead of "php artisan serve"

I want to make an alias like
php artisan go
instead of
php artisan serve
I will appreciate any other idea :-) .I also read this link and search a lot but it wasn't so clear and other questions were about making class or .env files and etc.
Thanks in advance
Update
This question is not duplicate of this because it's not contain calling php artisan itself.
Create the command using:
php artisan make:command GoCommand
Add this in the class:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Symfony\Component\Console\Output\ConsoleOutput;
class GoCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'go';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$output = new ConsoleOutput;
$output->writeln("Laravel development server started: <http://127.0.0.1:8000>");
Artisan::call('serve');
Artisan::output();
}
}
Use the command:
php artisan go
Visit: http://127.0.0.1:8000/
and see the output in your console.

How to clear all Log files data using Monolog in Laravel

How to empty log files data before adding new contents to it.
Using Monolog for saving all logs inside
storage/app/logs/*
You can use rm command through ssh to remove all logs:
rm storage/logs/laravel-*.log. Use * as wildcard to remove all logs if they have suffixes.
Or you can add custom code within Controller method only for admins of app:
$files = glob('storage/logs/laravel*.log');
foreach($files as $file){
if(file_exists($file)){
unlink($file);
}
}
Or create a console command. Depending on version, below 5.3 use for example:
php artisan make:console logsClear --command=logs:clear
For versions 5.3 and above
php artisan make:command logsClear
add signature within command class if it doesn't exist.
protected $signature = 'logs:clear';
Add your class in Console/Kernel.php, in protected $commands array ( note that your code varies upon customization for your app):
<?php
namespace App\Console;
use App\Console\Commands\logsClear;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Utils\ShareHelper;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
logsClear::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
}
}
You should add then custom code in handle() of logsClear class
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class logsClear extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'logs:clear';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
$files = glob('storage/logs/laravel*.log');
foreach($files as $file){
if(file_exists($file)){
unlink($file);
}
}
}
}
Then run php artisan logs:clear command

Laravel 5.1: Run Custom Artisan Command in Background

I'm working on a chat application using the Ratchet package. With the help of tutorials I've written a custom artisan command to start the Websocket server. I need to run this Artisan command in the background and it should be running all the time. How do I do it?
I tried using Artisan::queue and Artisan::call from Artisan Facade. But since my custom command runs indefinitely(for a long time) it isn't working.
Edit:
My hosting provider is not allowing me to run Artisan commands through ssh.
Here is the code for the Custom Artisan Command:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use App\Classes\Socket\ChatSocket;
use App\Classes\Socket\Base\BaseSocket;
class ChatServer extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'chat_server:serve';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->info("Start server");
$server = IoServer::factory(
new HttpServer(
new WsServer(
new ChatSocket()
)
),
8080
);
$server->run();
}
}
You simply should run it in console with command:
php artisan chat_server:serve
And probably you should make sure it will work all the time. One of the ways is using Supervisor to make sure this command will run (almost) all the time

Artisan: "Command not found"

So I try to add a Command with
php artisan command:make MailSendCommand
The file MailSendCommand.php is created.
I edited it to this:
class MailSendCommand extends Command {
protected $name = 'command:send-mail';
protected $description = 'Send mails to the users.';
public function __construct()
{
parent::__construct();
}
public function fire()
{
$this->info('Befehl wird ausgeführt' );
}
...
In my file start/artisan.php I added
Artisan::add(new MailSendCommand);
but when I enter 'php artisan command:send-mail' it says:
Command "command:send-mail" is not defined.
It just worked on my Home PC (XAMPP) but not on my live server (PHP 5.5.15 (cgi-fcgi))
'php artisan clear:cache' and 'php artisan dump-autoload' did not help.
Go to app/Console/Kernel.php and add the class to the commands variable. It would look something like:
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\MyCustomCommand::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
}
}
Here i added MyCustomCommand as a valid artisan command. Check it is among available commands executing:
php artisan list
It looks as though the problem is related to your setup. You're using PHP CGI instead of CLI to run your commands which will not work.
Ensure that the code is there on your live environment and run your commands on CLI, as these are command line scripts.

Resources