routes/web.pyand are contained in a
ROUTESconstant. All routes consist of some form of HTTP route classes (like
Post()). At the bare minimum, a route will look like:
/. Routes that should only be executed on Post requests (like a form submission) will look very similar:
prefixparameter will prefix that URL to all routes in the group as well as the
nameparameter. The code above will create routes like
/dashboard/url1with the name of
post.create. As well as adding the domain and middleware to the routes.
Matchroute class. This can match several incoming request methods. This is useful for matching a route with both
config/middleware.pyfile but we can go more in detail in the middleware documentation. To add route middleware we can use the middleware method like so:
app/http/controllersbut sometimes you may wish to put your controllers in different modules deeper inside the controllers directory. For example, you may wish to put all your product controllers in
app/http/controllers/productsor all of your dashboard controllers in
app/http/controllers/users. In order to access these controllers in your routes we can simply specify the controller using our usual dot notation:
app/http/controllersdirectory but you may wish to completely change the directory for a certain route. We can use a forward slash in the beginning of the controller namespace:
idfor the use in retrieving a certain model. Specifying route parameters in Masonite is very easy and simply looks like:
Requestobject which can be found inside our controllers.
parammethod on the
Requestobject like so:
/dashboard/user/settingsto the same controller method. In this event you can use optional parameters which are simply replacing the
/dashboard/joseph. In order to do this we simply need to pass a type to our parameter. If we do not specify a type then our parameter will default to matching all alphanumeric and underscore characters.
/dashboard/128372. Currently only the integer and string types are supported.
False. We can add them on the Route class from the container using the
compilemethod. A completed example might look something like this:
compile()method on the
Routeclass and make sure we specify a regex string by preceding an
rto the beginning of the string.
example.com/dashboardto route to a different controller than
.domain()method on our routes like so:
joseph.example.com/dashboardbut not to
subdomainparameter to the Request class. We can retrieve the current subdomain like so: