-
-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation about Controller/Router integration is incorrect #427
Comments
I actually feel even more dumb now. The second example in the README of this repo references a Are the README examples outdated, or am I missing something obvious? 😕 |
@weppos Hey Simone, this is a leftover of past WIP implementation. My apologies. The fix is on the way. |
Hi @jodosha!
As you recall, we use only the router and controller. I am currently looking for some examples on how to make it happen. A simplified version of the current code looks like: Hanami::Router.define do
get "/accounts", to: "accounts#index"
end The app itself is the following. Note how the controllers where scoped on a specific namespace. class App
def self.routes
Hanami::Router.new(namespace: Api::V2::Controllers, &eval(File.read('path/to/routes.rb'))
end
def initialize
routes = self.class.routes
@app = Rack::Builder.new do
use Hanami::Middleware::BodyParser, :json
run routes
end
end
def call(env)
@app.call(env)
end
end and each action was module Api::V2
module Controllers::Accounts
class Index
include Hanami::Action
def call(_params)
render "..."
end
end
end
end There's a number of changed I think we'll have to make. I've noticed all actions are now subclassed from The piece I'm missing is how to initialize a router and connect it so that it resolves the actions. I was able to make this work: def self.routes
# Hanami::Router.new(namespace: Api::V3::Controllers) do
# get "/whoami", to: "authentication_context.show"
# end
Hanami::Router.new do
get "/", to: ->(*) { [200, {}, ["OK"]] }, as: :welcome
end
end but nothing more than that. The following were tests I made and that's when I discovered the various readme issues: def self.routes
Hanami::Router.new(namespace: Api::V3::Controllers) do # namespace doesn't exist anymore
get "/", to: ->(*) { [200, {}, ["OK"]] }, as: :welcome
end
end also require "hanami/controller"
module Api::V3
module Controllers::AuthenticationContext
class Show < Hanami::Action
def handle(*)
res.format = :json
res.body = {}
end
end
end
end def self.routes
Hanami::Router.new do
get "/", to: ->(*) { [200, {}, ["OK"]] }, as: :welcome
get "/test1", to: Api::V3::Controllers::AuthenticationContext::Show.new # error undefined local variable or method `res'
get "/test2", to: "authentication_context#show" # error undefined method `call' for "authentication_context#show":String
end
end I must be missing something obvious to connect a router get to the corresponding action. 😅 |
That is due to the definition of It can accept two args:
It really depends on what you need. |
Good catch! It was a left over from the various attempts I made to implement a working code from the docs 😅 |
One notable change from That simplified the library: it isn't a loader anymore but only a dispatcher. But there is a way to solve this problem: During the initialization time, for each declared route it gets invoked. Example: class MyResolver
def initialize(namespace: Api::V3::Controllers)
@namespace = namespace
end
def call(path, to)
puts "path: #{path}"
puts "to: #{to}"
class_name = to.classify
@namespace.const_get(class_name).new
end
end
router = Hanami::Router.new(resolver: MyResolver.new) do
get "/test2", to: "authentication_context/show"
end
# => "path /test2"
# => "to authentication_context/show" |
OK, thanks. We'll look into it. That's a lot of knowledge that would be nice to have somewhere documented. 😉 |
The
Hanami::Router
integration docs seems to be incorrect. It references two parameters (configuration
andnamespace
) that are not accepted by the current 2.xHanami::Router
version:controller/README.md
Lines 884 to 905 in 2eb8390
I also noticed that, across various docs and README, the route definition is sometimes referenced using the notation
books#show
whereas in other cases isbooks.show
.I wish I could provide a PR to update the docs, but after a few hours of digging into v2 docs and code, I've yet to figure out how to successfully define an action and reference to it in a router without using the fully qualified
to: ClassName.new
notation.The text was updated successfully, but these errors were encountered: