newton-peixoto

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

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 conn variable containing a Plug.Conn struct and it needs to return a connection, as per the Plug spec…
Each Plug.Router has a plug pipeline, defined by Plug.Builder, and by default it requires two plugs: :match and :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

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.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement