Mohit Jain Mohit Jain - 3 months ago 8
PHP Question

Routing in PHP vs routing in Rails

I was working on PHP in the past 1 year and nowadays 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.

Answer

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.
Comments