Skip to content

Instantly share code, notes, and snippets.

@madalinignisca
Created October 22, 2015 08:09
Show Gist options
  • Save madalinignisca/2a416c3812d481ca1aea to your computer and use it in GitHub Desktop.
Save madalinignisca/2a416c3812d481ca1aea to your computer and use it in GitHub Desktop.
Yii serve using PHP's built in server

Found this on Yii wiki: Yii 2.0: PHP built-in server integration

Really cool and my default when working with Yii.

Place the ServeController.php file in "commands/" folder.

From the root of your project run "./yii serve" on Linux/BSD/OSX or "yii.bat serve" on Windows.

Open your browser to "http://localhost:8080" and work.

Supports the equivalent of Apache's mod_rewrite.

If you have problems with the network port 8080 (maybe Skype ;) ) or want to run it accessible in your network or on the internet (port forward in your router), the pass arguments to the "Yii CLI tool" like this: "./yii serve web 192.168.0.101 8888", where you would replace your IP and the port you want.

<?php
namespace app\commands;
use yii\console\Controller;
/**
* This command runs PHP built in web server
*
* @author Headshaker
*/
class ServeController extends Controller
{
/**
* This command echoes what you have entered as the message.
* @param string $root web root location relative to Yii app root.
* @param string $host hostname of the server.
* @param string $port port to listen for connections.
*/
public function actionIndex($root = "web", $host="localhost", $port= 8080)
{
$basePath = \Yii::$app->basePath;
$webRoot = $basePath.DIRECTORY_SEPARATOR.$root;
echo "Yii dev server started on http://{$host}:{$port}/\n";
echo "Document root is \"{$webRoot}\"\n";
passthru('"'.PHP_BINARY.'"'." -S {$host}:{$port} -t \"{$webRoot}\"")."\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment