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
Related
I'm running unit tests via docker like thus
doby phpunit
docker-compose run --rm -v $SSH_AUTH_SOCK:/tmp/ssh_auth_sock -e SSH_AUTH_SOCK=/tmp/ssh_auth_sock php bash -c 'bin/phpunit --filter SendEmailsCommandTest'
I receive the following error
Doctrine\Persistence\Mapping\MappingException: File mapping drivers must have a valid directory path, however the given path [/var/source/application/models/Content.php] seems to be incorrect!
Content.php
<?php
namespace MyDomain\Model;
use Exception as ModelException;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* #ODM\Document(
* db="mydb",
* collection="Content"
* )
*/
class Content
{
/** #ODM\Id */
protected $id;
/**
* #ODM\Field(type="string")
*/
protected $contentId;
/**
* #ODM\Field(type="string")
*/
protected $content;
...
}
I've logged into the running docker container and the files exist in the path above.
/var/source/application/models
- Content.php
- ...
Has anyone had a similar problem running tests through docker and doby?
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?
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 .