user1908466
user1908466

Reputation: 553

call a controller function from another controller

I have 2 controllers, let's call them c1 and c2. Now I want to call a function in c2, Let's say actionC2, from a function in c1.

I tried something like this:
$c2_instance = new c2();
$c2_instance->actionC2();

but it won't work. I get this error: Missing argument 1 for CController::__construct().
What am I doing wrong?

EDIT: maybe its important to say that it falls on the first line

Upvotes: 0

Views: 335

Answers (2)

MD. Sahib Bin Mahboob
MD. Sahib Bin Mahboob

Reputation: 20534

You should not call a controller from another controller . You should redirect using this

$this->redirect(array('controller/action'));

And if you do not have exactly no way other that than , reconsider your design . Solve the problem , do not try to hide it. It will bite back you anyway.

Upvotes: 3

jdcookie
jdcookie

Reputation: 121

As @user488074 said, your controller must have an argument that it is looking for when you create an instance of it. Go to that controller and look at what it is looking for in the contstruct function. If you don't want to pass an argument all the time for this controller then add something like this to the construct function argument

public function foo($argument = NULL){
}

so it has a default value if you don't want to pass something.

Upvotes: 0

Related Questions