How do I load one controller into another?

Asked

Viewed 5,398 times

1

Well, my doubt is how I load a controller/method into another controller, in Codeigniter 2.2.2

<?php if(! defined('BASEPATH')) exit('No direct script access allowed');

class Inicio extends CI_Controller {
   public function index(){
      $this->load->library('controllers/Desenvolvedor/Desenvolvedor');
   }
}
//Esse é o controlador principal.

print

  • Like this, I have 1 main controller, and through it I want to "ask" another controller that is inside a folder, in this case , Controllers/Developer/Developer.php (developer the first is the folder and the second is the name of the controller that equals the folder);

  • I recommend that you can edit your question, and add this supplementary comment information to the body of the question, making the question better and clearer. And if possible even provide more details than you want to do, making it clearer to anyone who can help you.

  • From the code, I think it’s pretty bad to do it like this, because opening a Library inside the briefcase Controller, does not carry controller within another controller, who knows telling what you want to solve, ie, what is your problem?

  • The name of it is POG. It may work, but it’s bad practice (I think). When you get to the point of having to carry one controller inside the other it’s time to think more about using a library, or a helper. That is, if the controller will house a function that must be accessed by others controllers, then what you need is a library, or a helper. In the MVC, the function of controller is to respond to interim requests model and view, and not serve data.

3 answers

2

By doing a search, you can also call per library and extend your child class (e.g., start.php) to the parent class (e.g., developer.php), allowing function calls in the controller

//desenvolvedor.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Desenvolvedor extends CI_Controller{
    public function __construct(){
        parent::__construct()
    }
    public function meuMetodo($algo){
        echo $algo;
    }
}


//inicio.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Inicio extends Desenvolvedor{
    public funcion inicio(){
        $this->meuMetodo('faça algo');
    }
}
  • 1

    Thanks so much! Thanks for the help hehe

  • If you have an answer that really helped you, mark it as accepted. If you came to the solution on your own, put in the solution as an answer. So content is more organized and easier to find in the future by other people with similar problems.

1

I came to this topic with the same question. However, none of these alternatives worked in Code Igniter 3.1.6, it could be that I did something wrong. But I found a very simple solution that solved my problem.

redirect('/NomeController');

After an entry in the database I used to call my controller.

1

Good friend if I understand right, this is simple to do. I imagined the following situation: you have the controller Desenvolvedor.php which is in controllers/Desenvolvedor/Desenvolvedor.php ai you have another controller called Frontend.php which is in controllers/Frontend/Frontend.php ai you need to use controller Desenvolvedor.php within the controller Frontend.php so inside the Frontend.php controller you will load your Developer.php controller this way:

$this->load->library('controllers/Desenvolvedor/Desenvolvedor');
  • 1

    That was my question, but it’s not working, in case it happens here: " An Error Was Encountered Unable to load the requested class: developer "

  • I already checked things... I saw no error in syntax...

  • http://i.imgur.com/Gwbwxfs.png Here ta "the photo" of directories

  • The.php start file is inside the "controllers" folder so you will call $this->load->library('Developer/Developer');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.