On a fresh Laravel install I created an Artisan command that just prints the memory usage.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MemoryTest extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'memory:test';
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$this->info(memory_get_usage()); // 17807760 -> 17 mb
}
}
When I run this command, the memory usage is 17mb.
If I make an HTTP request on a fresh Laravel install and print the memory usage it is 1038648 (1mb).
Also, if I run php artisan tinker then run memory_get_usage() I get 24247168 (24mb).
Why is the memory usage of Artisan commands so much higher than HTTP requests?
Related
I'm trying to add Horizon to my latest laravel app but when i visit my url with the horizon path (https://my-app.com/horizon) I get a 404, php artisan routes:list doesn't contain any routes to horizon. I have ran php artisan route:clear and no joy either.
I followed the official docs (https://laravel.com/docs/9.x/horizon) So i did the following commands:
composer require laravel/horizon
php artisan horizon:install
and edited the config to use redis. I'm using a local environment at the moment so the gate would not be needed (right?) I am also using spatie/permissions so with a permission of access horizon i edited the HorizonServiceProvider.php file as follows:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\HorizonApplicationServiceProvider;
class HorizonServiceProvider extends HorizonApplicationServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
parent::boot();
}
/**
* Register the Horizon gate.
*
* This gate determines who can access Horizon in non-local environments.
*
* #return void
*/
protected function gate()
{
Gate::define('viewHorizon', function ($user) {
return $user->can( 'access horizon' );
});
}
}
This should allow the user if they have the access horizon permission and refuse otherwise (but I'm on local so will run anyway?)
I'm using laravel 9.23 with php8.1 and have redis enabled. I also see that horizon is running php artisan horizonand then php artisan horizon:status says Horizon is running.
Have I missed something? So Horizon is running redis is enabled and set to use the queue, no horizon route in my route list and if i visit the url i obviously get a 404 error.
After power down for the weekend and startup this morning Horizon is now working!
I have already developed project in laravel and i'm setting up it in my local computer but after running php artisan serve i'm getting this error
PHP Fatal error: Uncaught ReflectionException: Class App\Console\Kernel does not exist in C:\xampp\htdocs\translate\vendor\laravel\framework\src\Illuminate\Container\Container.php:788
I don't know what is wrong but I have tried everything that I have found on internet
composer update
composer dump-autoload
composer self-update
php artisan config:clear
php artisan cache:clear
none of that command worked for me
You must run composer install for installing the new dependencies.
Since you mentioned that you have issues with your other artisan commands delete everything inside the bootstrap->cache folder, except of the .gitignore file of course, manually and then run php artisan optimize
That way your "corrupted" cache will be recreated and reconfigured.
First thing you must do is to make sure your artisan commands are working right, so do the caching fix i suggest first. Then you can run the composer commands you mentioned also in your question.
Check you have this file in app/Console folder
<?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 = [
//
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
/*$schedule->command('users:update')->everyMinute();
$schedule->command('servers:update')->everyMinute();*/
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
I have a Laravel project that has been deployed with Forge and had OPCache enabled using Forge. I noticed last week that when I pushed some changes, the changes that were in the views and in the controllers were present on the server, but custom artisan commands that I run don't recognize updates.
Put another way, updates to the blades are showing on the screen. Updates that I have added to the controllers are changing the way information is passed to the blade files, but I have a custom artisan command that runs a series of methods in a trait. The actual file on the server shows the new method that I pushed, but when I run the artisan command in the CLI, it says that the method cannot be found.
I have stopped, restarted, and reloaded OPCache countless times. I have restarted Nginx. I have disabled OPCache and restarted PHP. It is still saying that the method is not found. Does anyone have any ideas?
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Traits\FTPImportsTrait;
class CheckFTPImports extends Command
{
use FTPImportsTrait;
protected $signature = 'checkForImports';
protected $description = 'Check for imports...';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->checkBankImports();
}
}
-----------
<?php
namespace App\Traits;
trait FTPImportsTrait;
{
public function checkBankImports()
{
dd('YOU ARE NOT CRAZY');
}
}
$ php artisan checkForImports
$ method checkBankImports does not exist.
UPDATE:
It has to be some sort of configuration issue on the server. I just deployed the project to a fresh DO droplet and the command works as expected.
It only happened in the production environment for me.
Running:
php artisan clear-compiled
deleted the cached version and solved my issue.
Thanks a ton to #num8er.
I have created a custom service provider in my app called DemoServiceProvider in app/providers directory , and i register it in config/app as App\Providers\DemoServiceProvider::class
here is my DemoServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class DemoServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
dd("test");
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
the problem is i don't see "test" ,did i miss something ?
I have replicated your code and I retrieve:
php artisan serve --port=4433
"test"
Have you tried already:
composer dumpautoload
php artisan cache:clear
ok i found the solution ,it was with these two commands php artisan clear-compiled and php artisan cache:clear .
I'm trying to execute code within my Lumen install via the command line. In full Laravel , I've read that you can use commands to achieve this via "make:command", but Lumen does not seem to support this command.
Is there anyway to enable this command? Failing that, what's the best way of running code from the CLI in Lumen?
Thanks
You can use the artisan CLI in Lumen as the same way as in Laravel but with fewer built-in commands. To see all built-in commands, use the php artisan command in Lumen.
Although there is no make:command command at Lumen, you can create your custom command:
Add new command class inside the app/Console/Commands folder, you can use the sample class template of the framework serve command
Register your custom command by adding your created class to the $commands member inside the app/Console/Kernel.php file.
Except the command generating, you can use the Laravel docs for commands when working with Lumen.
Here is a template for a new command.
You can just copy and paste this in to a new file and start working.
I tested it on lumen 5.7.0
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CommandName extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'commandSignature';
/**
* 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('hello world.');
}
}
Then register it on the Kernel.php file.
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\App\Console\Commands\CommandName::class
];
When you create your command class use this:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
Instead of what was described above about using serve command example