【问题标题】:Cron job - Codeigniter - Class 'CI_Controller' not foundCron 作业 - Codeigniter - 找不到类“CI_Controller”
【发布时间】:2012-12-01 23:37:39
【问题描述】:

我正在尝试使用此命令在 cpanel 上创建一个 cron 作业:

/usr/bin/php -q /home/mystuff/public_html/application/controllers/scripts.php scripts release_reviews

我的scripts.php控制器如下:

<?php

class Scripts extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        if (!$this->input->is_cli_request()) show_error('Direct access is not allowed');

    }

    public function release_reviews()
    {
        echo release_reviews(); //where the actual logic will sit once the cron job works
    }
}

我在尝试运行 cron 作业时得到的反馈: 致命错误:在第 3 行的 /home/mystuff/public_html/application/controllers/scripts.php 中找不到类“CI_Controller”

我找不到任何证据表明有人和我有同样的问题 - 与此相关的大多数主题都和我一样,而且显然效果很好。

提前非常感谢!

【问题讨论】:

    标签: php codeigniter cron


    【解决方案1】:

    要通过命令行访问 CodeIgnter,您需要调用 index.php 文件,而不是您的控制器。

    php /home/mystuff/public_html/index.php scripts release_reviews
    

    文档:http://ellislab.com/codeigniter/user-guide/general/cli.html

    【讨论】:

    • 我已经尝试过了,但它现在显示“404 Page Not Found”。然而,index.php 肯定位于我所说的位置。
    • 我实际上也只是在尝试——如果我添加“本地”,cron 作业就不再运行了。所以这似乎不是问题。
    • @Davor:我不知道该告诉你什么。 CodeIgniter 是否已完全配置?
    • 嗯,我有一个完整的网站,所以 CI 已完全配置(至少 CI_Controller 或 MY_Controller 在应用程序中工作)。感谢您的帮助,希望奇迹发生,并在某个时候弹出解决方案!
    • 您使用的是哪个 CI 版本? CLI 在 1.7.x 系列中有点不稳定。
    【解决方案2】:

    没有CI_Controller 类。 CRON 作业仅加载该文件,因此找不到任何 CI_Controller。您必须在类定义之前包含 CI_CONTROLLER

    类似的东西

    <?php
    
    require_once('path_to_CI_controller');
    
    class Scripts extends CI_Controller
    {
        ...
    

    【讨论】:

      【解决方案3】:

      在路由文件中使用了以下内容,并且成功了! 我想通了。我的新手问题是需要配置参数。

      例如

      `$route['pdfscript/runmethod']  = "batch/pdfscript/runmethod";
      $route['pdfscript/runmethod/(:any)'] = "batch/pdfscript/runmethod/$1";'
      
      `pdfscript.php
      <?php 
      class pdfscript extends CI_Controller {
      public function runmethod($file) {
      echo $file;
      .....`
      

      命令行> php.exe index.php pdfscript runmethod 文件名

      【讨论】:

        猜你喜欢
        • 2012-02-11
        • 1970-01-01
        • 2013-11-04
        • 1970-01-01
        • 2013-03-05
        • 1970-01-01
        • 1970-01-01
        • 2011-01-15
        • 2011-11-11
        相关资源
        最近更新 更多