Accessing CodeIgniter super object from external php script outside codeigniter installation

前端 未结 2 1588
醉话见心
醉话见心 2020-12-18 15:07

I have been trying so long but couldn\'t find a solution. For some reason I need to access the codeigniter super object get_instance() from an external php script which is l

相关标签:
2条回答
  • 2020-12-18 15:28

    CI's main index.php sets the paths of the system and application folders. If you are including index.php from another directory, these paths are set relative to your "including" directory.

    Try changing the following lines in index.php

    $system_path = 'system';
    // change to...
    $system_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'system';
    
    // and...
    $application_folder = 'application';
    // change to...
    $application_folder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'application';
    

    The dirname(__FILE__) will give you the absolute path of the index.php even if you include it somewhere else.

    0 讨论(0)
  • 2020-12-18 15:34

    The problem with loading codeigniter from external file outside codeigniter installation is solved using the first answer which I marked as accepted. And the problem with calling default controller/method was resolved using constant (define). Here is the updated code for external.php file:

    <?php
    // Remove the query string
    $_SERVER['QUERY_STRING'] = '';
    // Include the codeigniter framework
    define("REQUEST", "external");
    ob_start();
    require('./new/index.php');
    ob_end_clean();
    ?>
    

    And here is the default controller method:

    public function index($flag = NULL) 
     {
      if (constant("REQUEST") != "external")
      {
        // some code here
      }
     }
    

    Thanks very much to contributor hyubs. @hyubs

    0 讨论(0)
提交回复
热议问题