Kernel does not exist in laravel - php

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');
}
}

Related

Why do Laravel Artisan Commands use so much Memory?

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?

Laravel console command trait method not found

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.

Ignore a custom Laravel Artisan command on production

I've written a custom Artisan command (let's call it MyDusk.php) that expands/abstracts some of the functionality of the core Dusk command.
This custom command extends Laravel\Dusk\Console\DuskCommand from within the Dusk package.
The problem is, on production the Dusk package is not installed (it's under require-dev in composer.json)
So when composer is generating its autoload files on production, it errors out when it gets to MyDusk.php because it can't find Laravel\Dusk\Console\DuskCommand.
PHP Fatal error: Class 'Laravel\Dusk\Console\DuskCommand' not found in app/Console/Commands/Dusk.php on line 10
In Dusk.php line 10:
Class 'Laravel\Dusk\Console\DuskCommand' not found
I tried moving the Dusk package to require so it would be available on production (not ideal, I know), but there's a line in the core Dusk service provider that throws an exception when it's run on production preventing this:
# From: vendor/laravel/dusk/src/DuskServiceProvider.php
if ($this->app->environment('production')) {
throw new Exception('It is unsafe to run Dusk in production.');
}
I'm trying to think of the most elegant solution to allow for my custom Dusk command to be part of the application and accessible locally, without throwing errors on production.
One idea: Write my Dusk command as its own package, that's also only in require-dev.
Any other ideas?
I just took a look at the API, you could do this:
You could move your command to App\Console\Commmands\Local\DuskCommand.php.
By default, if you check the commands() method in the Kernel, it's only going to load commands found in App\Console\Commands. This will not include the sub-directories.
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
This is the default commands() method. You could switch this implementation to the one below:
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$paths = [
__DIR__ . '/Commands'
];
if(app()->environment('local')) {
$paths[] = __DIR__ . '/Commands/Local';
}
$this->load($paths);
require base_path('routes/console.php');
}
So, in local, we are also going to load commands based in App\Console\Commands\Local.
Admittedly, I didn't give this a try myself, but I am assuming that it should work.
Edit: I gave it a try and it seems to be working just fine. I thought, I'd try to explain it a bit more. Basically, after doing a composer dump-autoload, Laravel is listening to this event and doing two things:
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"#php artisan package:discover --ansi"
]
The second one is trying to run the Auto-Package Discovery command and this is where it will fail. The Artisan executable actually boots the application with the Console Kernel.
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);
While resolving the Kernel, it will also try to boot up the Commands that it needs so that they are available to Artisan, and this is where it fails.
However, like I mentioned above, if you only boot the Commands you need in production, this issue won't happen.
Accepted answer seems not to work in Laravel 6. This worked for me:
Create your command with php artisan make:command YourCommand and move it to app/Console/Local.
Change its namespace to App\Console\Local.
Then, in app/Console/Kernel.php:
protected function commands()
{
$paths = [
__DIR__ . '/Commands'
];
if(app()->environment('local')) {
$paths[] = __DIR__ . '/Local';
}
$this->load($paths);
require base_path('routes/console.php');
}
Enjoy ;)

My custom service provider doesn't work in laravel

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 .

Lumen make:command

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

Categories