Gianni Ceccarelli


CatalystX::RouteMaster - role for components providing Catalyst actions


version 1.08


  package MyApp::Base::Stuff;
  use Moose;
  extends 'Catalyst::Component';
  with 'CatalystX::RouteMaster';

  sub _kind_name {'Stuff'}
  sub _wrap_code {
    my ($self,$c,$url_prefix,$action_name,$route) = @_;
    my $code = $route->{code};
    my $extra_config = $route->{extra_config};
    return sub {
      my ($controller,$ctx) = @_;


  package MyApp::Stuff::One;
  use Moose;
  extends 'MyApp::Base::Stuff';

  sub routes {
    return {
      '/some/url' => {
        action_name => {
          code => \&my_action_method,
          extra_config => $whatever,

  sub my_action_method {
    my ($self,$request) = @_;

    # do something

Also, remember to tell Catalyst to load your Stuff components:

   search_extra [ ::Stuff ]


First of all, this role does not give you anything that you could not do with Catalyst directly. It does not add any functionality. What it does is allow you to write some simple controllers in a different style.

This was originally part of CatalystX::ConsumesJMS, then I realised it could easily be generalised, so here is the "general" part.


So, since this module is quite complicated, and does not actually provide anything that Catalyst doesn't already do, why does it exist? When I wrote CatalystX::ConsumesJMS and Plack::Handler::Stomp, I was (ab)using Catalyst as a combination dependency injection container + dispatcher, to write an application to consume ActiveMQ messages. One of the design goals was to hide Catalyst from the actual application code, since in the end there was no need to expose the framework. If you don't need to hide Catalyst from your code, or you need non-trivial dispatching, you're probably better off without this module.


This role is to be used to define base classes for components in your Catalyst applications. It's not to be consumed directly by application components. Classes inheriting from those base classes will, when loaded, create controllers and register their actions inside them.


Subclasses of your component base specify which URLs they are interested in, by writing a routes method, see the synopsis for an example.

They can specify as many URLs and action names as they want / need, and they can re-use the code values as many times as needed.

The main limitation is that you can't have two components using the exact same URL / action name pair (even if they derive from different base classes!). If you do, the results are undefined. This is the same "limitation" as not having two actions for the same request in regular Catalyst.

It is possible to alter the URL via configuration, like:

    logical_url  /the/actual/url

You can also do this:

    logical_url  /the/actual/url
    logical_url  /another/url

to get your class to respond to two different URLs without altering the code.

You can even alter the action name via the configuration:

    <logical_url /the/actual/url>
      action_name local_action
    <logical_url /another/url>
      action_name local_action1
      action_name local_action2

That would (with the default "_action_extra_params" method) install 3 identical actions for the following URLs:


(Again, nothing that you couldn't do with normally-written controllers, it's just a different way to do it).

The "code"

The hashref specified by each URL / action name pair will be passed to the "_wrap_code" function (that the consuming class has to provide), and the coderef returned will be installed as the action to invoke for that name under that URL.

Required methods


As in the synopsis, this should return a string that, in the names of the classes deriving from the consuming class, separates the "application name" from the "component name".

These names are mostly used to access the configuration.


This method is called with:

  • the Catalyst application as passed to register_actions

  • the URL (mapped via "routing")

  • the action name (mapped via "routing")

  • the value from the routes corresponding to the URL and action name slot (see "Routing" above)

You can do whatever you need in this method, see the synopsis for an idea.

The coderef returned will be invoked as a Catalyst action for each matching request, which means it will get:

  • the controller instance (you should rarely need this)

  • the Catalyst application context

Implementation Details


This role should be consumed by sub-classes of Catalyst::Component.

The consuming class is supposed to be used as a base class for application components (see the "SYNOPSIS", and make sure to tell Catalyst to load them!).

Since these components won't be in the normal Model/View/Controller namespaces, we need to modify the COMPONENT method to pick up the correct configuration options. This is were "_kind_name" is used.

We hijack the expand_modules method to generate various bits of Catalyst code on the fly.

If the component has a configuration entry enabled with a false value, it is ignored, thus disabling it completely.

We generate a controller package for each (mapped) URL, by calling "_generate_controller_package", and we add an after method modifier to its register_actions method inherited from Catalyst::Controller, to create all the (mapped) actions. The modifier is generated by calling "_generate_register_action_modifier".


  my $pkg = $self->_generate_controller_package(

Generates a controller package, inheriting from whatever "_controller_base_classes" returns, called ${appname}::Controller::${url} (invalid characters are replaced with _). Any roles returned by "_controller_roles" are applied to the controller.

Inside the controller, we set the namespace config slot to the $url.


List (not arrayref!) of class names that the controllers generated by "_generate_controller_package" should inherit from. Defaults to 'Catalyst::Controller'.


List (not arrayref!) of role names that should be applied to the controllers created by "_generate_controller_package". Defaults to the empty list.


  my $modifier = $self->_generate_register_action_modifier(

Returns a coderef to be installed as an after method modifier to the register_actions method. The coderef will register each (mapped) action with the Catalyst dispatcher. You can pass additional parameters to the controller's create_action method by overriding "_action_extra_params".

Each action's code is obtained by calling "_wrap_code".


  my %extra_params = $self->_action_extra_params(

You can override this method to provide additional arguments for the create_action call inside "_generate_register_action_modifier". For example you could return:

  attributes => { MySpecialAttr => [ 'foo' ] }

to set that attribute for all generated actions. Defaults to:

  attributes => { 'Path' => ["$url/$action_name"] }

to make all the action "local" to the generated controller (i.e. they will be invoked for requests to $url/$action_name).


Gianni Ceccarelli <>


Thanks to Peter Sergeant (SARGIE) for the name.


This software is copyright (c) 2012 by

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.