Search code examples
perlmodel-view-controllermojolicious

Modify default Controller/Model paths in Mojolicious


I would like to be able to move Controllers, Models etc from their default Mojolicious paths:

  - App
    - Controller
      - Namespace1
        - ...
      - Namespace2
        - ...
    - Model
      - Namespace1
        - ...
      - Namespace2
        - ...

into something a little more manageable, such as:

  - App
    - Namespace1
      - Controller
        - ...
      - Model
        - ...
    - Namespace2
      - Controller
        - ...
      - Model
        - ...

So instead of

$r->any('/api/test')->to('Namespace1::Controller1#test');

I could call something like

$r->any('/api/test')->to('App::Namespace1::Controller1#test');

How does one accomplish this in Mojolicious?


Solution

  • It turns out that you can specify the namespace like this:

        $r->any('/api/test')->to(
            namespace => 'App::Namespace1',
            controller => 'Controller::Controller1',
            action => 'test'
        );
    

    Which would call the test method from the controller App::Namespace1::Controller::Controller1

    See Mojolicious Routing for details