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:

<?php
require_once('../laravel/app/Http/Controllers/NotificationsController.php');

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:

    $schedule->command('inspire')
                     ->hourly();
    

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,

$schedule->command('processQueuedNotifications')
                 ->everyMinute();
//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: http://laravel.com/docs/5.0/commands / http://symfony.com/doc/current/components/console/introduction.html

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料