1

Im using Dingo to build out an API and up until this point I've had no problems with the routes, until trying to add show into the controller, I'm just getting a 404.

Details here:

{
    "error": {
        "message": "404 Not Found",
        "status_code": 404,
        "debug": {
            "line": 179,
            "file": "/var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php",
            "class": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
            "trace": [
                "#0 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(546): Illuminate\\Routing\\RouteCollection->match(Object(Dingo\\Api\\Http\\Request))",
                "#1 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(525): Illuminate\\Routing\\Router->findRoute(Object(Dingo\\Api\\Http\\Request))",
                "#2 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(511): Illuminate\\Routing\\Router->dispatchToRoute(Object(Dingo\\Api\\Http\\Request))",
                "#3 /var/www/html/myapi/api/vendor/dingo/api/src/Routing/Adapter/Laravel.php(81): Illuminate\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))",
                "#4 /var/www/html/myapi/api/vendor/dingo/api/src/Routing/Router.php(513): Dingo\\Api\\Routing\\Adapter\\Laravel->dispatch(Object(Dingo\\Api\\Http\\Request), 'v1')",
                "#5 /var/www/html/myapi/api/vendor/dingo/api/src/Http/Middleware/Request.php(126): Dingo\\Api\\Routing\\Router->dispatch(Object(Dingo\\Api\\Http\\Request))",
                "#6 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(114): Dingo\\Api\\Http\\Middleware\\Request->Dingo\\Api\\Http\\Middleware\\{closure}(Object(Dingo\\Api\\Http\\Request))",
                "#7 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
                "#8 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Dingo\\Api\\Http\\Request), Object(Closure))",
                "#9 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Dingo\\Api\\Http\\Request))",
                "#10 /var/www/html/myapi/api/vendor/dingo/api/src/Http/Middleware/Request.php(127): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
                "#11 /var/www/html/myapi/api/vendor/dingo/api/src/Http/Middleware/Request.php(103): Dingo\\Api\\Http\\Middleware\\Request->sendRequestThroughRouter(Object(Dingo\\Api\\Http\\Request))",
                "#12 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Dingo\\Api\\Http\\Middleware\\Request->handle(Object(Illuminate\\Http\\Request), Object(Closure))",
                "#13 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))",
                "#14 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))",
                "#15 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))",
                "#16 /var/www/html/myapi/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))",
                "#17 /var/www/html/myapi/api/public/index.php(54): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))",
                "#18 {main}"
            ]
        }
    }
}

Here is a portion of my route file api.php

$api->group(['middleware' => 'jwt.auth'], function(Router $api) {
    $api->get('protected', function() {
        return response()->json([
            'message' => 'Access to protected resources granted! You are seeing this text as you provided the token correctly.'
        ]);
    });

    $api->get('refresh', [
        'middleware' => 'jwt.refresh',
        function() {
            return response()->json([
                'message' => 'By accessing this endpoint, you can refresh your access token at each request. Check out this response headers!'
            ]);
        }
    ]);

    $api->get('leads', 'App\\Api\\V1\\Controllers\\LeadController@index');
    $api->get('leads/{$id}', 'App\\Api\\V1\\Controllers\\LeadController@show');
    $api->post('leads/store', 'App\\Api\\V1\\Controllers\\LeadController@store');
    $api->put('leads/update/{$id}', 'App\\Api\\V1\\Controllers\\LeadController@update');
    $api->post('leads/update/{$id}', 'App\\Api\\V1\\Controllers\\LeadController@update');
});

And the show function in the controller: LeadController.php

public function show(Lead $leads, $id)
    {
        dd($id);

        $lead = Lead::with('user', 'source', 'industry', 'status')->find($id);

        //if(!$lead)
        //    return $this->response->error('invalid_data', 400);

        //return fractal($lead, new LeadTransformer())->respond();
    }

I've attempted to do a Die'n'Dump to ensure the ID is coming through, but it doesn't appear to be getting that far. Both the @index and @store work no problems and if I change LeadController@index to LeadController@show, the route works, and I of course get the error regarding the second parameter.

Really at a loss as to why this won't work, so help here would be really appreciated.

Seán McCabe
  • 893
  • 4
  • 23
  • 47

1 Answers1

0

Wow, from looking too hard, and not enough sleep, the reason it didn't work was because the variable was set as {$id} not {id} as it should have been.

When using these variables, don't use the $ sign!

Seán McCabe
  • 893
  • 4
  • 23
  • 47