我应该在CodeIgniter中哪里需要vendor / autoload.php?

时间:2017-11-10 19:03:41

标签: php codeigniter

我想添加一些将为每个控制器运行的代码。将代码添加到CodeIgniter的CI_CONTROLLER类似乎是非常规的。

在哪里包含您想要为每个控制器运行的代码的正确位置?

以下是代码:

require_once 'vendor/autoload.php';

$bugsnag = Bugsnag\Client::make("my-secret-key-is-here");
Bugsnag\Handler::register($bugsnag);

这些类都来自与Composer一起安装的依赖项。

我怀疑我应该创建一个帮助器,并将其包含在application/config/autoload.php中。但对CodeIgniter来说是新手,所以不确定约定。

编辑:我正在使用CodeIgniter 3.1.6。

4 个答案:

答案 0 :(得分:3)

如果您想在CodeIgniter的生命周期中的不同点执行任意代码,您可以使用hooks功能。

官方文档: https://codeigniter.com/user_guide/general/hooks.html

<强> 1。启用挂钩

  • 转到/application/config/config.php
  • 搜索enable_hooks并将其设置为true:$config['enable_hooks'] = TRUE;

<强> 2。将所需的代码添加到CodeIgniter的钩子文件中:

  • 转到/application/config/hooks.php
  • 选择要挂钩的所需生命周期(请参阅上面的文档链接以获取列表)

  • 将代码添加到生命周期,例如$hook['pre_controller'] = function(){... your code goes here ...}

对于这个问题的例子,我的hooks.php看起来像这样:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

// This is the code I added:
$hook['pre_system'] = function(){
  require_once 'vendor/autoload.php';

  $bugsnag = Bugsnag\Client::make("my-client-key");
  Bugsnag\Handler::register($bugsnag);
}

?>

答案 1 :(得分:2)

我只是扩展Controller类。

See "Extending Core Class"

  

&#34;如果您需要做的只是向现有库中添加一些功能 - 可能会添加一两个方法 - 那么它就太过分了用您的版本替换整个库。在这种情况下,最好只是扩展类。&#34;

     

...

     

&#34;提示:将使用类中与父类中的方法完全相同的任何函数而不是本机函数(这称为“方法重写”)。这允许您大幅改变CodeIgniter核心。&#34;

class MY_Controller extends CI_Controller {

    ....

}

你放入的任何函数都会被添加到核心,否则,如果你使用与现有方法相同的名称,它将只替换那个方法。

您可以将其命名为MY_Controller.php并将其放在application/core/中,然后将其选中以自动覆盖CI_Controller 。< / p>

  

如果要扩展Controller核心类,请确保在应用程序控制器的构造函数中扩展新类。

class Welcome extends MY_Controller {

        public function __construct()
        {
                parent::__construct();
                // Your own constructor code
        }

        public function index()
        {
                $this->load->view('welcome_message');
        }
}

看起来您也可以使用此处所述的pre_systempre_controller挂钩:

https://www.codeigniter.com/user_guide/general/hooks.html

答案 2 :(得分:1)

假设它是CodeIgnitor 3.X 转到 application / config / config.php 并更改:

Dim rng As Range, destRow As Long
    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim c As Range '-- this is used to store the single cell in the For Each loop

    Set shtSrc = Sheets("Sheet1") ' Sets "Sheet1" sheet as source sheet
    Set shtDest = Sheets("Sheet2") 'Sets "Sheet2." sheet as destination sheet
    destRow = 5 'Start copying to this row on destination sheet

' >> Set range to search for dates in Look Ahead period <<
    Set rng = Application.Intersect(shtSrc.Range("P:P"), shtSrc.UsedRange)

' >> Look for matching dates in columns C to D <<
    For Each c In rng.Cells
        If (c.value >= startDate And c.value <= endDate) Or _
    (c.Offset(0, 1).value >= startDate And c.Offset(0, 1).value <= endDate) Then ' Does date fall between start and end dates? If Yes, then copy to destination sheet

            c.Offset(0, -2).Resize(1, 12).Copy _
                          shtDest.Cells(destRow, 1) 'Copy a 12 cell wide block to the other sheet, paste into Column A on row destRow
            destRow = destRow + 1

' > Ends search for dates <
        End If
    Next

$config['composer_autoload'] = FALSE;

上方行以下

$config['composer_autoload'] = TRUE;
控制器中的

包括

require_once APPPATH.'vendor/autoload.php';

来自How to use composer packages in codeigniter?

答案 3 :(得分:-1)

转到 application / config / config.php 并将$config['composer_autoload'] = FALSE;设置为TRUE

  

启用此设置将告诉CodeIgniter查找Composer   在 application / vendor / autoload.php 中打包自动加载程序脚本。

因此,您无需拨打require_once 'vendor/autoload.php';