CodeIgniter Forums
load Controller in Controller, CI3 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: load Controller in Controller, CI3 (/showthread.php?tid=61706)



load Controller in Controller, CI3 - zeos - 05-07-2015

hi
I can't load controller in another controller in CI3, but in CI2 i do this without problem!!!
for test plz create MY_Controller in core and load a test library or model or etc
then in welcome controller or each controller you want, load another controller like my codes then see errors, i cant know why this doesn't work in CI3 plz help me Sad

and other question why CI2 benchmark is very better than CI3? it isn't bad? do you recommended me for use CI2?

"please sorry if you found any mistake in my english"

---

MY_Controller:
PHP Code:
class MY_Controller extends CI_Controller
{
    public function 
__construct()
    {
        
parent::__construct();

        
$this->load->library('cart'); // any library or model you like, you can load
    
}


and this is my welcome controller
PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends MY_Controller {

    public function 
index()
    {
        
$this->load->library('../controllers/_404');
        
$this->_404->index();
    }


and my _404 controller
PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class 
_404 extends MY_Controller
{
    public function 
index()
    {
        echo 
'404';
    }




RE: load Controller in Controller, CI3 - Jammyman - 07-31-2015

Not sure if I understand your problem correctly, but I was able to load controller where ever I wan't without errors in CI3 too after I moved most of the autoload stuff to MY_Controller instead autoload config. If Session, Form_validation, Pagination etc. are in autoload, you get bunch of different errors.

http://www.jammeri.com/post/loading-controller-wherever-you-want-with-codeigniter-30


RE: load Controller in Controller, CI3 - ignitedcms - 08-02-2015

Why on earth would you want to do something like this?


RE: load Controller in Controller, CI3 - zeos - 08-03-2015

for custom 404 error page i would do this


RE: load Controller in Controller, CI3 - mwhitney - 08-04-2015

(08-03-2015, 12:21 PM)zeos Wrote: for custom 404 error page i would do this

Why wouldn't you use show_404() or the 404_override route?


RE: load Controller in Controller, CI3 - tatarespecia - 09-20-2019

(07-31-2015, 11:30 AM)Jammyman Wrote: Not sure if I understand your problem correctly, but I was able to load controller where ever I wan't without errors in CI3 too after I moved most of the autoload stuff to MY_Controller instead autoload config. If Session, Form_validation, Pagination etc. are in autoload, you get bunch of different errors.

http://www.jammeri.com/post/loading-controller-wherever-you-want-with-codeigniter-30

The link you gave is broken. Please update.


RE: load Controller in Controller, CI3 - ciadmin - 09-20-2019

(09-20-2019, 04:18 PM)tatarespecia Wrote: The link you gave is broken. Please update.

I trust you realize this is a 4 year old thread. Jammyman hasn't been active on the forum since early 2016, so you might have a long wait for a reply.


RE: load Controller in Controller, CI3 - yuma2020 - 07-04-2022

(05-07-2015, 06:04 AM)zeos Wrote: hi
I can't load controller in another controller in CI3, but in CI2 i do this without problem!!!
for test plz create MY_Controller in core and load a test library or model or etc
then in welcome controller or each controller you want, load another controller like my codes then see errors, i cant know why this doesn't work in CI3 plz help me Sad

and other question why CI2 benchmark is very better than CI3? it isn't bad? do you recommended me for use CI2?

"please sorry if you found any mistake in my english"

---

MY_Controller:
PHP Code:
class MY_Controller extends CI_Controller
{
 public function 
__construct()
 {
 
parent::__construct();

 
$this->load->library('cart'); // any library or model you like, you can load
 
}


and this is my welcome controller
PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class 
Welcome extends MY_Controller {

 public function 
index()
 {
 
$this->load->library('../controllers/_404');
 
$this->_404->index();
 }


and my _404 controller
PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class 
_404 extends MY_Controller
{
 public function 
index()
 {
 echo 
'404';
 }


Try anywhere
PHP Code:
$this->SomeController =  new SomeController
more simply is imposible, Good understanding, little word