Mojolicious::Plugin::RelativeUrlFor - relative links in Mojolicious, really.
# Mojolicious $self->plugin('RelativeUrlFor'); # Mojolicious::Lite plugin 'RelativeUrlFor';
This Mojolicious plugin adds a new helper to your web app: relative_url_for, together with its short alias rel_url_for. Mojo's URL objects already had a method for this before 4.90, but to get really relative URLs like ../foo.html you had to add the request url like this:
relative_url_for
rel_url_for
my $url = $self->url_to('foo', bar => 'baz'); my $rel_url = $url->to_rel($self->req->url);
The new helper method gets the job done for you:
my $rel_url = $self->rel_url_for('foo', bar => 'baz');
Generated URLs are always relative to the request url.
Since this is a helper method, it's available in templates after using this plugin:
<%= rel_url_for 'foo', bar => 'baz' %>
url_for
To use relative URLs in your whole web app without rewriting the code, this plugin can replace Mojolicious' url_for helper for you, which is used by useful things like link_to and form_for. You need to set the replace_url_for option for this:
link_to
form_for
replace_url_for
# Mojolicious $self->plugin(RelativeUrlFor => { replace_url_for => 1 }); # Mojolicious::Lite plugin RelativeUrlFor => { replace_url_for => 1 };
This plugin lives in github: http://github.com/memowe/mojolicious-plugin-relativeurlfor. You're welcome to use github's issue tracker to report bugs or discuss the code: http://github.com/memowe/mojolicious-plugin-relativeurlfor/issues
Copyright Mirko Westermeier <mirko@westermeier.de>
This software is released under the MIT license. See MIT-LICENSE for details.
To install Mojolicious::Plugin::RelativeUrlFor, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::RelativeUrlFor
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::RelativeUrlFor
For more information on module installation, please visit the detailed CPAN module installation guide.