the beauty of Elixir language works seamlessly here
- classic layers of Phoenix for HTTP request would like
connection |> endpoint() |> router() |> pipelines() |> controller()
Erlang |> Elixir |> Phoenix; installing Hexmix local.hex; Ecto can work withPostgrSQL
if Frontend is involved one need to mannage Assets; need
nodejs |> npmfor it.. Phoenix will usewebpackjsto compile
inotifyof linux for live reloading to work
- using Mix to install Phoenix
mix archive.install hex phx_new
mix phx.new -v
mix phx.new demo --live
mix phx.new.ecto just_apiandmix phx.new.web just_frontend_nodboptions are also available
- create a project
mix phx.new ehloandYto install dependencies
if install dependencies fail here
pushd ehlo
mix deps.get # for Phoenix deps
pushd assets
npm install && node node_modules/webpack/bin/webpack.js --mode development # for assets compilation
popd
mix deps.compile`# for phoenix deps
popd
- now cd to
ehlo;mix ecto.createto after DB config atconfig/dev.exsfor db model creation;mix phx.serverto run at:4000by default oriex -S mix phx.serverto run with REPL
in
config/dev.exsforMIX_ENV=dev; DB configs belong underconfig :ehlo, Ehlo.Repo, ..to change listening PORT or other server props goto,
config :ehlo, EhloWeb.Endpoint, ..visiting
/gives Getting Started page
- for 1st feature let's just print a string at a specific URL
the created
lib/ehlo_webhas dirs forchannels,controllers,templates/{layout,page},viewsalongwith files likerouter.ex,endpoint.ex,gettext.ex
-
router.exhere contains block of requests; with pre-definedget "/" ...under scope/ -
this scope shall handle for all paths starting
/; addget "ehlo", EhloController, :userto this scope
- visiting page now will error for
EhloWeb.EhloController.init/1 is undefined
- let's add controller as ehlo_controller.exwith
useraction defined
- visiting page now error for
EhloWeb.EhloView.render/2 is undefined
- adding user.html.eex
now
localhost:4000/ehloshall display new page;*.eexfiles here are templates
- add dynamix data from URL to Routes like
get "/ehlo/:name", EhloController, :user, to pass a username which will be available asdef user(conn, %{"name" => name})..in Controller which will pass it torender
use pat-match function def for
userfor/ehlo/:nameabove/ehloexternal parameter key is a string
"name"not atom as a convention for Phoenix to not trust conversion for external datathen adding an if condition to
user.html.eexchecking ifassignsMap passed with parameters to Template has a keynameor not to pick required value does the page render with user name at/ehloand/ehlo/alicecorrectly
<%= %>surround Elixir code blocks in EEx; Assigns enable parameter passed available
- Phoenix encourages breaking bigger functions into smaller ones; then provide a place to explicitly register each cog in a way easy to understand and replace
all these cogs tied together with
Pluglibrary
Pluglib act as a spec for web apps; each Plug consumes and produce a common data-structPlug.Connrepresenting entirety for a request... Phoenix just calling one Plug after another
Response is just one more action on
connectionPlugs are functions and Web-apps are pipelines of Plugs
- phoenix file structure
config goes at
./ehlo/config
./ehlo/assetsget JS and CSS
./ehlo/lib/ehlogets supervision trees, long-running processes, app business logicweb related code for routes, controllers, views (templates), channels go to
./ehlo/lib/ehlo_webmigrations go to
./ehlo/priv/repoand some boilerplate test setup is at./ehlo/testconfig structure and env relation is similar to any other Mix project
EhloWeb.Endpoint(a Plug made of multiple other Plugs) are the chain of functions at the beginning of each request
a Connection begins with an Endpoint, finishes at Controller
- Phoenix doesn't limit Endpoint count
so an app can have main app Endpoint running at Port
80,443and admin Endpoint running at a special port like4443we can also split these Endpoints into separate applications still run side-by-side (using Umbrella Projects)
- Router by default has 2 pipelines, Browser for HTML and API for JSON. For matched scope it calls Pipeline mentioned and that passes to Controller