问题
I am working with code-igniter and running a function from cron job.
class event extends CI_Controller {
public function newEvent()
{
//some process here using the parameter
}
}
cron command:
* */2 * * * /usr/bin/php /var/www/project/index.php 'event/newEvent/parameter'
I want to pass the parameter like written in cron command and do some process with that parameter in newEvent function.
What extra code I should write in my function to receive the parameter from cron command.
Thanks
回答1:
CI explains how to execute command from the command line in their documentation
php index.php event newEvent parameter
回答2:
You can simply add a parameter to your function.
class event extends CI_Controller {
public function newEvent($parameter)
{
//some process here using the parameter
echo $parameter;
}
}
The default routing engine will proceed your parameter and pass it to your function. If that parameter is optional, feel free to initialize it to something by default.
public function newEvent($parameter = 'default')
{
//some process here using the parameter
echo $parameter;
}
Edit: After reading's @dm03514 answer, it seems that the documentation recommends to call your application with spaces instead of slashes.
The cron command should be
* */2 * * * /usr/bin/php /var/www/project/index.php event newEvent parameter
回答3:
I have tried this and it's working for me.
* */2 * * * /usr/bin/curl http://domain.com/index.php/event/newEvent/parameter
来源:https://stackoverflow.com/questions/15682977/how-to-pass-parameter-from-cron-command-to-codeigniter-function