0

I have two controllers. Let's say A and B.

For clarity's sake I want to run some functions in B controller but I have trouble getting back to A.

Of course I could just run

$this->load->view('pages/examplepage');

But this example page has to get data from database and of course it gives me an error that it doesn't have the necessary data.

Now I could just run

$data['exampledata'] = $this->example_model->get_data();
$this->load->view('pages/examplepage', $data);

Now for couple of rows it's okay but isn't there a better way? What am I missing? Can't I just run the A controller's function from B controller and let the already existing function do the job?

korops
  • 31
  • 11
  • `$this->load->library(APPPATH.'controllers/B');` and `$this->B->method()` This was in CI2x though. – ArtisticPhoenix Mar 28 '18 at 08:21
  • @ArtisticPhoenix This just gives me `Unable to load the requested class: A`. Is it working for you? Maybe it's not working anymore for v3? – korops Mar 28 '18 at 08:30
  • If you have the need to call a method from another controller then maybe you should abstract that method in a helper ? – ctbs1 Mar 28 '18 at 08:31
  • see https://stackoverflow.com/questions/14165895/how-to-load-a-controller-from-another-controller-in-codeigniter/14166077 maybe a duplicate. – ArtisticPhoenix Mar 28 '18 at 08:34
  • @ctbs1 That might actually be the solution. Although it feels kind of stupid to not be able to run functions from other controllers as they for example open the views, so I would need to create a helper for every page I want to move to. Maybe I should move to other controller by using php function header()? – korops Mar 28 '18 at 08:40
  • Well I got it working by using `header('Location: ' . base_url());exit();`. Kinda feel like there should be another way but well if it works... – korops Mar 28 '18 at 08:53

1 Answers1

0

Hope this will help you :

user codeigniter url helper redirect()

Example :

Controller A

      class Abc extends CI_Controller {
         public function first()
         {
             redirect('xyz/some_method');
         }
      }

Controller B

      class Xyz extends CI_Controller {
         public function some_method()
         {
            redirect('abc/first');
         }
      }

for More : https://www.codeigniter.com/user_guide/helpers/url_helper.html

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • Thank you so much! Can't believe didn't find this simple solution. You made my day. – korops Mar 31 '18 at 13:09
  • Just noticed that redirect() works as it should but how come it redirects for example to "index.php/abc/first" and not just "abc/first" as it should be in routes file? – korops Mar 31 '18 at 14:17