interchange / Dancer-Plugin-Interchange6

Dancer Plugin for Interchange6 shop machine
4 stars 1 forks source link

hook before_navigation_404 #51

Closed hexfusion closed 2 years ago

hexfusion commented 9 years ago

The new schema result class UriRedirect needs some code in DPIC6. My idea is to add a new hook before_navigation_404. This will allow us to release the hook/schema class and play with different ideas for the redirect code via the dancer app until we get exactly what we want for the plugin.

https://github.com/interchange/Dancer-Plugin-Interchange6/blob/people/hexfusion/404/lib/Dancer/Plugin/Interchange6/Routes.pm#L376

possible app use case.

hook 'before_navigation_404' => sub {
    my $tokens = shift;
    my $path = $tokens->{path};
    $path =~ s|^/||;

    debug "path ", $path;

    # check for uri redirect record
    my $redirect = shop_schema->resultset('UriRedirect')->find($path);

    if ($redirect) {
        # permanent redirect to specific URL
        debug "UriRedirect record found redirecting uri ", $redirect->uri_target,
            " for $path, with status code ", $redirect->status_code;

        Dancer::Continuation::Route::Forwarded->new(
            return_value => redirect(uri_for($redirect->uri_target), $redirect->status_code) )->throw;
    }
};