Calling a Controller X function via a Controller Y (Codeigniter)

Asked

Viewed 1,102 times

0

Hello!

Well, I’ve got the following question on my head. Today I have a routine in CRON that runs a certain function of a Controller X of my application.

However, I would like the user to perform certain action (which is processed in another Controller, let’s call Y), it perform this routine manually.

There is a way to execute a Controller function from another Controller with CI? And if there is, does it hurt the MVC in which it is worked? Or is there a better way to treat this situation?

Really for the sake of code organization would need the two functions to be in separate controllers.

1 answer

4


First, let’s have doubts.

Is there a way to execute a Controller function from another Controller with CI? And if there is, does it hurt the MVC in which it is worked? Or is there a better way to treat this situation?

It exists. I believe that for this problem there are several solutions and that depending on the case, some become more viable than the others. However, I will mention here only the solutions that I know and that do not hurt the MVC standard.

Inheritance

One of the ways to solve the problem is through Inheritance.
This requires the creation of a Controller Core that will be inherited by the others Controllers of your application (X and Y in the case) and within it you can implement the logic that the CRON is running.

For example:

./application/core/My_controller.php

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

class MY_Controller extends CI_Controller {

    protected function _rotina()
    {
        // Lógica
    }
}

./application/controllers/Controller_x.php

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

class Controller_x extends MY_Controller {

    // Action executada pelo CRON
    public function action()
    {
        // Executa a rotina...
        $this->_rotina();
    }
}

./application/controllers/Controller_y.php

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

class Controller_y extends MY_Controller {

    // Action executada pelo outro Controller
    public function action()
    {
        // Executa a rotina...
        $this->_rotina();
    }
}

Helpers

Another way to solve the problem in Codeigniter is using Helpers.
To this end, a Helper customized in your application.

For example:

./application/helpers/rotina_helper.php

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

if ( ! function_exists('rotina'))
{
    function rotina()
    {
        // Lógica
    }
}

And within the actions of their Controllers X and Y just charge the Helper and use:

$this->load->helper('rotina');

// Executa a rotina
rotina();
  • 1

    Show! I had even thought about the issue of helpers, but as it is a somewhat complex routine it did not seem to me that fit well as a "helper", I believe that this idea of Controller Core will suit me, thanks!

  • 1

    Actually, the Helpers help to some extent. Here I use the issue of Heritage a lot because it is much more scalable.

Browser other questions tagged

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