Replace Underscores with Hyphens in Perl Catalyst URLs

We are considering options for converting CamelCase to camel-case and underline hyphens and hope that someone can offer some compelling examples. (Using hyphens for SEO reasons).

In particular:

Working with MMORPG in Catalyst and tired of having to write things like this:

 sub travel_to ( $self, $c, $star ) : Path('travel-to') Args(1) { ... } 

Or that:

 package Veure::Controller::Area::StorageLocker { .... sub index ( $self, $c ) : Path('/area/storage-locker') { ... } } 

Update: due to some confusion, I meant that we would prefer developers to write this:

 # getting rid of Args() would be nice, but sigs don't have introspection sub travel_to ( $self, $c, $star ) : Args(1) { ... } 

Or that:

 package Veure::Controller::Area::StorageLocker { .... sub index ( $self, $c ) { ... } } 

This is important because for an SEO perspective, underscores instead of hyphens can significantly improve your SEO. Doing too much grumbling work to always force hyphens, the developers forget to do this, and we continue to spend money returning and rewriting the code where this warning was forgotten. This is what we should do automatically.

+5
source share
2 answers

I searched a bit for Catalyst sources.

Cammel Enclosure Controller Names

You can change class2prefix in Catalyst :: Utils to change how controller names translate into a namespace.

Here is a very quick hack that demonstrates what happens with the new MyApp created with the .pl catalyst. I borrowed Borodin’s proposal to implement it.

 package MyApp::Controller::FooBar; use Moose; use namespace::autoclean; BEGIN { extends 'Catalyst::Controller'; } { require Class::Method::Modifiers; require String::CamelCase; Class::Method::Modifiers::around( 'Catalyst::Utils::class2prefix' => sub { my $orig = shift; # I borrowed most of this from the original function ... my $class = shift || ''; my $prefix = shift || 0; if ( $class =~ /^.+?::([MVC]|Model|View|Controller)::(.+)$/ ) { $prefix = $2; $prefix =~ s{::}{/}g; # ... and this from /questions/1251612/replace-underscores-with-hyphens-in-perl-catalyst-urls/3998681#3998681 $prefix = String::CamelCase::decamelize($prefix) =~ tr/_/-/r; } return $prefix; } ); } sub index :Path :Args(0) { my ( $self, $c ) = @_; $c->response->body('Matched MyApp::Controller::FooBar in FooBar.'); } 1; 

I tested this briefly, but cannot guarantee that it will not break anything. I believe that if he finds himself in a better place and is more appropriate, this could be a viable option.

Emphasizes actions

It looks harder. It's best to play around with Catalyst :: DispatchType :: Path in some way or create something that sets an ActionClass that modifies it. It basically replaces _ with - . This thing can be built around gather_default_action_roles in Catalyst :: Controller (possibly as a subclass) to add this to all actions. This is very speculative .

+3
source

CPAN has a String::CamelCase module that offers the decamelize function, after which you will need to convert underscores to hyphens using tr/_/-/

Hope this short example helps answer your question.

 use strict; use warnings 'all'; use v.14.1; use String::CamelCase 'decamelize'; for my $s ( 'travel_to', 'Veure::Controller::Area::StorageLocker' ) { (my $ss = $s) =~ s|^[\w:]*::Controller(?=::)||; $ss =~ s|::|/|g; $ss = decamelize($ss) =~ tr/_/-/r; say $ss; } 

Output

 travel-to /area/storage-locker 
+2
source

All Articles