diff --git a/serve b/serve deleted file mode 100644 index dc829edca18d..000000000000 --- a/serve +++ /dev/null @@ -1,36 +0,0 @@ - 'The PHP Binary [default: "PHP_BINARY"]', + '-host' => 'The HTTP Host [default: "localhost"]', + '-port' => 'The HTTP Host Port [default: "8080"]', + ]; + + public function run(array $params) + { + // Collect any user-supplied options and apply them + $php = CLI::getOption('php') ?? PHP_BINARY; + $host = CLI::getOption('host') ?? 'localhost'; + $port = CLI::getOption('port') ?? '8080'; + + // Get the party started + CLI::write("CodeIgniter development server started on http://{$host}:{$port}", 'green'); + CLI::write('Press Control-C to stop.'); + + // Set the Front Controller path as Document Root + $docroot = FCPATH; + + // Mimic Apache's mod_rewrite functionality with user settings + $rewrite = __DIR__ . '/rewrite.php'; + + // Call PHP's built-in webserver, making sure to set our + // base path to the public folder, and to use the rewrite file + // to ensure our environment is set and it simulates basic mod_rewrite. + passthru("{$php} -S {$host}:{$port} -t {$docroot} {$rewrite}"); + } +} diff --git a/rewrite.php b/system/Commands/Server/rewrite.php similarity index 65% rename from rewrite.php rename to system/Commands/Server/rewrite.php index 1ad69c1ca6c8..ca2a061f844c 100644 --- a/rewrite.php +++ b/system/Commands/Server/rewrite.php @@ -2,27 +2,37 @@ /** * CodeIgniter PHP-Development Server Rewrite Rules * - * This script works with serve.php to help run a seamless + * This script works with the CLI serve command to help run a seamless * development server based around PHP's built-in development * server. This file simply tries to mimic Apache's mod_rewrite * functionality so the site will operate as normal. */ +// Avoid this file run when listing commands +if (php_sapi_name() === 'cli') +{ + return; +} + // If we're serving the site locally, then we need // to let the application know that we're in development mode $_SERVER['CI_ENVIRONMENT'] = 'development'; $uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); -$path = __DIR__.'/public/'.ltrim($uri,'/'); +// Front Controller path - expected to be in the default folder +$fcpath = realpath(__DIR__ . '/../../../public') . DIRECTORY_SEPARATOR; + +// Full path +$path = $fcpath . ltrim($uri, '/'); // If $path is an existing file or folder within the public folder // then let the request handle it like normal. if ($uri !== '/' && (is_file($path) || is_dir($path))) { - return false; + return false; } // Otherwise, we'll load the index file and let // the framework handle the request from here. -require_once __DIR__.'/public/index.php'; +require_once $fcpath . 'index.php';