Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 229
Ryan Dunphy Avatar asked May 12 '26 10:05

Ryan Dunphy


1 Answers

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

like image 118
Ryan Dunphy Avatar answered May 15 '26 01:05

Ryan Dunphy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!