Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: simplest way to get RESTful urls

Currently I have the usual Controller/Action structure:

BlogController:

/blog/list  
/blog/create  
/blog/detail/my-blog-hash

And PostController

/post/create  
/post/detail/my-post-hash

What I would like is a URL for the blog post detail view:

/blog/detail/my-blog-hash/post/my-post-hash

I know there is a RESTBundle, but this is overcomplicating things IMO and there are some real issues with routes and form validation which I was not able to solve (even with help of the guys on IRC). I don't need accesspoints for JSON, serializers, special views etc., I just need a way to stack controllers somehow.

Can someone help me with this?

like image 433
stoefln Avatar asked Jan 12 '12 21:01

stoefln


2 Answers

This doesn't answer your specific question but I wanted to recommend a cleaner URL design that is more RESTful.

To work with blogs in general interact with the /blogs base resource.

POST /blogs to create a blog.

GET /blogs to list all the blogs.

To work with a specific blog you then specify which blog.

GET /blogs/:id to get the details for the specific blog.

Now specific which subresource of blogs you want to interact with.

POST /blogs/:id/posts to create a new post.

GET /blogs/:id/posts/:id to get details of a specific post for a specific blog.

like image 56
abraham Avatar answered Oct 26 '22 03:10

abraham


Thinking RESTfully, you have two resources: blogs and posts.

In a RESTful route, the action is implied by the http verb. I understand you are not in Rails, but these simple tables illustrate the "Rails way" of doing REST and RESTful nested resources:

http://edgeguides.rubyonrails.org/routing.html#crud-verbs-and-actions

http://edgeguides.rubyonrails.org/routing.html#nested-resources

Very clean.

like image 45
Robert Head Avatar answered Oct 26 '22 03:10

Robert Head