Routing

With Inertia all routing is defined server-side.

Generating URLs

To avoid hard-coding URLs path into your application client-side it would be good if Masonite could send the routes information. There are different options you can use.

Generate server-side and send them as props

Here the url to create a user is generated in the controller and sent to client through the prop create_url.

from masonite.routes import Router

class UsersController(Controller):

    def index(self, view: InertiaResponse, router: Router):
        return view.render(
            "Users/Index",
            {
                "users": User.all().serialize(),
                "create_url": router.route("users.create"),
            },
        )

Generated as JSON and include in view

You can also generate all your routes (or a selection) as a JSON payload that you will include into your view.

To help you in the process you can use the small package masonite-js-routes. It provides a routes() helper that you can use in your view to include the routes client-side. After installing the package you just have to add this to your view.

It will actually generate this

Then you're free to use this global variable into your client-side application to get route path from the name.

Hopefully an existing package called ziggy-js can be installed to provide some javascript helpers that can parse this Routes variable.

Last updated

Was this helpful?