What is Restful Routing?

2019-01-22 21:42发布

I'm familiar with Ruby On Rails's routing system and well as how Code Igniter and PhpCake route things but is there more to it than having a centralized location where you give out routes based on a directory structure? Like this

controller/action/id/
Admin/editUser/22

5条回答
Deceive 欺骗
2楼-- · 2019-01-22 21:46

it maps HTTP VERBS + URL to a specific action in the controller

Example:

GET /users/1      

goes to :

:controller => 'users', :action => 'show'

to see the full mapping, go to the terminal, and type:

rake routes
查看更多
小情绪 Triste *
3楼-- · 2019-01-22 21:47

One big part of the whole restful thing is that you should use the different HTTP methods to represent different actions.

For example in Rails if you were to send a HTTP Delete to /users/[id] it would signify that you want to delete that user. HTTP Get would retrieve an appropriate representation of the user. HTTP Put can update or create a user.

These are some examples, but since there is no standard for RESTful API's in HTTP this is not correct in all cases.

查看更多
走好不送
4楼-- · 2019-01-22 21:57

The basic premise is, instead of relying exclusively on the URL to indicate what webpage you want to go to (and just using the one method), it's a combination of VERB and URL.

This way, the same URL, when used with a different verb (such as GET, PUT, POST, DELETE), will get you to a different page. This makes for cleaner, shorter URLs, and is particularly adapted to CRUD applications, which most web apps are.

查看更多
Explosion°爆炸
5楼-- · 2019-01-22 21:58

RESTful Rails routes, i think that this shows the principle of REST

/users/       method="GET"     # :controller => 'users', :action => 'index'
/users/1      method="GET"     # :controller => 'users', :action => 'show'
/users/new    method="GET"     # :controller => 'users', :action => 'new'
/users/       method="POST"    # :controller => 'users', :action => 'create'
/users/1/edit method="GET"     # :controller => 'users', :action => 'edit'
/users/1      method="PUT"     # :controller => 'users', :action => 'update'
/users/1      method="DELETE"  # :controller => 'users', :action => 'destroy'
查看更多
倾城 Initia
6楼-- · 2019-01-22 22:00

@edtsech is correct. I would like to add one more thing here.

In the case of update,the method is "POST" with a hidden field which contains the data need to be updated.

So PUT = POST + Hidden field.

查看更多
登录 后发表回答