dongtan9066 2015-06-05 01:14
浏览 48

Laravel设置,尝试让cronjob在php laravel文件中调用一个函数

Original Post

Good evening folks. I have a laravel setup and I'm trying to have a cronjob execute a php function to a file within the laravel project directory.

I am getting class and name space errors when I try to do something like this:


and then calling the processQueuedNotifications() function.

This of course gives me errors, what is the correct way to call my function within the laravel directory? I need to call this function as this function has all the correct namespaces and extended controllers necessary to execute the function properly.

Update 1:

Thanks to @michael, I've been made aware of a component in Laravel called commands.

So I ran this code: php artisan make:console processQueuedNotifications and it created some files in the console directory.

Currently exploring on what to do next.

  1. After checking out the Events class which the kernel.php file makes use of, I noticed that this class provides an easy to use interface for me to create cron jobs on the fly. Am I correct in think so?

  2. I notice there is not function to run a cron job every minute, is it safe to edit the Events class file without it being overwritten by future make:console commands, or laravel updates?

  3. I saw this code in the kernel.php file:


So is this the place you wanted me to add my function? as I notice that the inspire function is something automatically created for me to understand what's going on?

So I would write,

//Providing it's safe to edit the Event's class or figure out a clean way of doing so without my code being deleted in the future on Laravel updates.
  • 写回答

2条回答 默认 最新

  • dongtu4028 2015-06-05 01:21

    A very convenient way is to use laravels console component. You can create a new command by issuing

    php artisan make:console

    And find it thereafter in your app/console directory. Make sure to enable the command in the Kernel.php file once created.

    Simply call your class or whatever you want to run via cron from inside the command. The console command itself is callable via cli just as you would run one of laravels php artisan ... commands. You can set this in the file created for you. For example, you can then call the file from everywhere you want with

    /usr/bin/php /path/to/file/artisan my:command

    You can set options and arguments if you need to.

    Here's the documentation: /

    There's an array in kernel.php you need to register your class (include the namespace) in. After that it is callable via cli. For a start, have a look on arguments and options you can initialize in case you need to make different requests on your controller class. (The filename you have chosen for your console command, is an argument. You can make them required or optional for your own commands. )

    Within your file, you can create them by simply creating an array in the appropriate method with these values:

     [$name, $mode, $description, $defaultValue]

    have a look at the docs or Jeffrey's laracasts, they are very good.

    To only call your class from the console command, it's enough to name your command in the above section of the file and call you controller like

    (new namespace\controller)->method();
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 C#点击按钮的时候的循环次数就是最后一次,如何是循环第几次的值?
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写
  • ¥15 graphpad prism 三因素重复测定报错