Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Controller method of CodeIgniter outside Application directory

Tags:

codeigniter

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.

like image 915
aanandj23 Avatar asked Apr 01 '26 12:04

aanandj23


1 Answers

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.

like image 122
Prasanth Avatar answered Apr 03 '26 02:04

Prasanth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!