The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Plugin::RelativeUrlFor - relative links in Mojolicious, really.

SYNOPSIS

    # Mojolicious
    $self->plugin('RelativeUrlFor');

    # Mojolicious::Lite
    plugin 'RelativeUrlFor';

DESCRIPTION

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:

    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.

In templates

Since this is a helper method, it's available in templates after using this plugin:

    <%= rel_url_for 'foo', bar => 'baz' %>

Replacing 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:

    # Mojolicious
    $self->plugin(RelativeUrlFor => { replace_url_for => 1 });

    # Mojolicious::Lite
    plugin RelativeUrlFor => { replace_url_for => 1 };

REPOSITORY AND ISSUE TRACKING

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

AUTHOR AND LICENSE

Copyright Mirko Westermeier <mirko@westermeier.de>

This software is released under the MIT license. See MIT-LICENSE for details.