Search code examples
pythonmasonite

Can a Masonite route accept a parameter anywhere other than the end of a URL?


The Masonite route documentation describes passing a parameter at the end of a URL like so:

# Handles /article/1234
Route.get('/article/@id', 'ArticleController@show')

What I want to do is create a slug after the article.id, and just use the article.id to return the appropriate article from the database. The Masonite documentation doesn't seem to explain how to do this, but I guessed it'd look something like the below (which doesn't work).

# Handles /article/1234/my-nice-slug
Route.get('/article/@id/*', 'ArticleController@show')

Does anybody know the correct way to do this?


Solution

  • I figured this out myself.

    You can grab the article.id by assigning the next part of the path another parameter and not using it. i.e:

    # Handles /article/1234/my-nice-slug
    Route.get('/article/@id/@my_slug', 'ArticleController@show')
    

    edit

    Joe Mancuso of Masonite also suggests using route compilers.

    Try '/article/@id:integer/@my_slug'

    which would match /article/123 but not /article/string_here