WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Using scope with dynamic path segments, PATH_INFO and SCRIPT_NAME #291

@ahx

Description

@ahx

Hi. I am a bit stuck when trying to use hanami-router with dynamic path segments using scope.

Consider these two examples and the resulting PATH_INFO and SCRIPT_NAME fields in the rack env. For both examples let's look at the request GET /stations/42/trains.

Example A - scope with dynamic segment

# config.ru
Router = Hanami::Router.new do
  scope '/stations/:station_id' do
    mount ->(env) { binding.irb }, at: '/'
  end
end
run Router

# "SCRIPT_NAME" => "/stations/:station_id"
# "PATH_INFO" => "/stations/42/trains",
# Rack::Request.new(env).path # => "/stations/:station_id/stations/42/trains"

Example B - scope without dynamic segment

Router = Hanami::Router.new do
  scope '/stations' do
    mount ->(env) { binding.irb }, at: '/'
  end
end
run Router

# # "SCRIPT_NAME" => "/stations"
# # "PATH_INFO" => "/42/trains"
# Rack::Request.new(env).path # => "/stations/42/trains"

The issue I have with this is that using a scope with a dynamic segment (Ex A) makes Rack::Request#path unusable. – I think it somehow makes sense that SCRIPT_NAME is actually "/stations/:station_id" here, but it's hard to get the actual request when using Rack::Request#path.

Questions:

  • Is this behavior intended?
  • My assumption is that you should be able to get to the actual request path by concatenating SCRIPT_NAME + PATH_INFO - Is that wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions