dingo / api

A RESTful API package for the Laravel and Lumen frameworks.
BSD 3-Clause "New" or "Revised" License
9.33k stars 1.25k forks source link

Getting value in url wildcard using lumen #1621

Closed lloricode closed 4 years ago

lloricode commented 5 years ago
Q A
Bug? yes
New Feature? no
Framework Lumen
Framework version 5.8.1
Package version 2.1.0
PHP version 7.3.2

Actual Behaviour

I have a route

$api->get('users/{id}', [
     'uses' => 'UserController@show',
]);

Inside controller

public function show(\Dingo\Api\Http\Request $request){
    $id = $request->id;
}

Then requesting

{{domain}}/users/1

Then the result give error

{
    "message": "Call to a member function parameter() on array",
    "status_code": 500,
    "debug": {
        "line": 531,
        "file": "/home/vagrant/code/laravels/api/lumen/vendor/illuminate/http/Request.php",
        "class": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
        "trace": [

This is work while dingo not install

Expected Behaviour

I expected to get the value from url named {id}

Steps to Reproduce

make a route like this

$api->get('users/{id}', [
     'uses' => 'UserController@show',
]);

then get it on constructor

public function show(\Dingo\Api\Http\Request $request){
    $id = $request->id;
}
lloricode commented 5 years ago

Solution is to create a class something like Dingo\Api\Http\LumenRequest then it extend on Laravel\Lumen\Http\Request to make compatible in lumen

lloricode commented 5 years ago

instead of Illuminate\Http\Request when using lumen, because Laravel\Lumen\Http\Request already extend in Illuminate\Http\Request

lloricode commented 5 years ago

@specialtactics

lloricode commented 5 years ago

hi, how can we totally adopt lumen request here? I see the code of dingo api, it seems it totally for extended in laravel?

lloricode commented 5 years ago

or need to separate this with lumen request

lloricode commented 5 years ago

I got this exception when running a test

{"message":"Argument 1 passed to Dingo\\Api\\Http\\RateLimit\\Handler::rateLimitRequest() must be an instance of Dingo\\Api\\Http\\Request, instance of Laravel\\Lumen\\Http\\Request given, called in \/home\/vagrant\/code\/laravels\/api\/kimberly-api-pwa\/vendor\/dingo\/api\/src\/Http\/Middleware\/RateLimit.php on line 64","status_code":500}"
    /**
     * @test
     */
    public function show404()
    {
        $this->get('blocks/xxx', $this->addHeaders())
            ->assertResponseStatus(404);
        $this->get('blocks/xxx/xxx', $this->addHeaders());
            dd($this->response->getContent());
//            ->assertResponseStatus(404);

        $machineName = Block::first()->machine_name;
        $this->get('blocks/xxx/' . $machineName, $this->addHeaders())
            ->assertResponseStatus(404);
    }

because lumen on testing is using Laravel\\Lumen\\Http\\Request

specialtactics commented 5 years ago

Sorry, I am not too sure as I do not use Lumen. I think you will find that Lumen is not used much nowdays (especially for new projects) - so the best advice I can give is to switch your project to Laravel - as I am aware of a few weird issues with Lumen and Dingo, and I myself don't have the Lumen experience to look at them.

lloricode commented 5 years ago

hmm, maybe minor adjustment because we much need lumen because we use it for pure api,

thanks @specialtactics i will find solution for this

specialtactics commented 5 years ago

@lloricode You will find you can use Laravel for pure API just as easily nowdays, and with deferred providers there is not much of any advantage to lumen like there was when it first came out.

At the same time the feature sets between Lumen and Laravel routing are becoming too great which causes other problems, and hence the issues between dingo and lumen.

lloricode commented 5 years ago

yes i see i will try recreate our existing lumen to a new laravel, and observe if we got same result, thanks for idea, maybe i should close this, because it clearly to me

thanks @specialtactics

lloricode commented 5 years ago

@specialtactics let me find solution, please give me more time ☺️, i will stay this open as a progress in my side

specialtactics commented 5 years ago

Sure it's no problem, I will assign the issue to you :smile:

pixeliner commented 4 years ago

@lloricode So what did you do? I'm not really in the mood to spend hours on a Lumen/dingo api project, to then come to the conclusion I have to rewrite it in Laravel ^^

lloricode commented 4 years ago

@pixeliner try this https://github.com/lloricode/lumen-dingo-boilerplate