It's pretty clear how to create a simple command for the CLI in Laravel and this goes also for creating arguments, but I can't seem to find a simple documentation on how to create options for the CLI.
I want to make it so that the user can add options after the argument.
So for example:
php artisan cmd argument -a
php artisan cmd argument -a -c -x
How do I implement such a structure in the class below?
Updated code
There are indeed a few possible solutions. It was actually quit easy.
class cmd extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'cmd {argument}
{--s : description}
{--x : description}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$var = $this->argument('argument');
$options = $this->options();
new Main($var,$options);
}
}
There are a lot of posible solutions for this, but I prefer to add optional arguments and if they exist do determinate actions with the ? that means argument can exist or not, plus * this means can be more tan one, like this:
class cmd extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'cmd {argument} {-extra*?}';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$var = $this->argument('argument');
if($this->argument('-extra')) {
//do things if -extra argument exists, it will be an array with the extra arguments value...
}
new Main($var);
}
}
There a whole doc section dedicated to creating commands from scratch and it's well documented. Look through it.
https://laravel.com/docs/5.4/artisan
If you need to learn from examples then have a look here into all the laravel's built in console commands.
vendor/laravel/framework/src/Illuminate/Foundation/Console
Related
I have Controller:
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct(){}
public function index()
{
pcntl_fork();
}
}
Then I call index() by HTTP request, And I get:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to undefined function App\Http\Controllers\pcntl_fork()
Then I try this:
class CodeSheet extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'code_sheet';
/**
* 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()
{
pcntl_fork();
echo "1\n";
}
}
Then I call this command:
vagrant#homestead:~$ php artisan code_sheet
1
1
So my question is, Why I can call pcntl_fork() in command, but can't in HTTP request?
The PCNTL extension is disabled in web environments (it probably even only compiles for the CLI SAPI), this has nothing to do with Laravel.
The reason for that is simple - the web server itself (or PHP-FPM) is in control of process management, so using this extension would create a conflict with it.
It's also not available under Windows as it's a UNIX-specific thing.
trying to setup an example console command on laravel 5.2 but it's not working
I ran php artisan make:console CoolCommand
Here's my file
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CoolCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'be:cool';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Allows you to be cool';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
echo "Yes you are very cool!";
}
}
When I hit php artisan the command is not listed under the given signature
What am I missing?
Thanks
If it's not listed when you type php artisan, you forgot to register the command as described here. Open app/Console/Kernel.php and enter your command.
protected $commands = [
Commands\CoolCommand::class
];
Consider the following:
This is a conf file, in: conf/log_rotation/laravel_rotate.conf
storage/logs/laravel.log {
missingok
notifempty
compress
size 1024k
daily
create 0640
}
Is triggered by this laravel task run daily:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Logrotater extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'log_rotater';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Rotates the logs on an hourly basis';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
exec("logrotate -f config/log_rotation/tweet_rotate.conf");
exec("logrotate -f config/log_rotation/laravel_rotate.conf");
}
}
When I do: php artisan log_rotater I get:
error: config/log_rotation/laravel_rotate.conf:1 unknown option 'storage' -- ignoring line
error: config/log_rotation/laravel_rotate.conf:8 unexpected }
Ideas as to why I am getting this error?
logrotate requires path provided to be absolute, otherwise it treats given string as configuration option. That's why you're getting unknown option storage error.
Replace
storage/logs/laravel.log
with the absolute path.
i set up a cronjob in cpanel, i'm using laravel 4.2. i also set to send the cronjob to my email and below yellow part is the error i received in email.
[InvalidArgumentException]
There are no commands defined in the "user" namespace.
//laravel command.php
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class CronDelFilesCommand extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'user:active';
/**
* 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 fire()
{
echo "aaa";
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
//command in cpanel
* * * * * /usr/bin/php /home/project/public_html/artisan user:active > /home/project/public_html/log.txt
what's wrong? what's the error mean?
UPDATED
thanks to #KristianHareland i use wget. :)
I think you forget to add this command to artisan app/start/artisan.php.
You shoud wright:
Artisan::add(new CronDelFilesCommand());
And don't forget about composer dump-autoload
More info you can find here.
I have created an artisan command called sendUnreadNotifications this triggers the system to send users emails if they have unread notifications. Eventually this will be run via a cron job, and a user can either have hourly updates or daily updates.
For this reason I am wanting to send an argument with my command, something like this,
php artisan command:name sendUnreadNotifications H --env=local
However running this, I get the following error,
[RuntimeException]
Too many arguments.
My code, looks like this,
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class sendUnreadNotifications extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'command:name';
/**
* 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 fire()
{
$request = Request::create('api/notifications/send/unread', 'GET', array($this->getArguments()));
Route::dispatch($request)->getContent();
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
array('frequency', InputArgument::OPTIONAL, 'How often should the email be sent', 'H'),
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
I cannot see why I would be getting the too many arguments exception?
You do only have one argument set, for frequency
protected function getArguments()
{
return array(
array('frequency', InputArgument::OPTIONAL, 'How often should the email be sent', 'H'),
);
}
so the
php artisan command:name sendUnreadNotifications H --env=local
here the H is the argument that is too much. You should change your command's name to what you want to do, the command names need to be unique btw...
Change this:
protected $name = 'command:name';
to
protected $name = 'send:unreadNotifications';
and run your job with
php artisan send:UnreadNotifications H
and it will work.