How to pass parameter from cron command to codeigniter function?

六月ゝ 毕业季﹏ 提交于 2019-12-23 02:06:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!