Call function in another controller in Yii

28,357

Solution 1

I've solved this problem. The autoloader doesn't load controllers.

It was in config/main.php:

'import' => array(
    'application.models.*',
    'application.components.*',
),

All work with this:

'import' => array(
    'application.models.*',
    'application.components.*',
    'application.controllers.*',
),

Solution 2

class ServiceController extends Controller
{
    public function actionIndex()
    {
        Yii::import('application.controllers.back.ConsolidateController'); // ConsolidateController is another controller in back controller folder
        echo ConsolidateController::test(); // test is action in ConsolidateController



class ServiceController extends Controller
{
    public function actionIndex()
    {   
    Yii::import('application.controllers.back.CservicesController');
    $obj =new CservicesController(); // preparing object
    echo $obj->test(); exit; // calling method of CservicesController

Solution 3

When you create a Yii project, each of your controllers extend the Controller class, and that class extends the built in Yii class CController.

This is nice because Controller is a class within your application (it can be found in the components folder).

If you want a method to be accessible by both of your controllers, put that method in the Controller class, and since they both extend it. They'll both have access. Just make sure to declare it either public or protected.

Share:
28,357

Related videos on Youtube

ZhukovRA
Author by

ZhukovRA

PHP/JS Developer

Updated on July 09, 2022

Comments

  • ZhukovRA
    ZhukovRA almost 2 years

    I've created 2 controllers in my Yii application: FirstController.php and SecondController.php in default controller path.

    FirstController.php:

    <?php
     class FirstController extends Controller {
      public static function returnFunc() { return 'OK'; }
    }
    

    SecondController.php:

    <?php
     class SecondController extends Controller {
      public function exampleFunc() {
         $var = First::returnFunc();
      }
    }
    

    When I try to execute exampleFunc() in SecondController, Yii throw the error:

    YiiBase::include(FirstController.php) [<a href='function.YiiBase-include'>function.YiiBase-include</a>]: failed to open stream: No such file or directory
    

    Calling FirstController::returnFunc() similarly don't work.

    I'm newbee in OOP and Yii framework. What's the problem?

  • pestaa
    pestaa over 13 years
    The idea of sharing static functions across controllers in a controller is evil. That's what components are for.
  • thaddeusmt
    thaddeusmt over 13 years
    Yes, I would use a component, or have both controllers extend a base class that has the function you want both of them to call. You could even attach this function with a Behavior: yiiframework.com/doc/guide/1.1/en/…
  • thaddeusmt
    thaddeusmt over 13 years
    Yes, I think this or a variation (creating another controller class with this function, and extending that one) is the way to go. Also, a behavior might do what you need, by attaching the function: Yes, I would use a component, or have both controllers extend a base class that has the function you want both of them to call. You could even attach this function with a Behavior: yiiframework.com/doc/guide/1.1/en/…
  • Sonal Khunt
    Sonal Khunt about 11 years
    can you please tell me which version of codeignitor you are using?
  • shorif2000
    shorif2000 almost 11 years
    i get the following error Missing argument 1 for CController::__construct(), called in

Related