Adds a router to the built-in serve command #12310
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When running
./craft serve
PHP utilizes it's built-in web server. The built-in web server automatically serves static files with the appropriate mime-type and headers based on the file extension. So, a request to a.php
file will route through PHP and return HTML. A request to a.css
file will serve the static file with a CSScontent-type
header. This works really well for actual files on disk.Unfortunately, if Craft is serving a virtual file (with an extension) through its own internal routing (via a module or otherwise) it will break. For example, insert the following in to a Module,
This should serve the template out of the
templates
directory but instead you get a PHP error thatfoo.json
does not exist. This is because PHP assumes the.json
extension is a static file and not something that PHP would normally serve.To work around this you can call the built in web server with a "router". Adding the router script allows the application code to make a determination if the request is static or not instead of assuming specific extensions are static.
This PR adds a
router.php
that checks the document root for a file before serving it. If the file does not exist in the filesystem it will fall back to Craft/PHP for serving the file (which mimics the recommended setup for Craft with popular web servers like Nginx).Note: when running PHP's built in web-server we can be assured that
$_SERVER['DOCUMENT_ROOT']
points where we want it too because the Yiiserve
console command does this for us.Lastly, it's important to have a separate
router.php
instead of usingindex.php
as the router. When you specify a router it is (essentially) prepended to all requests. A request to the homepage would (more or less) look likerequire 'router.php'; require 'index.php'
; If the index was your router you end up with a recursive call that times out or just breaks because it tries to prependindex.php
on to itself and you get errors with duplicate constants and the like.--
Long story short: this makes
php -S
behave more like "production" in that 404'd files route through Craft/PHP.