klein / klein.php

A fast & flexible router
MIT License
2.66k stars 290 forks source link

dynamic routes not being matched on nginx (klein v2.1.0) #326

Closed funkytaco closed 8 years ago

funkytaco commented 8 years ago

Hi all,

(klein v2.1.0)

I can't seem to use dynamic routes. For harcoded routes, Klein.php is responding as expected.

I decided to use the name joe (randomly) for this example:

#route is matched if I hardcode "joe"
- [{ method: GET }, { uri: "/api/1.0/tickets/joe" }, { class: [Main\Controllers\Api10TicketsController, getJoe] }]
#route is not matched
- [{ method: GET }, { uri: "/api/1.0/tickets/jo." }, { class: [Main\Controllers\Api10TicketsController, getJoe] }]
#route is not matched - this is the one I want to use
- [{ method: GET }, { uri: "/api/1.0/tickets/[*:id]" }, { class: [Main\Controllers\Api10TicketsController, getJoe] }]

I'm serving everything through index.php behind nginx:

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to index.php
    try_files $uri $uri/ /index.php?q=$uri&$args;
}

So how do I get it to match route of /api/1.0/tickets/123 where 123 = :id ? Thanks!

funkytaco commented 8 years ago

I found the problem.

            if (stristr($_SERVER['REQUEST_URI'],$uri)) {
                $router->respond($method, $uri, [$injector->make($class), $classMethod]);
            }

Since a regular expression would fail this check, the router wasn't matching the route.