Masonite - What is the difference between "name" and "prefix" when making route groups

55 Views Asked by At

I have this code snippet but I am not sure what the difference between prefix and name is in the group parameters?

group([
    #Get().route('/', 'WelcomeController@show').name('welcome'),
    get('/', 'PostController@show').name('form'),
    post('/store', 'PostController@store').name('create')
], prefix='blog.', name="blog."])
1

There are 1 best solutions below

0
On BEST ANSWER

The prefix is actually the prefix to the route itself and name just prepends the name to the names of all routes inside the group.

Take this for example:

group([
  get('', 'PostController@show').name('form'),
  get('/create', 'PostController@create').name('create'),
], prefix='/blog', name="blog."])

what this snippet does is creates two routes to match the URL's of /blog and /blog/create but also prepends the names to the routes such as blog.form and blog.create

The names of the routes can be used for redirection:

def show(self, request: Request):
    request.redirect_to('blog.create') #== /blog/create

or generating urls like this:

<a href="{{ route('blog.create') }}">Link</a>

which generates the same URL as above.