5 Replies - 322 Views - Last Post: 22 November 2017 - 09:37 PM Rate Topic: -----

#1 Splashsky   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 311
  • Joined: 25-August 13

Laravel Routing Contraints

Posted 21 November 2017 - 06:32 AM

Hey guys! Been working on a neat little project. I'm using Laravel 5.5, and I'm building routes to handle the various requests. I've got a route that accepts a slug to locate a particular guild via route model binding. Works great! Beautifully, actually. Then, I defined a "static" route that doesn't use a parameter to show the form for creating a new guild. Here's the routes...
Route::get('/guilds', '[email protected]')->name('guilds');
Route::get('/guild/{guild}', '[email protected]')->name('guild');
Route::get('/guild/create', '[email protected]')->name('create_guild');
Route::get('/guild/{guild}/edit', '[email protected]')->name('edit_guild');
Route::post('/guild/create', '[email protected]')->name('store_guild');


But when I attempt to navigate to '/guild/create', I get a 404 because a guild with the slug "create" doesn't exist. How can I work around this particular issue?

Is This A Good Question/Topic? 0
  • +

Replies To: Laravel Routing Contraints

#2 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2041
  • View blog
  • Posts: 6,263
  • Joined: 15-January 14

Re: Laravel Routing Contraints

Posted 21 November 2017 - 10:22 AM

Maybe if you listed the static route before the other one it would take precedence, but I'm not sure how Laravel handles that. If that's not possible then you need to come up with a URL scheme that is consistent, if "guild" is part of the URL then you're looking up a guild, not creating one. You can move the other operations under guilds or another route.
Was This Post Helpful? 1
  • +
  • -

#3 Splashsky   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 311
  • Joined: 25-August 13

Re: Laravel Routing Contraints

Posted 21 November 2017 - 11:37 AM

Yeah, I hadn't really thought of order and determining precedence. Once I'm home from work I intend on testing it. Another idea I had was to just group URIs by function instead, where creating guilds and items might fall under '/create/guild' and '/create/item' but since I'm trying to learn this to also make a tutorial for a game, I'm trying to use the best practices. :P

What routing scheme would you recommend?

This post has been edited by Splashsky: 21 November 2017 - 11:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2041
  • View blog
  • Posts: 6,263
  • Joined: 15-January 14

Re: Laravel Routing Contraints

Posted 21 November 2017 - 05:02 PM

I would just say to keep it simple, don't try to cram everything into one route. You don't have to pay by the route, if your guild route is going to be for showing guilds then use another route for creating, editing, etc. You could use guilds, for example, for operations over guilds in general instead of a specific one.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: Laravel Routing Contraints

Posted 21 November 2017 - 08:54 PM

You seem to be trying to use a restful design, so why not use resource?
Route::resource('/guilds', 'GuildController');

Was This Post Helpful? 1
  • +
  • -

#6 Splashsky   User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 311
  • Joined: 25-August 13

Re: Laravel Routing Contraints

Posted 22 November 2017 - 09:37 PM

Thanks, Arti! That actually worked beautifully!

View PostCTphpnwb, on 21 November 2017 - 08:54 PM, said:

You seem to be trying to use a restful design, so why not use resource?
Route::resource('/guilds', 'GuildController');

This... this is the stuff that God created and bestowed upon man. This snippet is the single greatest Laravel tool I've used so far.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1