andyjh07's avatar

Reviewing my routes, do they make sense?

Hi everyone,

I'm still working on my first Laravel app - it's a reptile tracker app. Anyways I'm still trying to understand the best way of doing RESTful routes, below are my current routes:

// Weight routes...
Route::resource('weights', 'WeightController');
Route::get('weights/{gecko_name}/create', 'WeightController@create');

// Gecko routes...
Route::get('geckos', 'GeckoController@index');
Route::resource('gecko', 'GeckoController');
Route::get('gecko/{gecko_name}/view', 'GeckoController@show');
Route::get('gecko/{gecko_name}/edit', 'GeckoController@edit');

So to give you an idea, a couple of the routes could be:

app.dev/gecko/Zilly/view

app.dev/gecko/Zilly/update

app.dev/weights/Zilly/create

And I want to implement some image uploads, so I'm thinking the following routes:

app.dev/gecko/Zilly/images

app.dev/gecko/Zilly/images/upload

app.dev/gecko/Zilly/images/delete

Have I got my endpoints correct? Does the way I'm doing things make sense or could I make them better/more understandable?

I appreciate any help with this stuff,

Andy

0 likes
5 replies
Snapey's avatar

Go back and add three backticks ` before and after the code blocks.

One thing I notice is that if you use resource controllers, you don't need additional routes for create and edit.

andyjh07's avatar

@Snapey Sorry about the lack of formatting ;)

The reason the other routes are there is because I want to use the name variable in my controller

frezno's avatar

@andyjh07 , you can name them whatever you want.
it doesn't make sense to use resource and double various methods. If you feel unhappy with the "resource", just call every single method individually.There's nothing wrong with it.
or can can individualize the "resource" by just excepting some methods or just using some, eg:

Route::resource('weights', 'WeightController', ['except' => ['create']]);

Route::resource('geckos', 'GeckoController', ['only' => ['index', 'create', 'show']]);
1 like
Snapey's avatar

A couple of observations

Just using the name of the gecko, you are going to need a means to deal with name collisions. Or will you have like hotmail with 'Zilly2014'

If you have specific routes then make sure these come before 'catchall' resource routes

I think there is a semantic debate to be had as to whether images should be as you have or /gecko/images/{gecko_name}/upload

andyjh07's avatar

@frezno Thanks for that info, I think I'll restructure my routes file today as I wasn't aware that was available haha.

@Snapey The logic behind naming is all sorted don't worry. My system is working well, I'm just crap with knowing the best way to do the API.

I think your suggestion of /gecko/images/{gecko_name}/upload is good, would you also do something like /gecko/images/{gecko_name} in order to view a gallery?

Thanks for your feedback guys, trying to think of a logical route is difficult at times haha.

Please or to participate in this conversation.