Call Controller method of CodeIgniter outside Application directory

前端 未结 1 838
梦如初夏
梦如初夏 2020-12-21 05:56

I have one directory \"Store\" which is outside Application directory of CodeIgniter. Here I need to call one controller method from \"Store\" directory.

Is is poss

相关标签:
1条回答
  • 2020-12-21 06:06

    As far as I know, using $this->load (Loader class i.e.), you can't. Even if you directly include like in the following way:

    application/controllers/test.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    require('/absolute/path/to/dummy.php');
    
    class Test extends CI_Controller {
        public function handle(){
            $d = new Dummy();
            $d->handle();
        }
    }
    

    dummy.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Dummy extends CI_Controller {
        public function handle(){
            // do something here
        }
    }
    

    It(including) won't work because you specifically disallowed direct access! But, if you don't disallow that, then, your controller code is prone to exploitation, and you won't have a problem.


    So, one way to do it if the other controller part of another codeigniter project is, using command line.

    dummy.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access 
    
    class Test extends CI_Controller {
        public function handle(){
            $d = new Dummy();
            $d->handle();
        }
    }
    

    application/controllers/test.php

    public function handle(){
        exec("cd /absolute/path/to/dummyproject; php index.php dummy handle;");
    }
    

    You can further find out how to pass command line arguments too.

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