Api: Better Url Generator

Created on 25 Mar 2016  路  6Comments  路  Source: dingo/api

A better way to generate URLs would be nice.

app('Dingo\Api\Routing\UrlGenerator')->version('v1')->route('account.get', ['id' => $account->id])

is crazy long and not very enjoyable to look at. Even if I could just do something like:

$this->urlGenerator = app('Dingo\Api\Routing\UrlGenerator')

in the constructor (like I would with validator, or almost anything else in app() that is reused) would help a ton. However, that particular example segfaults (I have no idea, I couldn't find an actual error message in any of the logs after looking for a couple hours).

$this->urlGenerator = new \Dingo\Api\Routing\UrlGenerator(new \Illuminate\Http\Request)

almost worked, but it returned "Fatal error: Call to a member function getByName() on null". Anyway to reduce the amount of code I have to write for EVERY URL I want to generate would be fantastic! Thanks.

Most helpful comment

Main issue here is that the controller basically gets stuck an in infinite loop because the UrlGenerator attempts to load the routes which attempts to create a new route which instantiates the controller. Going to tweak a couple of things here.

There's a version helper function though.

version('v1')->route('account.get', [$account->id]);

All 6 comments

Main issue here is that the controller basically gets stuck an in infinite loop because the UrlGenerator attempts to load the routes which attempts to create a new route which instantiates the controller. Going to tweak a couple of things here.

There's a version helper function though.

version('v1')->route('account.get', [$account->id]);

@jasonlewis thanks for sharing! I was using api.url until now but your version makes it again a bit shorter!

app('api.url')->version('v1')->route('account.get', [$account->id]);

Awesome, that's quite a bit shorter. Thanks!

How can I force it to be HTTPS url? URL::forceSchema('https'); does not seem to work here...
Thanks.

@oleynikd and to anyone reading this.
Solution:
Put the app('Dingo\Api\Routing\UrlGenerator')->forceScheme('https'); function call to the end of the RouteService provider map() function (after the application mapped the routes).

Thanks @gazben

I have created a new wiki page with the goal of documenting little tibits of information like this;
https://github.com/dingo/api/wiki/Misc

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jdforsythe picture jdforsythe  路  3Comments

lloricode picture lloricode  路  3Comments

BartHuis picture BartHuis  路  3Comments

jhayiwg picture jhayiwg  路  3Comments

sukh-gill picture sukh-gill  路  3Comments