Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 5.4 Built in Server, Fix file not found error

Tags:

php

auraphp

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 ?

like image 440
Hari K T Avatar asked Dec 09 '22 03:12

Hari K T


1 Answers

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.

like image 80
hakre Avatar answered Dec 22 '22 00:12

hakre