Search code examples
perlcatalyst

Adding a extra field in url but avoiding that field in url handling of Catalyst framework


Suppose I have a url

http://www.somesite.com/path/to/catalyst/controller

And I want to redirect this to

http://www.somesite.com/extra-part/path/to/catalyst/controller

This extra-part should only be in url, And should not be considered for URL handling of Catalyst.

I found that, we can inherit/override prepare_path method for this but I couldn't use it properly.

I found this link for that : Catalyst Wiki

But still help needed.


Solution

  • Inside of lib/MyApp.pm I have a function prepare_path that looks like this:

    sub prepare_path {
      my $c = shift;
    
      $c->maybe::next::method( @_ ) ;
      my $base_uri_prefix = $c->config->{base_uri_prefix} ;
      my @path_chunks = split m[/], $c->request->path, -1;
    
      if (@path_chunks && $path_chunks[0] eq $base_uri_prefix ) {
        shift @path_chunks ;
    
        # Create modified request path from any remaining path chunks:
        my $path = join( '/' , @path_chunks ) || '/' ;
    
        # Stuff modified request path back into request:
        $c->request->path( $path ) ;
      } else {
        # Modify the path part of the URI to look as if it had a prefix:
        $c->request->uri->path( "$base_uri_prefix/" . $c->request->path ) ;
      }
    }
    

    And in myapp.conf is defined:

    base_uri_prefix   extra-part