-1

How could I send data/variable from controller to other controller in codeigniter without using session?

I know how to send data from controller to other controller using session but I don't want to use it as it's giving me problem. If I use session, data I need to send will be used in many pages.

Example of the Data from controller 1 is $id (which I want to use on the other controller), but in the controller 2. If I open many pages, I only get the same data which is not what I expected. In controller 2, if I'm in page 1 I need $id = 1 while if I'm in page 2 I need the $id = 2. Any help with me appreciated. Thanks in advance.

2 Answers2

0

GET -- It's your choice ?

if yes then.. use URI Class

http://example.com/index.php/news/local/1

segment(1) = "news"

segment(2) = "local"

segment(3) = "1" <-- your $id

Controller

$id = $this->uri->segment(3);
  • Thanks for the answer, but I don't prefer segments as it's not safe for other modules. Sorry I forgot to mention in my question also. – Vandolph Reyes Feb 26 '16 at 10:25
  • create your libaries -- https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html – Visarut Sae-Pueng Feb 26 '16 at 10:27
  • @VisarutSae-pueng Codeigniter doc's for old and new now on the codeIgniter official website http://www.codeigniter.com/docs –  Feb 26 '16 at 11:29
0

test.php Controller File :

Class Test {
 function demo() {
  echo "Hello";
 }
}

test1.php Controller File :

Class Test1 {
 function demo2() {
  require('test.php');
  $test = new Test();
  $test->demo();
 }
}

But using require like this in codeigniter is not a good idea...

Praveen Kumar
  • 2,408
  • 1
  • 12
  • 20
  • Thanks for the answer. But I can't use redirect. The controller 1 has it's own view and it's should run/work every time. I just need to get the id of that certain controller and use it in the other controller. – Vandolph Reyes Feb 26 '16 at 10:36
  • do you want to use another controller's function ?? – Praveen Kumar Feb 26 '16 at 10:43
  • Yes, I'm using the controller 2 and I need the $id's from the controller 1. – Vandolph Reyes Feb 26 '16 at 10:49
  • Copy that function to some helper and use that helper.... Or save controller values to some constant and access it in second controller... – Praveen Kumar Feb 26 '16 at 10:50
  • Thanks. But how could I use the path or the php file of the require in codeigniter? I tried like this require_once(base_url() .'application/modules/sample/controllers/sample.php'); but it's not working. – Vandolph Reyes Feb 26 '16 at 11:24
  • you should not use require to load controllers with in controllers as it not the best way. You should use HMVC. –  Feb 26 '16 at 11:31
  • @wolfgang1983 exactly what I'm thinking also. Any solution with my problem? – Vandolph Reyes Feb 26 '16 at 11:32