Welcome Guest, Not a member yet? Register   Sign In
respond_to just like on rails
#1

[eluser]Unknown[/eluser]
hey guys.. when I was reading about rails, I figured out that we can get the response from the server the way we want just telling on the controller what format we want to just like that:

www.example.com/aiports.js
Code:
class AirportsController < ApplicationController
#here we have the action
def index
    @airports = Airport.find :all
            #and here the format wich the method will respond
            respond_to do |format|
                format.html  # do nothing, allow Rails to render index.rhtml
                format.json    { render :json => @airports.to_json }
                format.xml   { render :xml => @airports.to_xml }
            end
    end
end

I think it is easy to implement on PHP, but the point is : how to insert the format (or extension) in front of the controller, as in "www.example.com/aiports.js". I know that must be something about rewrite in htaccess... but how?

some tips??

tks !
#2

[eluser]richthegeek[/eluser]
You could do this in the routes file, if you are doing it in CI;
Code:
$routes[ '([^\.]+)\.(.+)' ] = "$1/format/$2";
$routes[ 'respond([^\.]+)\.(.+)' ] = "respond/$1/$2";

That's the general idea at any rate.
#3

[eluser]Unknown[/eluser]
could you give me a usage example, please? tks




Theme © iAndrew 2016 - Forum software by © MyBB