StevenXL
Hello,
I was reading the Phoenix Guides on module plugs here and was confused as to how the module plug example was working. I was going to ask here for help, but in the process I believe I figured it out. (Happy to be corrected if that’s not the case).
I’ll just leave this here in case someone else is confused.
Line -3 (plug HelloPhoenix.Plugs.Locale, "en") is indicating that, when this plug is executed, we will invoke the HelloPhoenix.Plugs.Locale.init/1 function, passing in the argument "en".
The init/1 function will return a value, which will be used as the second argument to HelloPhoenix.Plugs.Locale.call/2.
If the first clause of call/2 matches, then we know that:
- There was a
localeURL parameter - The value of that parameter was in
@locale
We are happy with this so we assign the locale value to the connection.
If the second-clause of call/2 matches, then we know that we need to provide a default, because there was either no locale URL parameter, or the value of it was not in @locale. The default, again, came from whatever init/1 returned, which in the example is "en".
Trending in Questions
Other Trending Topics
Latest Phoenix Threads
Latest on Elixir Forum
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
- #blog-post
- #ai
- #phoenix_html
- #iex
- #elixirconf-us
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
This is basically correct, with one minor adjustment.
In most scenarios where
plugis implemented, theinitfunction is actually called exactly once at compile time to setup the options for the plug. Then at runtime when an actual connection is being processed thecallfunction is called on the conn with the opts derived at compile time.StevenXL
@benwilson512 thank you for the clarification!