Newbie question: Global mappings

3 views
Skip to first unread message

Mike Barton

unread,
May 3, 2008, 7:22:04 AM5/3/08
to Ramaze
Hi,

I'm new to Ramaze and web development in general, but the
documentation has made it simple for me to get a web site started.

However I'm trying to develop a simple Ramaze site under a wordpress
installation. I'm using lighttpd and mod_proxy to forward to the
correct port based on the url. This however means that the url is
prefixed by index.php/... etc.

Using map '/index.php/' method, I can fix this however this is not
very DRY as I have to apply this in all the controllers. I wonder if
there is a way to globally prefix all the controllers with a given
string?

Thanks

Mike

ara.t.howard

unread,
May 3, 2008, 10:40:27 AM5/3/08
to ram...@googlegroups.com

On May 3, 2008, at 5:22 AM, Mike Barton wrote:
> Using map '/index.php/' method, I can fix this however this is not
> very DRY as I have to apply this in all the controllers. I wonder if
> there is a way to globally prefix all the controllers with a given
> string?


Ramaze::Route( 'stripper' ) do |path, request|

match, path = %r|^/+index.php|.match(request.path_info).to_a

path if match

end

or similar should do - this simply nukes it off the front of all
incoming requests and re-routes.

this works only for controllers though - if you are serving static
files (css, etc) you may need to write a custom dispatcher to replace
the current File::Dispatcher...

cheers.

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama

Aman Gupta

unread,
May 3, 2008, 3:03:58 PM5/3/08
to ram...@googlegroups.com
Routes will work, but they're not ideal for this case as they're evaluated after the Dispatch cycle. You could use something like:

Ramaze::Current.before do
  Ramaze::Request.current.env.each do |key,val|
    val.sub!(%r|^/index.php|, '')
  end
end

ara.t.howard

unread,
May 3, 2008, 3:06:22 PM5/3/08
to ram...@googlegroups.com

On May 3, 2008, at 1:03 PM, Aman Gupta wrote:

> Ramaze::Current.before do
> Ramaze::Request.current.env.each do |key,val|
> val.sub!(%r|^/index.php|, '')
> end
> end


i'm not sure that'll work - before fires after the request has been
setup... maybe it should be moved?

Aman Gupta

unread,
May 3, 2008, 3:13:11 PM5/3/08
to ram...@googlegroups.com
Just tried it, and this seems to work:

require 'ramaze'

class MainController < Ramaze::Controller
  def index
    'hi'
  end

  def yo
    'yo'
  end
end

Ramaze::Current.before do
  Ramaze::Request.current.http_vars.each do |key,val|
    val.sub!(%r|^/index.php|, '')
  end
end

  Aman Gupta

Ramaze.start :adapter => :mongrel

ara.t.howard

unread,
May 3, 2008, 3:18:56 PM5/3/08
to ram...@googlegroups.com

On May 3, 2008, at 1:13 PM, Aman Gupta wrote:

> Just tried it, and this seems to work:

okay cool - did you try a static request? /css/site.css ??

Aman Gupta

unread,
May 4, 2008, 6:11:36 PM5/4/08
to ram...@googlegroups.com
Static requests work as well.

The latest darcs repo also adds a Ramaze::Rewrite, which is similar to Route but run before the action dispatcher. You can use something like:

require 'ramaze'

class MainController < Ramaze::Controller
  def index
    'hi'
  end

  def yo
    'yo'
  end
end

Ramaze::Rewrite[%r|^/index.php(.*)$|] = "%s"
Ramaze.start :adapter => :mongrel

Rewrites are run after the file dispatcher however, so static file requests will not work with this approach.

  Aman Gupta

ara.t.howard

unread,
May 4, 2008, 6:32:16 PM5/4/08
to ram...@googlegroups.com

On May 4, 2008, at 4:11 PM, Aman Gupta wrote:

>
> Ramaze::Rewrite[%r|^/index.php(.*)$|] = "%s"
> Ramaze.start :adapter => :mongrel
>
> Rewrites are run after the file dispatcher however, so static file
> requests will not work with this approach.
>

oh that's very cool.

i like

Rewrite %r|^/index.php(.*)$| => "%s"

as a syntax though

it'd also be cool to have them run before file dispatching... being
able to alter static requests in ramaze is very very powerful.

Aman Gupta

unread,
May 4, 2008, 7:24:48 PM5/4/08
to ram...@googlegroups.com
Agreed, the only issue is that those rewrites will be ignored if you setup nginx/lighty/apache in front of your ramaze instance to serve static files. That's probably fine though... it's your job to replicate those rewrites in your front-end proxy.

  Aman Gupta

ara.t.howard

unread,
May 4, 2008, 8:37:03 PM5/4/08
to ram...@googlegroups.com

On May 4, 2008, at 5:24 PM, Aman Gupta wrote:

> Agreed, the only issue is that those rewrites will be ignored if you
> setup nginx/lighty/apache in front of your ramaze instance to serve
> static files. That's probably fine though... it's your job to
> replicate those rewrites in your front-end proxy.

yup, exactly. no way around it i think - if you have crazy complex
static serving, like for a site of sites, you would indeed have to
replicated that logic or just let ramaze do it. ramaze does it very
well know, with minimal pain. but easier is always better ;-)

Aman Gupta

unread,
May 9, 2008, 7:43:36 PM5/9/08
to ram...@googlegroups.com
Rewrite rules are now evaluated before the file dispatcher: http://github.com/manveru/ramaze/commit/de363b2a83997c12f54c28ce8c3565b61819e586

  Aman Gupta

Mike Barton

unread,
May 12, 2008, 8:16:46 AM5/12/08
to Ramaze
Thank you for the information on this.

At the moment though I am not able to load Ramaze::Rewrite.

I have tried

require 'ramaze'
require 'ramaze/route'

However I get this error

./start.rb:9: uninitialized constant Ramaze::Rewrite (NameError)

I've also tried cloning the git repository and calling the files
directly

require File.dirname(__FILE__) + '/../vendor/ramaze/lib/ramaze.rb'
require File.dirname(__FILE__) + '/../vendor/ramaze/lib/ramaze/
route.rb'

However I get this error

undefined method `path_rewrite='

I'm sure I'm being ignorant, but I've been playing around with
different require combinations but I can't seem to load
Ramaze::Rewrite

On May 10, 12:43 am, "Aman Gupta" <themastermi...@gmail.com> wrote:
> Rewrite rules are now evaluated before the file dispatcher:http://github.com/manveru/ramaze/commit/de363b2a83997c12f54c28ce8c356...
>   Aman Gupta
>
> On Sun, May 4, 2008 at 5:37 PM, ara.t.howard <ara.t.how...@gmail.com> wrote:
>
> > On May 4, 2008, at 5:24 PM, Aman Gupta wrote:
>
> > > Agreed, the only issue is that those rewrites will be ignored if you
> > > setup nginx/lighty/apache in front of your ramaze instance to serve
> > > static files. That's probably fine though... it's your job to
> > > replicate those rewrites in your front-end proxy.
>
> > yup, exactly.  no way around it i think - if you have crazy complex
> > static serving, like for a site of sites, you would indeed have to
> > replicated that logic or just let ramaze do it.  ramaze does it very
> > well know, with minimal pain.  but easier is always better ;-)
>
> > a @http://codeforpeople.com/

Mike Barton

unread,
May 12, 2008, 1:02:06 PM5/12/08
to Ramaze
Installing the latest nightly gem has fixed this.
Thanks again for your help.
Reply all
Reply to author
Forward
0 new messages