【问题标题】:Bootstrap CodeIgniter without Routing to a Controller没有路由到控制器的引导 CodeIgniter
【发布时间】:2011-11-01 01:12:26
【问题描述】:

对于命令行脚本、工具和(我当前的用例)从另一个应用程序访问 CodeIgniter 环境 - 有没有办法在不启动路由过程的情况下“引导”CI?

例如,使用 Zend 框架,我可以设置普通常量,创建 Zend_Application 实例,引导应用程序,但只要我不调用:

$application->run();

它不会启动路由过程,但我仍然可以访问应用程序的资源/模型/等。

有没有办法用 CodeIgniter 做到这一点?

【问题讨论】:

  • 很少使用 CI,我从未见过。但是我没有挖那么多!
  • 据我所知,这是不可能的。尽管路由器在某种程度上是它自己的一个组件,但在那个级别上的所有东西在 CI 中几乎是一起烘焙的。其次,CI只能存在一次,不能实例化多个控制器并排相邻。
  • @hakre 我想我可以强制路由到单个控制器(我看到的 ExpressionEngine 代码基本上就是这样做的),但是你说一旦控制器完成,CI 可能会 die 在我身上,让我没有任何用处。
  • 我在一两个星期前试图破解它,但我没时间了,没能成功。基本问题是静态函数变量。为上下文创建一个全局数组,因此它允许您使用上下文重载静态垃圾(我猜它仍然存在,因为 PHP 4 的疯狂)。然后您应该能够实例化多个控制器(在每个上下文的控制器上),但是不知道如何删除路由。

标签: php codeigniter


【解决方案1】:

有一个 wordpress codeigniter 插件,它有一个引导文件。我认为你可以使用它。

我玩了一下,如果您尝试加载 CodeIgniter 的模型/库等,您可以这样做:

  1. 从上面的链接下载插件并解压。
  2. 有一个文件夹ci_mods 包含引导文件。将此文件夹复制到您的 codeigniter 目录(系统和应用程序文件夹所在的位置)。
  3. ci_mods 文件夹包含两个文件 - index.phpCodeIgniter.php。编辑 index.php 并编辑路径,例如系统路径将变为$system_path = dirname(__FILE__) . '/../system';
  4. 为防止路由,请注释行号。 CodeIgniter.php 中的 342

现在要在文件中使用它,只需包含 ci_mods/index.php 即可-

$ci = & get_instance();
$ci->load->model('usermodel','',TRUE);

$q = $ci->usermodel->getUsers();

我还没有完全探索它。它可能更有用。

这是我修改后的 index.php

<?php

  // WARNING: This is not an original CI file

  // backuping query string
  $query_string = urlencode($_SERVER['QUERY_STRING']);

  // destroying the $_GET array (but reconstructed when CI leaves)
  $_GET = array();

  // let's use CI a bit to recover our $_GET array
  $_SERVER['PATH_INFO'] = '/cidip/recover_get_array/'.$query_string;  

/*
 *---------------------------------------------------------------
 * APPLICATION ENVIRONMENT
 *---------------------------------------------------------------
 *
 * You can load different configurations depending on your
 * current environment. Setting the environment also influences
 * things like logging and error reporting.
 *
 * This can be set to anything, but default usage is:
 *
 *     development
 *     testing
 *     production
 *
 * NOTE: If you change these, also change the error_reporting() code below
 *
 */
    define('ENVIRONMENT', 'development');
/*
 *---------------------------------------------------------------
 * ERROR REPORTING
 *---------------------------------------------------------------
 *
 * Different environments will require different levels of error reporting.
 * By default development will show errors but testing and live will hide them.
 */

if (defined('ENVIRONMENT'))
{
    switch (ENVIRONMENT)
    {
        case 'development':
            error_reporting(E_ALL);
        break;

        case 'testing':
        case 'production':
            error_reporting(0);
        break;

        default:
            exit('The application environment is not set correctly.');
    }
}

/*
 *---------------------------------------------------------------
 * SYSTEM FOLDER NAME
 *---------------------------------------------------------------
 *
 * This variable must contain the name of your "system" folder.
 * Include the path if the folder is not in the same  directory
 * as this file.
 *
 */
    $system_path = dirname(__FILE__) . '/../system';

/*
 *---------------------------------------------------------------
 * APPLICATION FOLDER NAME
 *---------------------------------------------------------------
 *
 * If you want this front controller to use a different "application"
 * folder then the default one you can set its name here. The folder
 * can also be renamed or relocated anywhere on your server.  If
 * you do, use a full server path. For more info please see the user guide:
 * http://codeigniter.com/user_guide/general/managing_apps.html
 *
 * NO TRAILING SLASH!
 *
 */
    $application_folder = dirname(__FILE__) . '/../application';

/*
 * --------------------------------------------------------------------
 * DEFAULT CONTROLLER
 * --------------------------------------------------------------------
 *
 * Normally you will set your default controller in the routes.php file.
 * You can, however, force a custom routing by hard-coding a
 * specific controller class/function here.  For most applications, you
 * WILL NOT set your routing here, but it's an option for those
 * special instances where you might want to override the standard
 * routing in a specific front controller that shares a common CI installation.
 *
 * IMPORTANT:  If you set the routing here, NO OTHER controller will be
 * callable. In essence, this preference limits your application to ONE
 * specific controller.  Leave the function name blank if you need
 * to call functions dynamically via the URI.
 *
 * Un-comment the $routing array below to use this feature
 *
 */
    // The directory name, relative to the "controllers" folder.  Leave blank
    // if your controller is not in a sub-folder within the "controllers" folder
    // $routing['directory'] = '';

    // The controller class file name.  Example:  Mycontroller.php
    // $routing['controller'] = '';

    // The controller function you wish to be called.
    // $routing['function'] = '';


/*
 * -------------------------------------------------------------------
 *  CUSTOM CONFIG VALUES
 * -------------------------------------------------------------------
 *
 * The $assign_to_config array below will be passed dynamically to the
 * config class when initialized. This allows you to set custom config
 * items or override any default config values found in the config.php file.
 * This can be handy as it permits you to share one application between
 * multiple front controller files, with each file containing different
 * config values.
 *
 * Un-comment the $assign_to_config array below to use this feature
 *
 */
    // $assign_to_config['name_of_config_item'] = 'value of config item';



// --------------------------------------------------------------------
// END OF USER CONFIGURABLE SETTINGS.  DO NOT EDIT BELOW THIS LINE
// --------------------------------------------------------------------

/*
 * ---------------------------------------------------------------
 *  Resolve the system path for increased reliability
 * ---------------------------------------------------------------
 */

    // Set the current directory correctly for CLI requests
    if (defined('STDIN'))
    {
        chdir(dirname(__FILE__));
    }

    if (realpath($system_path) !== FALSE)
    {
        $system_path = realpath($system_path).'/';
    }

    // ensure there's a trailing slash
    $system_path = rtrim($system_path, '/').'/';

    // Is the system path correct?
    if ( ! is_dir($system_path))
    {
        exit("Your system folder path does not appear to be set correctly. Please open the following file and correct this: ".pathinfo(__FILE__, PATHINFO_BASENAME));
    }

/*
 * -------------------------------------------------------------------
 *  Now that we know the path, set the main path constants
 * -------------------------------------------------------------------
 */
    // The name of THIS file
    define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));

    // The PHP file extension
    define('EXT', '.php');

    // Path to the system folder
    define('BASEPATH', str_replace("\\", "/", $system_path));

    // Path to the front controller (this file)
    define('FCPATH', str_replace(SELF, '', __FILE__));

    // Name of the "system folder"
    define('SYSDIR', trim(strrchr(trim(BASEPATH, '/'), '/'), '/'));


    // The path to the "application" folder
    if (is_dir($application_folder))
    {
        define('APPPATH', $application_folder.'/');
    }
    else
    {
        if ( ! is_dir(BASEPATH.$application_folder.'/'))
        {
            exit("Your application folder path does not appear to be set correctly. Please open the following file and correct this: ".SELF);
        }

        define('APPPATH', BASEPATH.$application_folder.'/');
    }

/*
 * --------------------------------------------------------------------
 * LOAD THE BOOTSTRAP FILE
 * --------------------------------------------------------------------
 *
 * And away we go...
 *
 */
  require_once dirname(__FILE__) . '/CodeIgniter'.EXT;  

/* End of file index.php */
/* Location: ./index.php */

【讨论】:

    【解决方案2】:

    致下一个不得不与 CI 打交道的倒霉蛋。对于 CI 1.7 版,您只需执行此操作即可加载实例。假设你已经定义了 ROOT 文件夹

    <?php
    define('APPPATH', ROOT . DS . 'application' . DS);
    define('BASEPATH', ROOT . DS . 'system' . DS);
    define('EXT', '.php');
    
    require_once(BASEPATH . 'codeigniter/Common'.EXT);
    require_once(BASEPATH . 'codeigniter/Base5'.EXT);
    $controller =& load_class('Controller');
    

    此时您可以随意调用 get_instance 或 CI_Base::get_instance();

    发现它对于在我们迁移旧代码库时针对旧代码库进行单元测试进行黑客攻击很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-09
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      相关资源
      最近更新 更多