resourceful routes for mojolicious
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


NOTE: use the latest Github version of mojolicious, as code is changing quickly!


The ResourcefulRoutes plugin is part of MojoX Scaffold. For more information
go to:

MojoX Scaffold Summary

In many cases, you have to create the same template and route structures and controller methods again and again.

For example, in order to manage a list of users, you need routes, controller methods and templates to
    - to list all users,
    - to display information on a single user and
    - to create, update and delete users.

MojoX Scaffold helps you to set up a controller, template files and routes with a few commands.

MojoX Scaffold in action

1.) Install MojoX Scaffold, using cpanminus, this is just


2.) Create a Mojolicious app:

    mojo generate app Test

3.) Switch to the newly created Test app:

    cd Test

4.) Set the correct namespace

Information on routes can now be found in the file in the /lib folder.
Now set the desired namespace for the controller files. As we want all of our
controllers to be located under

    /lib/Test/Controllers (namespace Test::Controllers)

we add


to the beginning of the startup method in the routes file.

Also remove the existing sample routes!

5.) Create a resource named "users"

Make sure that you are still in the Test folder (NOT the lib folder).

    perl script/test generate scaffold

Enter the required information step by step.

At the end, some manual corrections are required.

Add to your startup method (in case you used dbi):

    ### Plugins
    $self->plugin('model_instance', namespace => 'Test::DBI', method => 'dbh');

    ### Resources

Make sure that you put the correct config data in the database BASE file:

and also add a table to your database (there will be an sql command generated
for this which works fine on mysql, but might have to be modified on other db systems)

$self->resources('users') automatically generates the following routes
on each start of your app:

        # GET /users/new - form to create a user
          ->to(controller => 'users', action => 'new_form')

        # GET /users/123 - show user with id 123
          ->to(controller => 'users', action => 'show')->name('users_show');

        # GET /users/123/edit - form to update a user
          ->to(controller => 'users', action => 'edit_form')

        # GET /users - list of all users
          ->to(controller => 'users', action => 'index')

        # POST /users - create new user
          ->to(controller => 'users', action => 'create')

        # PUT /users/123 - update an existing user
          ->to(controller => 'users', action => 'update')

        # DELETE /users/123 - delete an existing user
          ->to(controller => 'users', action => 'delete')

7.) Start your app:

    perl script/test daemon --reload

    Also start your database (e.g. mysql).

8.) Enter the following URL to see the result:


9.) Note: as PUT and DELETE methods are not supported by old HTML standard, you
have to add a hidden parameter in your update and delete forms:
<input type="hidden" name="_method" value="put" />
<input type="hidden" name="_method" value="delete" />