Ganesh Aher
Ganesh Aher

Reputation: 1128

How to pass data from one controller to other controller in codeigniter

I'm trying to code the system in that things are separated using "userId". That means, products or services are separated on the basis that user who add them in the system.

For that I'm trying to pass the userId from user controller to service and product controller. But I got an error that undefined variable userID.

$data['id'] = $this->session->userdata('user_id');

pass this in create() function of department controller. i'm loading user model in __construct() function also.

Thanks in advance.

Upvotes: 1

Views: 1889

Answers (1)

Sorav Garg
Sorav Garg

Reputation: 1067

You can do it using two ways.

1) By Helper - simple create a custom helper file and create a function inside helper file and inside function add get userid code and then using function name you can access this userid anywhere in whole project.

2) Using By Require Controller Class -- if you want to access user id in another controller so i will explain with you by example.

Ist Controller - Test.php

here i defined a variable as a public and this variable contains userid value.

2nd Controller - Test2.php

here we will require Test.php file

require('Test.php file path');

and then create object and then we can access Test.php file variables and methods inside Test2.php file.

But i will suggest you first way it`s very easy way and multiple advantages.

Hope it will work for you.

Upvotes: 1

Related Questions