Mokholichesky url_for: an absolute way

I'm currently trying to port one of my small catalyst apps to Mojolicious (just for fun).

Let's say that we are on the page: http://whatever.com/generate . This page has a link to, for example, "generated"

When using a catalyst (with TT templates) and you define the link as

uri_for 'generated' 

it will create a link to http://whatever.com/generate/generated But if you define it as

 uri_for '/generated' 

he will create a link to http://whatever.com/generated

I tried to do the same with Mojolicious' url_for, but it seems to work differently. This does not mean that I can’t say that

 url_for 'generated' 

or

 url_for '/generated' 

both calls generate a link to '/ generate / generated'

So my question is: how to get url_for to create a link to an absolute route. i.e. to '/ generated'

thanks Gr, LDX

+8
perl catalyst mojolicious
source share
2 answers

url_for generates URLs relative to the root of the application. This was done for portability: you can place your application on any URL and your links will not be broken.

If you need a link to an absolute path, why do you need url_for what? You can use only the string '/generated' .

On the other hand, if you really need a Mojo :: URL object, you can get it with

 <%= url_for->path('/generated') %> 

url_for when called without parameters returns the current url

+10
source share

You can easily get the absolute url from url_for. It returns a Mojo :: URL object, so you can just use the to_abs method:

 $ perl -Mojo -E 'a("/" => sub { $s=shift;$s->render(text=>$s->url_for("/")->to_abs) })->start' get / http://localhost:13733/ 
+10
source share

Source: https://habr.com/ru/post/650871/


All Articles