dingo / api

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

URLGenerator signed route not working as expected #1589

Open harlekoy opened 6 years ago

harlekoy commented 6 years ago
Q A
Bug? yes
New Feature? yes
Framework Laravel
Framework version 5.6
Package version 2.0.0-alpha2
PHP version 7.2.6

Actual Behaviour

First i tried using the Laravel signed URL feature with QUEUE_DRIVER set to sync and its working fine, like this code below:

URL::temporarySignedRoute('route.name')

But when I change it my QUEUE_DRIVER to redis I got an issue saying Route 'route.name' is not defined.

so instead of using the normal URL facade of Laravel, I used something like this:

app('api.url')->version('v1')->temporarySignedRoute('route.name');

now the error is showing that the keyResolver is set to NULL. So I check how Laravel registered their Illuminate\Routing\UrlGenerator and found this inIlluminate\Routing\RoutingServiceProvider

Click here to see the code

$url->setKeyResolver(function () {
    return $this->app->make('config')->get('app.key');
});

Expected Behaviour

It should allow me to successfully make a signed URL

app('api.url')->version('v1')->signedRoute('route.name');
app('api.url')->version('v1')->temporarySignedRoute('route.name');

Steps to Reproduce

Just create a mail class like this having your URL link for the email and set your QUEUE_DRIVER to anything aside from sync in my end I used redis:

class SendEmail extends Mailable implements ShouldQueue
{
    ...

    public function url()
    {
        return app('api.url')->version('v1')->temporarySignedRoute('route.name');
    }

Possible Solutions

Just insert this code in the Dingo\Api\Provider\RoutingServiceProvider

in line 55:

$url->setKeyResolver(function () {
    return config('app.key');
});

and it should successfully create the signed URL you want

harlekoy commented 6 years ago

https://github.com/dingo/api/pull/1590

harlekoy commented 6 years ago

Temporary fix will be something like this

app('api.url')
    ->version('v1')
    ->setKeyResolver(->setKeyResolver(function () {
        return config('app.key');
     }))
    ->temporarySignedRoute('route.name',
        now()->addDay(), ['id' => 1]
    )
specialtactics commented 6 years ago

Looks good @harlekoy , @thilanga - might having a look?

jourdon commented 5 years ago

now ,it 's not work.

specialtactics commented 5 years ago

@jourdon Can you be more specific please? What version of this package are you using? What is the error you are getting?