Yii Framework Forum: External action class - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

External action class Rate Topic: -----

#1 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 02 December 2008 - 11:38 PM

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
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 03 December 2008 - 07:54 AM

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?)
0

#3 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 03 December 2008 - 10:45 AM

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

0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 04 December 2008 - 07:42 AM

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).
0

#5 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 04 December 2008 - 09:01 AM

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

0

#6 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 04 December 2008 - 09:59 AM

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.
0

#7 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 04 December 2008 - 12:55 PM

The PHP start tag was missing  :-[
Sorry for bothering you with this mistake.
Maybe I should stick to C/C++/C#  :'(

/Tommy
0

#8 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,879
  • Joined: 04-October 08
  • Location:DC, USA

Posted 04 December 2008 - 01:00 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users