4

I am using PHP 5.4 RC5, and starting the server via terminal

php -S localhost:8000

Currently using Aura.Router , and at the root I have index.php file with code

<?php
$map = require '/Aura.Router/scripts/instance.php';

$map->add('home', '/');

$map->add(null, '/{:controller}/{:action}/{:id}');

$map->add('read', '/blog/read/{:id}{:format}', [
    'params' => [
        'id' => '(\d+)',
        'format' => '(\.json|\.html)?',
    ],
    'values' => [
        'controller' => 'blog',
        'action' => 'read',
        'format' => '.html',
    ]
]);

$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

$route = $map->match($path, $_SERVER);
if (! $route) {
    // no route object was returned
    echo "No application route was found for that URI path.";
    exit;
}
echo " Controller : " . $route->values['controller'];
echo " Action : " . $route->values['action'];
echo " Format : " . $route->values['format'];

A request for http://localhost:8000/blog/read/1 works as expected.

But when a dot json or dot html like http://localhost:8000/blog/read/1.json , http://localhost:8000/blog/read/1.html request comes, the php throws

Not Found
The requested resource /blog/read/1.json was not found on this server.

As I am running the server with the built in php server, where can I fix not to throw the html and json file not found error ?

Or do I want to go and install apache and enable mod rewrite and stuffs ?

Sam Dark
  • 5,291
  • 1
  • 34
  • 52
Hari K T
  • 4,174
  • 3
  • 32
  • 51

3 Answers3

10

You are trying to make use of a router script for the PHP built-in webserver without specifying it:

php -S localhost:8000

Instead add your router script:

php -S localhost:8000 router.php

A router script should either handle the request if a request matches, or it should return FALSE in case it want's the standard routing to apply. Compare Built-in web server­Docs.

I have no clue if Aura.Router offers support for the built-in web server out-of-the-box or if it requires you to write an adapter for it. Like you would need to configure your webserver for that router library, you need to configure the built-in web server, too. That's the router.php script in the example above.

hakre
  • 193,403
  • 52
  • 435
  • 836
3

you can specify the document root by passing the -t option like so:

php -S localhost:8888 -t c:\xampp\htdocs
0

Sorry, I'm not familiar with Aura.Router. However, I would use whatever is going on the production server. You might find some unexpected errors when you go live with the project if you do not sync the same program versions on your test and production servers.

Yipyo Ai
  • 91
  • 1
  • 2
  • 6