Call Controller method of CodeIgniter outside Appl

2020-02-14 10:08发布

问题:

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 possible to call controller method from directory which is outside Application Directory?

Thanks.

回答1:

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.



标签: codeigniter