views:

281

answers:

5

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

A: 

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.xxx.com host

How the rounting is done in php

Exactly the same way.

list($controller,$blog,$id)=explode("/","/blog/show/123");
Col. Shrapnel
@col. Shrapnel I m not getting why u sayin question not clear..Will u please eloborate which part is not clear..
piemesons
@piemesons I didn't get what is the question, until it was edited
Col. Shrapnel
+1  A: 

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.
Veger
Oh never knew that Rails act as a webserver
Col. Shrapnel
@Veger... is that so simple.. Really?? means i m shocked to see so simple and acceptable explanation
piemesons
@Col. Shrapnel: Rails is not acting as a webserver. WEBrick is the provided webserver for default Rails projects. It can be started by the script/server command (when your current directory is set to the main directory of your Rails application)But Rails does handle the routing, after it received a page request which got passed through by the webserver.
Veger
@piemesons: I advice to read the Routing Guide page I gave. It contains all basic knowledge of the Rails routing system.
Veger
@Vegar yaa i m doin that.
piemesons
So, it is not rails where "URLs can be more sophisticated" but some WEBrick
Col. Shrapnel
Rails handles the routes, WEBrick just passes them through. So Rails *does* provide the sophisticated (RESTful) routes and not the webserver.
Veger
So you think to split a string and assign values to variables is too sophisticated task for PHP? :)
Col. Shrapnel
Nah... PHP is perfectly able to do so. But in PHP the correct script is found by the webserver. In this script you could use paramters for some routing tasks. In Rails the controller (like the PHP script) is found by the Rails routing system. That's the difference. This is not about whether things are possible or not or which is better (at least in my story it was not intended), but about how things are done.
Veger
+2  A: 
www.xxx.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.xxx.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.

pinkgothic
+1  A: 

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.

poseid
+1  A: 

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:

  1. A webserver - a piece of software that handles incoming connections (eg. Apache, nginx, Eebrick)
  2. Interpreter - application that executes dynamic scripts (eg. Ruby, PHP, Perl)
  3. 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;)

el.pescado
thnkssss.......
piemesons