laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.51k stars 11.02k forks source link

Resourceful URL generation #37

Closed ShawnMcCool closed 11 years ago

ShawnMcCool commented 11 years ago

I think that it'd be good to have the URL class able to generate resourceful links to match resourceful routing.

Maybe something like URL::resource(resource route, parameters)

Resource Routes for a user: userIndex, userCreate, userStore, userShow, userEdit, userUpdate, userDestroy

URL::resource('userCreate') URL::resource('userEdit', $user->id)

niallobrien commented 11 years ago

+1. Would be awesome to have. Perhaps URL::resource(resource route, method, parameters) would be better as the methods in our controller class are not prefixed with the resource name? Just a thought.

JeffreyWay commented 11 years ago

Shawn - do you think something like this would even be necessary, if the resourceful controller generator automatically created the named routes? I think I might prefer to stick with:

$url = URL::to_route( 'edit_user_path', array($id) );

...One less new thing to learn, and fully readable.

ShawnMcCool commented 11 years ago

Yea, I think you're right.

On Mon, Jan 14, 2013 at 3:47 PM, Jeffrey Way notifications@github.comwrote:

Shawn - do you think something like this would even be necessary, if the resourceful controller generator automatically created the named routes? I think I might prefer to stick with:

$url = URL::to_route( 'edit_user_path', array($id) );

...One less new thing to learn, and fully readable.

— Reply to this email directly or view it on GitHubhttps://github.com/laravel/framework/issues/37#issuecomment-12221088.

Shawn McCool | Big Name shawn@heybigname.com heybigname.com

niallobrien commented 11 years ago

I agree, providing we have named routes for resources. Also means that Meido's HTML package can still be used to create links.

taylorotwell commented 11 years ago

Coming back around to this getting ready to implement something. What do you guys think about using "dot" syntax for the route names on resourceful routes?

Like...

URL::route('user.edit', [1]);

URL::route('user'); // would point to index...

Also planning on implementing nested resources, so like:

URL::route('magazine.ads.edit', [1, 2]); // 1 being the magazine ID and 2 being the ad ID
taylorotwell commented 11 years ago

I suggest dots because we use those throughout the framework for other things and to indicate nesting, etc.

ShawnMcCool commented 11 years ago

I don't think that it's a bad convention. If you create a new convention for every type of thing then you're going to run out of delimiters quickly.

niallobrien commented 11 years ago

@taylorotwell Am I correct in assuming that we reference the controller method? Eg. URL::route('user.destroy', [1]); ? Just to clarify, routing to the action, not a named route variant.

franzliedke commented 11 years ago

This looks cool. And I agree, dots feel right.

taylorotwell commented 11 years ago

@niallobrien the name happens to match the controller action, yeah, but it's a true named route. I just thought it would be convenient if they matched the action names.

JeffreyWay commented 11 years ago

I think that would work just fine. My only concern might relate to whether it confuses people. For URL::route, they'd use dot notation (preferred). For URL::action, they'd use @. Not a big deal, though.

Or, I wonder which would be considered more readable/recognizable by the community:

URL::route('user.create'); 

// or

URL::route('create_user');

Either way, I think both fit nicely.

niallobrien commented 11 years ago

Good point @JeffreyWay. People are used to @ representing a controller action.

taylorotwell commented 11 years ago

Using @ could still be a tad confusing because it's not really generating anything from a controller action (even though it kinda looks that way), it's a true named route. I think we'll just stick with dots and see how it's received.

taylorotwell commented 11 years ago

This has been implemented. Resource routes also use wildcards matching the resource names so that they may be easily used with Route::model. Also, nested resources are now supported using "dot" syntax as well:

Route::resource('magazine.photo', 'PhotoController');
raftalks commented 11 years ago

@taylorotwell I noted that due to this change, I am not able to workout my api routes which is actually segmented under "api/" path for all my resources, and it is now that route::resource() no longer accept slash. Is it possible to workout a solution as I have proposed here #181 and #170 was also taken note of this issue.

GaryJones commented 11 years ago

Resource routes also use wildcards matching the resource names so that they may be easily used with Route::model

Does that explain #182?