Lumen-framework: delete, put, and patch routes do not work in Lumen 5.1

Created on 25 Jan 2016  路  2Comments  路  Source: laravel/lumen-framework

Background

I am creating an api server using Lumen 5.1. (I cannot yet switch to 5.2 because I have not yet found any session()->flash equivalent.) I am using the standard get, post, patch, and delete verbs for my requests. I am testing with postman. This stuff all works in my local Laravel 4.2 installations, so the issue is not any stray Apache setting.

Issue

According to the routing documentation, Lumen 5.1 should be able to handle delete and put requests. However, I encounter MethodNotAllowedHttpException in Application.php line 1259 with every request:

  1. in Application.php line 1259
    at Application->handleDispatcherResponse(array('2', array('GET', 'POST'))) in Application.php line 1212
  2. at Application->Laravel\Lumen{closure}() in Application.php line 1442
  3. at Application->sendThroughPipeline(array(), object(Closure)) in Application.php line 1213
  4. at Application->dispatch(null) in Application.php line 1153
  5. at Application->run() in index.php line 28

Further Details

This seems very strange considering that there is code for PUT requests on line 970, for PATCH requests on line 984, and for DELETE requests on line 998. Example, for DELETE requests:

public function delete($uri, $action)
{
    $this->addRoute('DELETE', $uri, $action);

    return $this;
}

The code for addRoute is in lines 1026 through 1045.

public function addRoute($method, $uri, $action)
{
    $action = $this->parseAction($action);

    if (isset($this->groupAttributes)) {
        if (isset($this->groupAttributes['prefix'])) {
            $uri = trim($this->groupAttributes['prefix'], '/').'/'.trim($uri, '/');
        }

        $action = $this->mergeGroupAttributes($action);
    }

    $uri = '/'.trim($uri, '/');

    if (isset($action['as'])) {
        $this->namedRoutes[$action['as']] = $uri;
    }

    $this->routes[$method.$uri] = ['method' => $method, 'uri' => $uri, 'action' => $action];
}

Based on this code, the $this->routes array should be properly set, but it appears that is not the case. The code that is generating the exception is in lines 1204 through 1218:

try {
    return $this->sendThroughPipeline($this->middleware, function () use ($method, $pathInfo) {
        if (isset($this->routes[$method.$pathInfo])) {
            return $this->handleFoundRoute([true, $this->routes[$method.$pathInfo]['action'], []]);
        }

        return $this->handleDispatcherResponse(
            $this->createDispatcher()->dispatch($method, $pathInfo)
        );
    });
} catch (Exception $e) {
    return $this->sendExceptionToHandler($e);
} catch (Throwable $e) {
    return $this->sendExceptionToHandler($e);
}

Notice that the isset($this->routes[$method.$pathinfo]) conditional is being skipped, though it should have been set by the addRoute method.

True source of the weirdness: everything is GET

I added die($method.var_dump($this->getPathInfo())); at the beginning of the addRoute method from line 1026. The result:

string(19) "/api/v0/discussions"
GET

The Application::get($uri, $action) is getting called instead of the appropriate method. Why this would happen, I have no idea.

Other Notes

I removed the vendor/lumen-framework/src/Http/Middleware/VerifyCsrfToken.php file while testing this. I can guarantee that is not causing the issue. And for good measure, I tried the form method spoofing to no avail.

Also, adding die($this->getMethod()); in addRoute results in outputting the correct http verb. This might be the route parser.

Finale

Your mission, should you choose to accept it, is to figure out why the application is not behaving the way it should and introduce a patch to fix this bug. (I will attempt to do the same.)

Most helpful comment

After just over an hour of fiddling with vendor code, I figured this out. My postman url was incorrect. Extreme facepalm moment. I should probably sleep on occasion.

All 2 comments

After just over an hour of fiddling with vendor code, I figured this out. My postman url was incorrect. Extreme facepalm moment. I should probably sleep on occasion.

same here. forgot to add https:// in url. noted just in case anyone else visit here to see this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timrogers picture timrogers  路  3Comments

hamihaa picture hamihaa  路  3Comments

dunice picture dunice  路  3Comments

jampack picture jampack  路  3Comments

georgeboot picture georgeboot  路  4Comments