newton-peixoto
Controllers on vanilla plug router
Hello everyone, is it possible to dispatch a request to a specific module using plug.router? Phoenix does it using its own macros but I’d like to know how to achieve the same or at least close results using only plug.router
Marked As Solved
codeanpeace
Off the top of my head, it might be worth trying to pass in conn without using the capture operator. Also, check out these other requirements from the Plug.Router docs:
Each route receives a
connvariable containing aPlug.Connstruct and it needs to return a connection, as per the Plug spec…
EachPlug.Routerhas a plug pipeline, defined byPlug.Builder, and by default it requires two plugs::matchand:dispatch. ``
So maybe something like this for a minimal example using only Plug.Router:
defmodule AppRouter do
use Plug.Router
plug :match # required by default
plug :dispatch # required by default
get "/baz" do
HelloWorld.Controller.show(conn) # must accept and return a connection
end
end
Last Post!
LostKobrakai
Another option is using forward. Phoenix controllers are also module plugs with MyApp.Controller.call(conn, :action)
So e.g.
defmodule AppRouter do
use Plug.Router
plug :match # required by default
plug :dispatch # required by default
forward "/baz", to: HelloWorld.Controller, init_opts: :show
end
Keep in mind that Plug.Router.forward has different options than Phoenix.Router.forward, so don’t confuse them.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









