Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Plug vs Module Plug

I wanted to write a plug for user authetication in my app. Was going through phoenix plug documentation and was a bit confused on which kind of plug to use, function or module.

Generally speaking also, of the two plugs which plug is preferred when?

like image 276
coderVishal Avatar asked Apr 01 '26 20:04

coderVishal


1 Answers

There are two main uses I can think of when you would want a function plug:

  1. You want a plug that is local to a file (controller, router, endpoint, etc.) A module plug is preferred if you want it to be available from multiple files.
  2. You are writing a library that allows you to use the function plug in the module (Phoenix does this for put_layout and scrub_params)

Most of the time, my plugs start as functions while I am developing them, then I move them out to their own modules.

The module plug has the benefit of being able to perform some login in the init/1 function that will later be passed to the call/2 function.

like image 106
Gazler Avatar answered Apr 03 '26 16:04

Gazler