Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resourceful vs restful controller routing #34

Closed
niallobrien opened this issue Jan 14, 2013 · 30 comments
Closed

Resourceful vs restful controller routing #34

niallobrien opened this issue Jan 14, 2013 · 30 comments

Comments

@niallobrien
Copy link

Are we switching to resourceful routing over restful routing for any particular reason?
Is there a reason why they can't both exist?
Are we worried that this will make the framework less accessible?

Thanks.

@buhl
Copy link

buhl commented Jan 14, 2013

AFAIK both are still there. (just not in the documentation yet)

@niallobrien
Copy link
Author

@bgm Not true.
Try generate a link or URL to a controller method named getIndex(). You will have to access it via HomeController@getIndex. This is not restful as it was in Laravel 3 when $restful = true;

@ktee55
Copy link

ktee55 commented Jan 14, 2013

Personally I like the old manual way of restful routing, that way I feel like I have more control over it; change as what you want; no need for "composer dump autoload". If we copy&paste or take advantage of snippets it's not such a big deal and with st2 things are much easier: https://gist.github.com/3717337

But I guess we can still go for the old way in someway, aren't we? I don't know.

@niallobrien
Copy link
Author

@Ktee8 Why use that snippet when you can just do something like Route::resource('users', 'UsersController'); in L4 now?

@niallobrien
Copy link
Author

Does L4 automatically generate named routes for resources like Rails? I only ask because I don't see anything in the URL class for resources, but do for actions and named routes and was wondering what's the best way to do this if we're going with 100% resourceful?

@ktee55
Copy link

ktee55 commented Jan 14, 2013

Could it be your answer? https://github.com/daylerees/autoload-demo I'm sorry if I'm off target.

@niallobrien
Copy link
Author

@Ktee8 No, sorry. That issue is not directly related to this one.

@JeffreyWay
Copy link
Contributor

Thinking in terms of resources is inherently restful. So it's not really an issue of choosing resourceful vs. restful.

When Laravel generates a full resource for you - ( Route::resource('users', 'UsersController') - that's still following the RESTful architecture (similar to the Rails flavor).

@JeffreyWay
Copy link
Contributor

In reference to whether L4 generates the named routes, as part of Route::resource, I'm not sure. I asked Taylor about that a month or so ago, and he seemed to imply that he would implement that.

@buhl
Copy link

buhl commented Jan 14, 2013

@niallobrien how about

Route::controller('UserController','users');

@niallobrien
Copy link
Author

@JeffreyWay Yes, but in Laravel 3 we could do something like get_login() and post_login() if we wanted to when $restful = true;
I also note that there's not a nice way of actually generating URLs for performing CRUD on resources. For example, if I want to edit my profile, how do I generate the url to /users/4/edit ?
See: #37

@JeffreyWay
Copy link
Contributor

Well ideally, when/if Taylor adds named route support to resourceful controller creation, then you'd do something along the lines of:

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

But I guess, until then, you have to do it manually??

@niallobrien
Copy link
Author

@JeffreyWay Yeah, that's what we were discussing in #laravel IRC. I think adding a URL::resource() method would be a cleaner solution, but both would work.

Also, while on the topic of resources, I don't see anything there for nested resources. Might open a new issue for that.

@JeffreyWay
Copy link
Contributor

Hmm - I'm looking over the URL::resource ticket that Shawn just added. Not sure I think that's preferable to clean named routes.

I thought I read something from Taylor on Twitter about implementing nested resources. Maybe not...

@ktee55
Copy link

ktee55 commented Jan 14, 2013

Named routes: that's what I wanted to say instead of "old manual way of restful routing"; then why you need that restful snippet? 'cause I want something to refer to and change some parts as what I want; then if with Route::resource, I can make it, that's exactly what I want. Thanks.

@JeffreyWay
Copy link
Contributor

You don't need my restful snippet in L4. Pretty sure resourceful controllers were implemented to keep us from having to resort to big snippets like that to achieve fairly standard resource + named routes setups.

@niallobrien
Copy link
Author

@JeffreyWay Actually now that you say it, named routes for resources probably are the right way to go. Less things to remember (action vs named routing) and a big bonus is that people already using Meido's HTML package can still use it to generate links to resources via named routes! :)
Would be nice if @taylorotwell could comment on nested resources.

@raftalks
Copy link
Contributor

I support named routes for resource controllers.

@ktee55
Copy link

ktee55 commented Jan 14, 2013

@JeffreyWay Thanks. I guess I'm beginning to see the trick.

@niallobrien Sorry for a bit out of topic you intend; but some amateurs like me might benefit from it. Thank you so much.

@niallobrien
Copy link
Author

So something like Rails (I don't think we need '_path')?
URL::route('photos') returns /photos
URL::route('new_photo') returns /photos/new
URL::route( 'edit_photo', ['id' => $id]) returns /photos/{id}/edit (for instance, edit_photo(10) returns /photos/10/edit)
URL::route( 'photo', ['id' => $id]) returns /photos/{id} (for instance, photo(10) returns /photos/10)

Looks nice and clean to me.

@JeffreyWay
Copy link
Contributor

Yeah, works for me. And then a command line option to view all the registered named routes, for convenience. Something like:

php artisan routes

@niallobrien
Copy link
Author

@JeffreyWay That would be badass! :)

@bencorlett
Copy link
Contributor

Possibly you are getting confused with the terminology?

REST by it's very design is resourceful. Rather than rattle on, take a look at this which goes into a bit of depth about API design. You will understand why the new resourceful setup is awesome :)

@niallobrien
Copy link
Author

@bencorlett Yes, I am aware. However, I was initially referring to L3's implementation of restful controllers when $restful = true;
This functionality is not available in L4. I have yet to check Route::controller() which I think implements this functionality in L4 (correct me if I'm wrong).
Perhaps re-reading my previous comments would save me from having to "rattle" on.

@robclancy
Copy link
Contributor

You are asking for what is Route::controller... it is your RESTfull controller from L3. Resource is like a more specific (something used in Admin a lot) RESTfull controller.

@niallobrien
Copy link
Author

@Robbo- Yes, I was beginning to think that. Must fully test and contribute to the docs.

@ktee55
Copy link

ktee55 commented Jan 15, 2013

Sorry for keeping amateur questions and using wrong terminology;
but I choose Laravel because it's easy to understand
and looked for every level of developers, give me a room.

How about something like this?

Route::get('/', array('as'=>'home', 'uses'=>'questions@index'));
Route::get('register', array('as'=>'register', 'uses'=>'users@new'));
Route::get('login', array('as'=>'login', 'uses'=>'users@login'));
Route::get('logout', array('as'=>'logout', 'uses'=>'users@logout'));

Route::post('register', array('before'=>'csrf', 'uses'=>'users@create'));
Route::post('login', array('before'=>'csrf', 'uses'=>'users@login'));

That's from a Nettus+ Course that I'm taking now.
https://tutsplus.com/course/hands-on-building-a-practical-web-application-with-laravel/

At first I thought it's a bit different from RESTful pattern that I learned a while back;
but have the same concept and could make sense sometimes.

Then my wonder is if it could be achieved by Route::resource in some way easily,
or I should do it by old way of routing like in L3. If it would force me a hassle,
I'd rather stick to the old way even at the sacrifice of new way of advantages;

That's what I've been thinking and talking about here.

@JeffreyWay
Copy link
Contributor

Yes, the routes in Andrew's course could be modified to use L4's resourceful controllers.

@ktee55
Copy link

ktee55 commented Jan 15, 2013

Thanks! The fact reassurances me a lot! :)

@taylorotwell
Copy link
Member

A lot of the issues in this thread have been fixed. Closing.

joelharkes pushed a commit to joelharkes/framework_old that referenced this issue Mar 7, 2019
dbpolito pushed a commit to dbpolito/framework that referenced this issue Oct 1, 2019
gonzalom pushed a commit to Hydrane/tmp-laravel-framework that referenced this issue Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants