bartleyg Posted September 12, 2014 Share Posted September 12, 2014 Larry et al., After working through several web applications I have started to work on a console application. To get started I created a simple class in in the commands folder called SayHelloConsole.php. When I try to run the application ./yiic SayHello I receive the following error: $ ./yiic sayhelloPHP Fatal error: Cannot redeclare class SayHello in /var/www/myapp/protected/commands/SayHelloCommand.php on line 6 I have no other classes with the same name and I cannot track down where an additional import is taking place. I have tried removing: 'import'=>array( 'application.commands.*', ), from console.php, but then yiic fails because it cannot find the class file. I have googled and reviewed other reports of the problem, however I cannot find a solution to my problem. I have also tried the same command on two different Yii installations and still have the same issue. If I don't import 'application.commands' yiic cannot find the file and if I do, I get the 'cannot redeclare class' error. Any suggestions? Thanks, Bart My application is in the file SayHelloCommand.php <?phpclass SayHello extends CConsoleCommand{ public function run($args){ echo "Hello!\n"; }} My console configuration is console.php <?phpreturn array( 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'name'=>'Console', // preloading 'log' component // autoloading model and component classes 'import'=>array( 'application.commands.*', ), ); Link to comment Share on other sites More sharing options...
Larry Posted September 12, 2014 Share Posted September 12, 2014 Is there any more to the error message? Other than that, I hate to say that the problem is almost certainly that you do, in fact, have a SayHello class defined somewhere else in your code. You could change your class name to something really random to confirm. Link to comment Share on other sites More sharing options...
bartleyg Posted September 12, 2014 Author Share Posted September 12, 2014 Larry, I created a new file FurryWhiteBunnyCommand.php with the following class: <?phpclass FurryWhiteBunny extends CConsoleCommand{ public function bunnies(){ echo "I am a bunny\n"; }} and I get the following: $ ./yiic furrywhitebunny bunnies PHP Fatal error: Cannot redeclare class FurryWhiteBunny in /opt/local/www/myapp/protected/commands/FurryWhiteBunnyCommand.php on line 7 That is the only error message. No other bunny files/classes on my system. If I type ./yiic help it show the command: $ ./yiic helpYii command runner (based on Yii v1.1.14)Usage: ./yiic <command-name> [parameters...]The following commands are available: - furrywhitebunny - getxml - message - migrate - sayhello - shell - webappTo see individual command help, use the following: ./yiic help <command-name> The rest of my Yii application seems to work great. That is why I'm stumped on this. Is there another way to autoload other than 'import'=>array( 'application.commands.*', ), in the console.php file? Thanks, Bart Link to comment Share on other sites More sharing options...
bartleyg Posted September 12, 2014 Author Share Posted September 12, 2014 Also, The commands getxml and sayhello are both commands I created trying to figure out what was happening with the error. They have never worked either. Bart Link to comment Share on other sites More sharing options...
bartleyg Posted September 12, 2014 Author Share Posted September 12, 2014 Larry, Just to make sure I didn't have some other custom configuration issue causing the problem I created a new web application on another machine. I then created the commands/SayHelloCommand.php file identical to the one I used earlier and attempted to run the console command: $ ./yiic sayhelloPHP Error[2]: include(SayHelloCommand.php): failed to open stream: No such file or directory in file /opt/local/www/yii/framework/YiiBase.php at line 427#0 /opt/local/www/yii/framework/YiiBase.php(427): autoload()#1 unknown(0): autoload()#2 /opt/local/www/yii/framework/console/CConsoleCommandRunner.php(170): spl_autoload_call()#3 /opt/local/www/yii/framework/console/CConsoleCommandRunner.php(67): CConsoleCommandRunner->createCommand()#4 /opt/local/www/yii/framework/console/CConsoleApplication.php(92): CConsoleCommandRunner->run()#5 /opt/local/www/yii/framework/base/CApplication.php(180): CConsoleApplication->processRequest()#6 /opt/local/www/yii/framework/yiic.php(33): CConsoleApplication->run()#7 /opt/local/www/html/myapp/protected/yiic.php(7): require_once()#8 /opt/local/www/html/myapp/protected/yiic(4): require_once()I then changed config/console.php to add the import statement so autoloading could find the new command:<?php// This is the configuration for yiic console application.// Any writable CConsoleApplication properties can be configured here.return array( 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'name'=>'My Console Application', // preloading 'log' component 'preload'=>array('log'), 'import'=>array( 'application.commands.*', ),); I then re-ran the command and received the same results. $ ./yiic sayhelloPHP Fatal error: Cannot redeclare class SayHello in /opt/local/www/html/myapp/protected/commands/SayHelloCommand.php on line 6 I'm using Yii 1.14, but I also tried it with 1.13 and 1.12. Thanks, Bart Link to comment Share on other sites More sharing options...
Larry Posted September 22, 2014 Share Posted September 22, 2014 Very, very strange. Sounds like you've done your due diligence here. May be a bug in Yii, although not one I've seen before. Link to comment Share on other sites More sharing options...
host138 Posted July 17, 2020 Share Posted July 17, 2020 I get same error. The problem is in the class name "SayHello". In the example must be "SayHelloCommand" and file name SayHelloCommand.php It resolves the problem. Hope this helps somebody else. Link to comment Share on other sites More sharing options...
Recommended Posts