Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
ServeCommand, shortcut for starting serving application to the incoming HTTP requests with PHP 5.4 built-in web server
* ServeCommand class file.
* @author resurtm <>
* @link
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license
* @since 1.1.11
* @property string $help The help information for the serve command.
* @author resurtm <>
* @package system.cli.commands
* @since 1.1.11
class ServeCommand extends CConsoleCommand
* @return string the help information for the serve command
public function getHelp()
return <<<EOD
yiic serve [address[:port [webroot]]]
This command starts serving the Yii web application for incoming
HTTP requests using PHP built-in server.
PHP built-in server command: $ php -S localhost:8000 -t /home/user/dir
This command: $ protected/yiic serve
Note that you should have PHP version 5.4 or newer to use this command.
* address: optional, listening address. Default value is 'localhost'.
* port: optional, listening port. Default value is '8000'.
* webroot: optional, document root path. Default is a current directory.
* Execute the action.
* @param array $args command line parameters specific for this command
* @throws CException
public function run($args)
// minimal PHP version is 5.4.0
throw new CException('The minimal PHP version for ServeCommand is 5.4.');
// extract arguments
$address=isset($args[0]) ? $args[0] : 'localhost:8000';
$webroot=isset($args[1]) ? $args[1] : getcwd();
// append default port if it was not specified
// additional information in CLI
echo "Serving at: {$address}\n";
echo "Web root is: {$webroot}\n";
// now start built-in PHP web server
exec(PHP_BINARY." -S {$address} -t {$webroot}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment