External action class

I'm trying to use an external action class with no luck.

Class file MyAction.php:



class MyAction extends CAction


{


  public function run()


  {


  ...


  }


}


SiteController.php:



  public function actions()


  {


    return array(


     'myaction'=>array('class'=>'application.controllers.site.MyAction'),


    );


  }


Source:



srv/www/htdocs/Yii/framework/YiiBase.php(270)





00258:      * Class autoload loader.


00259:      * This method is provided to be invoked within an __autoload() magic method.


00260:      * @param string class name


00261:      */


00262:     public static function autoload($className)


00263:     {


00264:         // use include_once so that the error PHP file may appear


00265:         if(isset(self::$_coreClasses[$className]))


00266:             include_once(YII_PATH.self::$_coreClasses[$className]);


00267:         else if(isset(self::$_classes[$className]))


00268:             include_once(self::$_classes[$className]);


00269:         else


00270:             include_once($className.'.php');


00271:     }


00272: 


Stack dump:



#0 /srv/www/htdocs/Yii/framework/YiiBase.php(270): autoload()


#1 unknown(0): autoload()


#2 /srv/www/htdocs/Yii/framework/collections/CConfiguration.php(138) : eval()'d code(1): spl_autoload_call()


#3 /srv/www/htdocs/Yii/framework/collections/CConfiguration.php(138): eval()


#4 /srv/www/htdocs/Yii/framework/web/CController.php(287): createObject()


#5 /srv/www/htdocs/Yii/framework/web/CController.php(171): SiteController->createAction()


#6 /srv/www/htdocs/Yii/framework/web/CWebApplication.php(146): SiteController->run()


#7 /srv/www/htdocs/Yii/framework/web/CWebApplication.php(118): CWebApplication->runController()


#8 /srv/www/htdocs/Yii/framework/core/CApplication.php(146): CWebApplication->processRequest()


#9 /srv/www/htdocs/ReturbudYii/index.php(15): CWebApplication->run()


If I place a copy of the file in the top directory (public) it seems like both files are found and dumped to the browser!? Same behavior with latest rev. (r318).

/Tommy

Could you please double check your action class? According to the call stack, it seems the class file doesn't contain the needed class (maybe the file name is not exactly the same as the class name?)

I had to add an explicit import element to config. Now I get this:



class MyAction extends CAction { public function run() { Yii::trace('MyAction'); } } Fatal error: Class 'MyAction' not found in /srv/www/htdocs/Yii/framework/collections/CConfiguration.php(138) : eval()'d code on line 1 


Doc suggestion (IIRC): explain how to import specific files (eg. no .php extension).

/Tommy

Are you running on Linux? And did you verify that the action file name is MyAction.php? Note that the name is case sensitive on Linux.

The error implies that the system can't find MyAction class (even you do an explicit import or include).

Yes, currently openSUSE 11 with PHP 5.2.6. Yii 1.0 framework.

The directories are all lowercase. MyAction.php is located in protected/controllers/site subdir. I've also tried all lowercase for filename and class name. It seems like the file is found (all content is present in the error message).

I'm not so familiar with PHP especially not ver 4/5, but it seems to me like something goes wrong in the eval() call. According to my PHP doc spl_autoload looks for a filename in lowercase. So I tried to have both MyAction.php and myaction.php present beside each other in the /site subdir, but still the same.



eval("$object=new $className($s);");


The actual values just before eval:

$className: "MyAction"

$s: "$args[1],$args[2]"

/Tommy

Would you mind sending me your action class file? I still think there is something wrong in the file.

This project site is using several external action classes like you did. They are working fine on both Windows and Linux.

The PHP start tag was missing  :-[

Sorry for bothering you with this mistake.

Maybe I should stick to C/C++/C#  :cry:

/Tommy

Glad you found the problem. I use C/C++ during worktime. It is powerful, but it also often traps you into minor details. Using script languages (not necessarily PHP) can free you from those things and concentrate on algorithms.