Format seems not recognizing #7

Closed
harikt opened this Issue Jan 23, 2012 · 14 comments

Projects

None yet

2 participants

@harikt
Member
harikt commented Jan 23, 2012

The whole code I have is

<?php
$package_dir = dirname( __DIR__ ) . '/auraphp/system/package';
$loader = require_once $package_dir . '/Aura.Autoload/scripts/instance.php';
$loader->add('Aura\Router\\', $package_dir . '/Aura.Router/src/' );
$loader->register();
use Aura\Router\Map;
use Aura\Router\RouteFactory;
$map = new Map(new RouteFactory);

$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);

//echo $path . '<pre>' . print_r( $map->getRoutes() , true );

$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'];

Started server via php -S localhost:8000

http://localhost:8000/blog/read/1

Controller : blog Action : read Notice: Undefined index: format in /media/Linux/auracomponentstest/index.php on line 38 Format :
Yes the format is not there , so the error , but

http://localhost:8000/blog/read/1.html

http://localhost:8000/blog/read/1.json

all throwing

`Not Found

The requested resource /blog/read/1.html was not found on this server.`

@harikt
Member
harikt commented Jan 23, 2012

When we comment //$map->add(null, '/{:controller}/{:action}/{:id}'); , its showing the default format as html , but the same error when trying http://localhost:8000/blog/read/1.html or http://localhost:8000/blog/read/1.json

<?php
$package_dir = dirname( __DIR__ ) . '/auraphp/system/package';
$loader = require_once $package_dir . '/Aura.Autoload/scripts/instance.php';
$loader->add('Aura\Router\\', $package_dir . '/Aura.Router/src/' );
$loader->register();
use Aura\Router\Map;
use Aura\Router\RouteFactory;
$map = new Map(new RouteFactory);

$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);

//echo $path . '<pre>' . print_r( $map->getRoutes() , true );

$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'];
@harikt
Member
harikt commented Jan 23, 2012

I feel this is something with PHP configuration. When we echo some content and exit, the .html and .json formats is not printing the echo , so I feel its not calling the index.php itself, and some configuration in php is throwing the Not Found error .

@pmjones pmjones was assigned Jan 23, 2012
@harikt harikt referenced this issue Jan 23, 2012
Closed

In readme #6

@pmjones
Member
pmjones commented Jan 23, 2012

Do me a favor: write this up as a test case, so that we can track regressions. That means setting up $_SERVER (etc) in a way that matches your existing environment.

@harikt
Member
harikt commented Jan 24, 2012

Currently my phpunit has some issues when running. I have created a test case, can you test ? If I miss anything let me know .

public function testIsMatchOnDynamicPathWithStaticServerValues()
{
    $server = [
        'DOCUMENT_ROOT' => '/media/Linux/auracomponentstest',
        'REMOTE_ADDR' => '127.0.0.1',
        'REMOTE_PORT' => 49850,
        'SERVER_SOFTWARE' => 'PHP 5.4.0RC5-dev Development Server',
        'SERVER_PROTOCOL' => 'HTTP/1.1',
        'SERVER_NAME' => 'localhost',
        'SERVER_PORT' => 8000,
        'REQUEST_URI' => '/',
        'REQUEST_METHOD' => 'GET',
        'SCRIPT_NAME' => '/index.php',
        'SCRIPT_FILENAME' => '/media/Linux/auracomponentstest/index.php',
        'PHP_SELF' => '/index.php',
        'HTTP_HOST' => 'localhost:8000',
        'HTTP_CONNECTION' => 'keep-alive',
        'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Ubuntu/11.10 Chromium/14.0.835.202 Chrome/14.0.835.202 Safari/535.1',
        'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'HTTP_ACCEPT_ENCODING' => 'gzip,deflate,sdch',
        'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8',
        'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
        'REQUEST_TIME' => '1327369518.2441',
    ];

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

    $actual = $route->isMatch('/blog/read/42.html', $server);
    $this->assertTrue($actual);
    $expect = [
        'controller' => 'blog',
        'action' => 'read',
        'id' => 42,
        'format' => '.html'
    ];
    $this->assertEquals($expect, $route->values);
}
@pmjones
Member
pmjones commented Jan 24, 2012

I added the test to the suite; it passes. It sounds like your problem is one of configuration, but I don't know what it would be.

@pmjones pmjones added a commit that referenced this issue Jan 24, 2012
@pmjones pmjones add a test for issue #7 da94ceb
@harikt
Member
harikt commented Jan 24, 2012

:-( , wondering .

@harikt
Member
harikt commented Jan 24, 2012

This is what I see in terminal

127.0.0.1:54705 [404]: /blog/read/1.json

for the request http://localhost:8000/blog/read/1.json

@pmjones
Member
pmjones commented Jan 24, 2012

Sounds like you need the equivalent of rewrite rules for the PHP-based server.

@harikt
Member
harikt commented Jan 24, 2012

Do we need an htaccess file really ?

@pmjones
Member
pmjones commented Jan 24, 2012

There has to be something that intercepts the request for a particular file, and points it instead to an index.php file to handle it.

@harikt
Member
harikt commented Jan 24, 2012

@pmjones I am really wondering how http://localhost:8000/blog/read/1 its making a proper request to index.php without a htaccess file and why http://localhost:8000/blog/read/1.html , http://localhost:8000/blog/read/1.json has the trouble.

Can you give me how you compiled php and apache and whether the piece of code really works on your system and not test case ?

@pmjones
Member
pmjones commented Jan 24, 2012

I think the key piece you are missing is a .htaccess file, specifically mod_rewrite rules. Take a look at https://github.com/auraphp/system and note the web/.htaccess file there (it is commented with narrative).

@harikt
Member
harikt commented Jan 24, 2012

Yes, I have the piece of htaccess file , but my rewrite rule is not applying :P . phpinfo , not showing mod_rewrite :-( , compiled the apache with --enable-rewrite --enable-so , but seems not working . May be I want to reinstall the whole again :-)

@harikt
Member
harikt commented Jan 29, 2012

So finally compiled with mod_rewrite module and stuffs . Works perfectly .

Also I was having a question over stackoverflow , from there I came to know php -S localhost:8000 router.php will work .

Or point to index.php php -S localhost:8000 index.php

Anyone interested can look into http://stackoverflow.com/questions/9007513/php-5-4-built-in-server-fix-file-not-found-error

@harikt harikt closed this Jan 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment