I need a custom command in my php artisan.
I have created the command file via artisan shell.
But when I execute it via php artisan it throws it as undefined.
It is also confusing for me how to add arguments to my command.
Here is my whole command file:
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class createLibrarySet extends Command
{
/**
* The console command name.
*
* #var string
*/
protected $name = 'createLibrarySet';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Creates a Library Set.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
$dir = mkdir(app_path()."/".library."/".$this->library_name);
$coreName = $this->library_name;
$serivceProvider = $coreName."."."ServiceProvider.php";
$library = $coreName."."."Library.php";
$facade = $coreName."."."Facade.php";
$interface = $coreName."."."Interface.php";
fopen($dir."/".$library, "w");
fopen($dir."/".$facade, "w");
fopen($dir."/".$serivceProvider, "w");
fopen($dir."/".$interface, "w");
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
You have to add the command to your app/start/artisan.php:
Artisan::resolve('createLibrarySet');
Now you should be able to see in the
php artisan
listing.
To get your arguments, you just have to:
$email = $this->argument('email');
And options
$force = $this->option('force');
Related
i have created following command in laravel using command php artisan make:command RedisDataListener
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RedisDataListener extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'listen:subscribe';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Subscribe to redis channel for listening events';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle() {
$value = cache()->has('tracking_application:mykey') ? cache()->get('tracking_application:mykey') : 'no';
print_r(array(
$value
));
}
}
but when i'm using cache()->get('key') it is not working. Same function works fine when i use in controller.
i'm using redis as the cache server.
also tried with cache::get() but nothing happen.
I am following this Cron Job with Laravel 4 as tutorial for cron in laravel 4 but it seems it's not working. This is my /app/commands/ReleaseOptionBooking.php
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ReleaseOptionBooking extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'release:option';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Check booking expirations';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
$to = "bikegearup#gmail.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: admin#theskitrip.ca" . "\r\n";
mail($to,$subject,$txt,$headers);
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
}
and this is on /app/start/artisan.php
Artisan::add(new ReleaseOptionBooking);
and on my server's crontab -e
*/10 * * * * php /var/www/myproject/protected/artisan release:option
do anyone have an idea about my case?
I got this error:
[RuntimeException]
Not enough arguments.
release:option [--example[="..."]] example
Do you have ssh access to your server?
Could you try to execute command manually:
php /var/www/myproject/protected/artisan release:option
Do you see any error messages? Any output?
Remove these examples from your command code:
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return array(
array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}
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'm not sure why __call doesn't work in this scenario. Does it have to do with how Command is implemented or am I doing something wrong?
class MyCommand extends Command
{
protected $reports = [];
/**
* The console command name.
*
* #var string
*/
protected $name = 'test';
/**
* The console command description.
*
* #var string
*/
protected $description = '';
public function __call($methodName, $args) {
return call_user_func_array($methodName, $args);
}
}
You can override the execute method if you are trying to extend the base Command class. This will essentially
/**
* Execute the console command.
*
* #param \Symfony\Component\Console\Input\InputInterface $input
* #param \Symfony\Component\Console\Output\OutputInterface $output
* #return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// your custom logic here to be ran before all handlers, e.g.
// setting up env variables, prompting user, ...
$method = method_exists($this, 'handle') ? 'handle' : '__invoke';
return (int) $this->laravel->call([$this, $method]);
}
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.