diff --git a/padrino-core/lib/padrino-core/application/routing.rb b/padrino-core/lib/padrino-core/application/routing.rb index 395107af1..2f079ef9b 100644 --- a/padrino-core/lib/padrino-core/application/routing.rb +++ b/padrino-core/lib/padrino-core/application/routing.rb @@ -582,10 +582,7 @@ def url(*args) else compiled_router.path(name, *(params_array << params)) end - url[0,0] = conform_uri(uri_root) if defined?(uri_root) - url[0,0] = conform_uri(ENV['RACK_BASE_URI']) if ENV['RACK_BASE_URI'] - url = "/" if url.blank? - url + rebase_url(url) rescue HttpRouter::InvalidRouteException route_error = "route mapping for url(#{name.inspect}) could not be found!" raise Padrino::Routing::UnrecognizedException.new(route_error) @@ -600,6 +597,17 @@ def get(path, *args, &block) route('HEAD', path, *args, &block) end + def rebase_url(url) + if url.start_with?('/') + new_url = '' + new_url << conform_uri(uri_root) if defined?(uri_root) + new_url << conform_uri(ENV['RACK_BASE_URI']) if ENV['RACK_BASE_URI'] + new_url << url + else + url.blank? ? '/' : url + end + end + private # Parse params from the url method def value_to_param(value) @@ -944,17 +952,24 @@ module InstanceMethods # url(:show, :id => 1) # url(:show, :name => :test) # url(:show, 1) - # url("/foo") + # url("/foo", false, false) # # @see Padrino::Routing::ClassMethods#url # def url(*args) - # Delegate to Sinatra 1.2 for simple url("/foo") - # http://www.sinatrarb.com/intro#Generating%20URLs - return super if args.first.is_a?(String) && !args[1].is_a?(Hash) - - # Delegate to Padrino named route URL generation. - settings.url(*args) + if args.first.is_a?(String) + url_path = settings.rebase_url(args.shift) + if args.empty? + url_path + else + # Delegate sinatra-style urls to Sinatra. Ex: url("/foo", false, false) + # http://www.sinatrarb.com/intro#Generating%20URLs + super url_path, *args + end + else + # Delegate to Padrino named route URL generation. + settings.url(*args) + end end alias :url_for :url diff --git a/padrino-core/test/test_routing.rb b/padrino-core/test/test_routing.rb index 2a53ac69e..88453e60d 100644 --- a/padrino-core/test/test_routing.rb +++ b/padrino-core/test/test_routing.rb @@ -296,6 +296,24 @@ class Test < Padrino::Application assert_equal 'http://example.org/test/foo?id=1', body end + should 'rebase simple string urls to app uri_root' do + mock_app do + set :uri_root, '/app' + get(:a){ url('/foo') } + get(:b){ url('bar') } + get(:c){ absolute_url('/foo') } + get(:d){ absolute_url('bar') } + end + get "/a" + assert_equal "/app/foo", body + get "/b" + assert_equal "bar", body + get "/c" + assert_equal "http://example.org/app/foo", body + get "/d" + assert_equal "http://example.org/bar", body + end + should 'allow regex url with format' do mock_app do get(/.*/, :provides => :any) { "regexp" }