New PHP development server launch script

This commit is contained in:
Lonnie Ezell 2016-11-14 00:03:17 -06:00
parent db3ceef910
commit 867965afd8
No known key found for this signature in database
GPG Key ID: 88F86F2034554774
4 changed files with 98 additions and 0 deletions

29
rewrite.php Normal file
View File

@ -0,0 +1,29 @@
<?php
/**
* CodeIgniter PHP-Development Server Rewrite Rules
*
* This script works with serve.php 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.
*/
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$path = __DIR__.'public/'.$uri;
// If $path is an existing file or folder within the public folder
// then let the request handle it like normal.
if (is_file($path) || is_dir($path))
{
return false;
}
// Otherwise, we'll load the index file and let
// the framework handle the request from here.
// If we're serving the site locally, then we need
// to let the application know that we're in development mode
$_SERVER['CI_ENV'] = 'development';
require_once __DIR__.'/public/index.php';

36
serve.php Normal file
View File

@ -0,0 +1,36 @@
<?php
/**
* CodeIgniter PHP-Development Server Launcher
*
* This script launches the built-in PHP development server
* making sure that it knows the webroot is in the public folder,
* and using the rewrite.php file to mimic mod_rewrite functionality.
*
* The script is automatically set to the development environment
* within the rewrite.php file.
*/
$php = PHP_BINARY; // command to call PHP
/*
* Collect any user-supplied options and apply them
*/
$options = getopt(null, ['host:', 'port:']);
$host = $options['host'] ?? 'localhost';
$port = $options['port'] ?? '8080';
/*
* Get the party started
*/
require_once __DIR__.'/system/CLI/CLI.php';
\CodeIgniter\CLI\CLI::write("CodeIgniter development server started on http://{$host}:{$port}", 'green');
\CodeIgniter\CLI\CLI::write("Press Control-C to stop.");
/*
* 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 localhost:'.$port.' rewrite.php -t public/');

View File

@ -65,4 +65,5 @@ TODO
self
upgrading
troubleshooting
local_server

View File

@ -0,0 +1,32 @@
########################
Local Development Server
########################
PHP provides a built-in web server that is can be used locally when developing an application without
the need to setup a dedicated web server like MAMP, XAMPP, etc. If you have PHP installed on your
development machine, you can use the ``serve.php`` script to launch PHP's built-in server and have
it all setup to work with your CodeIgniter application. To launch the server type the following
from the command line in the main directory::
> php serve.php
This will launch the server and you can now view your application in your browser at http://localhost:8080.
.. note:: The built-in development server should only be used on local development machines. It should NEVER
be used on a production server.
Customization
=============
If you need to run the site on a different host than simply localhost, you'll first need to add the host
to your ``hosts`` file. The exact location of the file varies in each of the main operating systems, though
all *nix-type systems (include OS X) will typically keep the file at **/etc/hosts**.
Once that is done you can use the ``--host`` CLI option to specify a different host to run the application at::
> php serve.php --host=example.dev
By default, the server runs on port 8080 but you might have more than one site running, or already have
another application using that port. You can use the ``--port`` CLI option to specify a different one::
> php serve.php --port=8081