Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex route syntax fails in namespaces #40

Closed
abackstrom opened this issue May 9, 2012 · 1 comment
Closed

Regex route syntax fails in namespaces #40

abackstrom opened this issue May 9, 2012 · 1 comment

Comments

@abackstrom
Copy link
Contributor

Regex style routes fail if respond() is called within a namespace. Sample code for testing purposes:

<?php

include __DIR__ . '/klein/klein.php';

echo '<pre>';

respond( '/', function( $request, $response, $app ) {
    echo 'Hello, World.';
});

with( '/entities', function() {
    respond( function(){ var_dump( null ); } );
    respond( '*', function(){ var_dump( '*' ); } );
    respond( '/?', function(){ var_dump( '/?' ); } );
    respond( '/people/[:name]', function(){ var_dump( '/people/[:name]' ); } );

    respond( '@foo', function(){ var_dump( '@foo' ); } );
    respond( '!@foo', function(){ var_dump( '!@foo' ); } );

    respond( '!@^/foo', function(){ var_dump( '!@^/foo' ); } );
    respond( '@^/foo', function(){ var_dump( '@^/foo' ); } );

    respond( '@foo$', function(){ var_dump( '@foo$' ); } );
    respond( '!@foo$', function(){ var_dump( '!@foo$' ); } );

    respond( '@^/foo$', function(){ var_dump( '@^/foo$' ); } );
    respond( '!@^/foo$', function(){ var_dump( '!@^/foo$' ); } );

    respond( '@^/.*\.(json|csv)$', function(){ var_dump( '@^/.*\.(json|csv)$' ); } );
    respond( '!@^/.*\.(json|csv)$', function(){ var_dump( '!@^/.*\.(json|csv)$' ); } );
});

dispatch();

In the current version of klein, most URIs (e.g. /entities/foo) will only match the catch-alls:

NULL
string(1) "*"

klein should be patched so that route regexes are appended to the current namespace.

@abackstrom
Copy link
Contributor Author

I've been using this extra code to debug route translations:

diff --git a/klein.php b/klein.php
index 4b8f042..ec943d1 100644
--- a/klein.php
+++ b/klein.php
@@ -19,6 +19,8 @@ function respond($method, $route = '*', $callback = null) {
         $method = null;
     }

+   $orig_route = $route;
+
     // empty route with namespace is a match-all
     if( $__namespace && ( null == $route || '*' == $route ) ) {
         $route = '@^' . $__namespace . '(/|$)';
@@ -26,6 +28,10 @@ function respond($method, $route = '*', $callback = null) {
         $route = $__namespace . $route;
     }

+   echo 'Original: '; var_dump( $orig_route );
+   echo 'Modified: '; var_dump( $route );
+   echo "----\n";
+
     $__routes[] = array($method, $route, $callback, $count_match);
     return $callback;
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant