Seem to have forgotten how to execute console commands

A while ago I made a couple of console commands extending from CConsoleCommand similar to what I had seen used in a CakePHP project - fixtures / fixturize - have a look in commands.zip

I had these commands working a while ago but seem to have forgotten how to execute them.

Have read through here again.

Below does not list the commands




cd [project-path]/protected

./yiic help



Below does not execute the command




cd [project-path]/protected

./yiic [command-name] [params]



Below produces an error - index.php does not exist or is not an entry script file.




cd [project-path]/protected

./yiic shell



Any suggestions?

Hi,

did you try to run yiic shell explicitly stating the config file route?

In my case would be:


./yiic shell /opt/lampp/htdocs/MySite/protected/config/main.php

Thank you, that did the trick.

Is it normal to have to specify the config file?

To be honest, I don’t know if it’s normal. I just had the same problem, read about it in the forum and fixed it (I can’t remember the thread, that’s why I didn’t reference it).

Perhaps there is a way to configure yiic shell with the bootstrap, I’m affraid I don’t know. Perhaps a more senior member can tell us.

OK thanks.

gazbond,

I’ve been looking into this a little bit.

As it says on the help when you call yiic shell without stating a config file, it looks for a file called index.php. Since it’s not there yiic shell throws an error. So to make it work you need to state a config file.

Now that I think about it, having to state the config file seems reasonable at least in this case: imagine you have more than one application using the same base framework. Is more than likely that you’d like to run a command in the context of one of these applications. So there you go, you set this context when calling yiic shell.

If you were to have only one application using the base framework and can’t be bothered to write every time the config file path, then you can modify your framework/cli/commands/ShellCommand.php file and hard code the path.

Simply change this line


$args[0]='index.php';

for this one (in my case would be):


$args[0]='/opt/lampp/htdocs/MySite/protected/config/main.php';

gazbond, a word of advice. I’m pretty new to programming so most probably there will be a better way and safer practice. Let’s see if someone who knows better can tell us about it.

Sometimes you have to use the config file with your customized application. For a newly generated application it should work if you run the command from the application base directory




protected/yiic shell



/Tommy

Hi there, I have a related issue trying to excecute a custom command. I think that I’m calling the commandMap’s class property in a wrong way. But a can’t figure this out.

With a fresh install of yii framework (latest release), I have made the following:

Put in /protected/commands/mycmd.php

Code of mycmd.php:




class MycmdCommand extends CConsoleCommand

{

	public function run($args)

	{

		return "Im run() of Mycmd";

	}

}



Code of /config/console.php:




return array(

	'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',

	'name'=>'My Console Application',

	// application components

	'components'=>array(

		'db'=>array(

			'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',

		),

		// uncomment the following to use a MySQL database

		/*

		'db'=>array(

			'connectionString' => 'mysql:host=localhost;dbname=testdrive',

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => '',

			'charset' => 'utf8',

		),

		*/

	),


	'commandMap'=>array(

		'mycmd'=>array(

			'class'=>'Mycmd'

		),

	),

);



And executed like so:




./yiic mycmd



The error:




PHP Error[2]: include(Mycmd.php): failed to open stream: No such file or directory

    in file /Developer/projects/yiibase/framework/YiiBase.php at line 396

#0 /Developer/projects/yiibase/framework/YiiBase.php(396): autoload()

#1 /Developer/projects/yiibase/framework/YiiBase.php(291): autoload()

#2 /Developer/projects/yiibase/framework/YiiBase.php(191): import()

#3 /Developer/projects/yiibase/framework/console/CConsoleCommandRunner.php(133): createComponent()

#4 /Developer/projects/yiibase/framework/console/CConsoleCommandRunner.php(60): CConsoleCommandRunner->createCommand()

#5 /Developer/projects/yiibase/framework/console/CConsoleApplication.php(88): CConsoleCommandRunner->run()

#6 /Developer/projects/yiibase/framework/base/CApplication.php(155): CConsoleApplication->processRequest()

#7 /Developer/projects/yiibase/framework/yiic.php(33): CConsoleApplication->run()

#8 /Developer/projects/yiibase/site/protected/yiic.php(7): require_once()

#9 /Developer/projects/yiibase/site/protected/yiic(4): require_once()



cactork,

on your console.php file, did you tried to write the path to your mycmd.php file? as in


'class'=> 'full/path/to/mycmd.php'

In any case cactork,

why don’t you just put your mycmd.php under protected/commands/shell of your application and execute it like




./yiic shell

mycmd



Thanks for your response caramelon,

It seems that putting full path, yii treats it like an Alias, and throws an exception.

I tried to enter to the shell with "./yiic shell" but, I think that I missing something, it shows:




Error: index.php does not exist or is not an entry script file.


USAGE

  yiic shell [entry-script | config-file]


DESCRIPTION

....



So, I pointed to "./yiic shell config/console.php"

but it says "Property "CWebApplication.commandMap" is not defined." wrapped by a bunch of HTML code.

In console.php, I configured like this:




return array(

...

	'commandMap' => array(

		'mycmd' => array(

			'class'=>'/full/path/mycmd.php',

		),

	),

...

);



:blink:

Could you just forget about that commandMap ?

Put your CConsoleCommand class in app/protected/commands, and that’s it. :)

I think I get this…

I setted an alias in console.php for the path of the /protected/ folder:




Yii::setPathOfAlias('frontend', 'full/path/to/protected');



And in the commandMap’s class property I set this:




...

	'commandMap'=>array(

		'mycmd'=>array(

			'class'=>'frontend.commands.mycmd',

		),

	),

...



Calling it like so:




> ./yiic mycmd



It throws a bunch of errors, but they are from command’s develop that I need to fix up :D

Thanks for the help!