Routing in PHP vs routing in Rails
I was working on PHP in the past 1 year and 开发者_如何学Gonowadays I'm learning Rails.
In rails:-- Routing takes an incoming URL and decodes it into a set of parameters that are used by Rails to dispatch to the appropriate controller and action
For example
rs.recognize_path "/blog/show/123"
{:controller=>"blog", :action=>"show", :id=>"123"}
Am I right?
We mention this (written down) line of code in our routes.rb under config directory to tell rails how to handle the request like "/blog/show/123" using this line of code.
map.connect "blog/show/:id", :controller => "blog", :action => "show", :id => /\d+/
Now in PHP when we do something like this
www.example.com/profile.php?profile_id=2
How is the request sent to the requested page? Means I never wrote anything for routing in PHP, so how hss this request been handled? How is the routing done in PHP (anything I missed during my learning/working in PHP)?
Hopefully you get what I am asking. Please let me know if there is any part unclear.
You cannot really compare Rails to PHP - it's not even like comparing apples and oranges; it's like comparing an apple to orange tree. Generally, there are three things to consider:
- A webserver - a piece of software that handles incoming connections (eg. Apache, nginx, Eebrick)
- Interpreter - application that executes dynamic scripts (eg. Ruby, PHP, Perl)
- Web framework - set of libraries and utility classes that help develop web applications (eg. Rails, CakePHP, Code Igniter, Django)
In the simplest case, webserver uses incoming URL as a path of a file that user requests:
http://example.com/example.php -> /var/www/example.php
http://example.com/other.rb -> /var/www/other.rb
Most static files (images, styles etc) are served this way.
You can pass some parameters using query string (in form ?a=foo&b=bar
). However, you can configure your webserver to route URLS using more sophisticated rules. In Apache, for example, you can use mod_rewrite
to specify some rules using regular expressions that map URLs to other URLS. For example, in Apache:
RewriteRule /foo/(.*)$ /index.php?id=$1
will map every request starting with /foo/
to file index.php and pass remaining part of URL as id
parameter:
http://example.com/foo/bar -> /var/www/index.php?id=bar
http://example.com/foo/other/bar -> /var/www/index.php?id=other/bar
Now, it's up to your application what to do with requests. In Rails applications URLs are mapped directly to actions in controllers. In PHP you can use frameworks that behave the same way. For example in CakePHP, request to /posts/show/2 will execute method show(2) in PostsController class. There is also Router class that dispatches requests to controllers.
Hope my answer helped a bit;)
With your PHP example, the page is found by looking at the given path profile.php
. This file is searched for by your webserver and (if found) executed.
In Rails the URLs are matched against routes to find the corresponding controller. In your Rails example blog
is mapped against the BlogController
. Now Rails knows that the file containing the controller can be found as apps/controllers/blog_controller.rb
.
Each controller has actions so the show
part is matched against the show
action of the BlogController, which is represented by a show
method in the controller.
For information about Rails routes, read the Routing Guide of Rails.
So to be short
- in PHP your URLs are matched against actual files: very simple no routes required.
- in Rails your URLs can be more sophisticated (controller/action possibilities) but require routes.
www.example.com/profile.php?profile_id=2
The ?
separates the resource/object of the URI from the query string. The browser sends the whole URI to the server, which looks for www.example.com/profile.php
and passes the information that the profile was GET
-requested with profile_id=2
. PHP parses this information and makes it available in an array called $_GET
, specifically in this form:
array(
'profile_id' => 2
)
The profile.php
script can now read that info by reading out $_GET['profile_id']
.
Since the 'Rails-routed URI' you quoted is better for SEO, have this snippet of further information: You can achieve routing much like in Rails if you use a .htaccess file or equivalent that maps your 'Rails-routed URI's to the resource profile.php
.
You can also use frameworks for help. The nearest equivalent (in that transition to it should come naturally due to comparable syntaxes used) I know of off-hand would be if you used the Zend Framework's Router.
In Rails, when the webserver receives a request, the webserver "dispatches" the request to an action in a controller. As you mention, how the dispatchting is done, is defined in the routing table.
In simple PHP projects, the incoming HTTP request is mapped to a view with controller and database logic mixed in the same file. Without using the MVC pattern, you most likely end up with duplicated code for similar actions, lose flexibility (e.g. filtering, pre- or post-processing an URL) or have risks of having errors and vulnabilities in your code.
R u getting what i am asking.
Not quite well.
How the request is sent to the requested page.
Browser does send /profile.php?profile_id=2
request to the www.example.com
host
How the routing is done in php
Exactly the same way.
list($controller,$blog,$id)=explode("/","/blog/show/123");
精彩评论