0

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

4
  • You are missing the argument when calling CController, add it when calling CController($argument) or remove the need for the argument. Commented Nov 18, 2013 at 10:58
  • @user488074 Though this can be a solution for the short time , it will bite back you heavily later. Commented Nov 18, 2013 at 11:03
  • 1
    You should never call an controller "method" from another controller, controllers must only have "actions" if you want to share a method use a helper Commented Nov 18, 2013 at 11:04
  • Separate the code out into it's own CAction class, can be used by both then. Commented Nov 18, 2013 at 12:20

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

@MD.Sahib's answer is much better for the yii framework, however you may still need to look at what arguments your controller needs to instantiate itself and understand that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.