Extending Yii’s Web Application Creation
I found out that after creating my Yii Applications through Yiic.bat, I tend to modify the same things in all of them (adding .htaccess files for pretty urls (no index.php), adding a reference for my custom modules directory, etc.), so I decided to try and override the WebApp command with one of my own. There might be better methods, but it’s also good practice.
My steps were as follows:
- Copy the following files from the framework directory to a side
directory (which I named
myWebApp):yiic,yiic.bat,yiic.php - In
yiic.php, modifyrequire_onceto reference the actual location ofyii.php - Create the following directories in the side directory:
cli/commandscli/views/webapp
- Copy the file
cli/commands/WebAppCommand.phpfrom the framework directory into yourcli/commandsdirectory. Be sure to rename it. I chosemyWebAppCommand.php - Open the copied file.
- Change the class name to match the file name. In my case -
MyWebAppCommand - Change it to extend
WebAppCommand, in order to have access to the original application generation code. -
Just before the class deceleration, manually import the original WebAppCommand file (autoload won’t find it) using the following statement:
:::php <? Yii::import('system.cli.commands.WebAppCommand'); ?> - Modify the class to your liking. Sadly, the “UI” and the file copy logic are located at the same method (
run), so it’s either copying the entire method to your clone, or missing out on the entire logic of the application. Assuming you “steal” the run method, you should do one of the following:- Also copy the template web site, found at
cli/views/webappto your side directory (at the same path!) - Modify the line that tells Yii where to find the source files:
Instead of$sourceDir=realpath(dirname(__FILE__).'/../views/webapp');
Use$sourceDir=realpath(Yii::getPathOfAlias('system').'/cli/views/webapp');
- Also copy the template web site, found at
- Further customize the file. The most common things to do:
- Copy additional files - Create your own webapp directory and copy it after copying the original one
- Add customization callback - copy one of the
generate\*methods and add appropriate callback in therunmethod.
Happy hacking!