Plug 0.10.0 moves CSRF tokens from cookies back to sessions. To avoid future bumps on the road, a get_csrf_token/0
function has been added to controllers and imported into views. Update all your csrf token reference code to use the new function. Additionally, form_tag
and link
helpers have been added that will inject the csrf token for you automatically. You should transition to these new functions where possible, ie:
<%= form_tag("/hello", method: :post) %>
... your form stuff. csrf is inject for you
</form>
Additionally, a link
helper has been added that can send POST/PUT/DELETE requests with csrf inclusion, ie:
<%= link "Remove", to: user_path(@conn, :delete, @user), method: :delete) %>
web/view.ex
has been removed, as well as the using
macro that allowed you to inject shared aliases/imports across other views. It has been replace by a web/web.ex
file that serves as a centralized place to group model, view, and controller shared code injection blocks.
- Save the following block of code as
web/web.ex
(replaceMyApp
with your application module), and copy yourweb/view.ex
contents as indicated in the block below. After the new file is saved, delete yourweb/view.ex
file.
defmodule MyApp.Web do
@moduledoc """
A module that keeps using definitions for controllers,
views and so on.
This can be used in your application as:
use MyApp.Web, :controller
use MyApp.Web, :view
Keep the definitions in this module short and clean,
mostly focused on imports, uses and aliases.
"""
def view do
quote do
use Phoenix.View, root: "web/templates"
# Import URL helpers from the router
import MyApp.Router.Helpers
# Import all HTML functions (forms, tags, etc)
use Phoenix.HTML
# *****
# Copy your old `web/view.ex` using block contents here
# *****
end
end
def controller do
quote do
use Phoenix.Controller
# Import URL helpers from the router
import MyApp.Router.Helpers
end
end
def model do
quote do
end
end
@doc """
When used, dispatch to the appropriate controller/view/etc.
"""
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
end
- Update all your views to use the new
Web
module, ie:
# 0.9.x
defmodule MyApp.PageView do
use MyApp.View
end
# 0.10.0
defmodule MyApp.PageView do
use MyApp.Web, :view
end
- Update all your controller use declrations, ie:
# 0.9.x
defmodule MyApp.UserController do
use Phoenix.Controller
end
# 0.10.0
defmodule MyApp.UserController do
use MyApp.Web, :controller
end
- The
:options
key has been removed from the:pubsub
keyword list. Just define the options alongside the pubsub configuration. - The :name option when configuring a pubsub adapter is now required instead of inflected
# 0.9.x
config :my_app, MyApp.Endpoint,
pubsub: [adapter: Phoenix.PubSub.PG2]
# 0.10.0
config :my_app, MyApp.Endpoint,
pubsub: [name: MyApp.PubSub,
adapter: Phoenix.PubSub.PG2]
The :eredis
based adapter has been replaced with :redo
for propper remote connections. Update your configuration and mix deps like so:
# config.exs
# 0.9.x
config :my_app, MyApp.Endpoint,
pubsub: [adapter: Phoenix.PubSub.Redis]
# 0.10.0
config :my_app, MyApp.Endpoint,
pubsub: [name: MyApp.PubSub,
adapter: Phoenix.PubSub.Redis,
host: "127.0.0.1",
...]
# mix.exs
# 0.9.x
defp deps do
[{:eredis, github: ...
{:poolboy, "~> 1.4.2"},
...]
end
# 0.10.0
defp deps do
[{:redo, github: "heroku/redo"},
{:poolboy, "~> 1.4.2"},
...]
end
Phoenix 0.10.0 includes brunch for static asset compilation. Brunch is a nodejs project, so node is required. See node's website for installation instructions. Use the following steps to get your existing application setup:
- Copy
package.json
to the root of your project https://raw.githubusercontent.com/phoenixframework/phoenix/39aa3e69870ebdd90d82774c933a8b117a8daaba/priv/static/brunch/package.json - Copy
brunch-config.js
to the root of your project https://raw.githubusercontent.com/phoenixframework/phoenix/39aa3e69870ebdd90d82774c933a8b117a8daaba/priv/static/brunch/brunch-config.js - Ensure node is installed
project_root$ npm install
project_root$ mkdir -p web/static/js
project_root$ mkdir -p web/static/css
project_root$ mkdir -p web/static/vendor
project_root$ touch web/static/js/app.js
project_root$ touch web/static/css/app.scss
- copy
phoenix.js
toweb/static/vendor/phoenix.js
https://raw.githubusercontent.com/phoenixframework/phoenix/39aa3e69870ebdd90d82774c933a8b117a8daaba/priv/static/phoenix.js - Move/copy your application javascript and app css/sass to the former files
- Update your
web/templates/layouts/application.html.eex
(and others if exist) to use the new brunch built css/js, ie:<script src="<%= static_path(@conn, "/js/app.js") %>"></script>
<link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>">
- Add this to load your main js module in your layout(s) just before
</body>
:<script>require("web/static/js/app")</script>
- Add the following Endpoint
:watchers
and:live_reload
configuration toconfig/dev.exs
:
config :my_app, MyAPp.Endpoint,
...
watchers: [{Path.expand("node_modules/brunch/bin/brunch"), ["watch"]}]
# Watch static and templates for browser reloading.
# *Note*: Be careful with wildcards. Larger projects
# will use higher CPU in dev as the number of files
# grow. Adjust as necessary.
config :my_app, MyApp.Endpoint,
live_reload: [Path.expand("priv/static/js/app.js"),
Path.expand("priv/static/css/app.css"),
Path.expand("web/templates/**/*.eex")]
Now start your server with mix phoenix.server
and observe brunch compiling your assets in web/static/css
and web/static/js
and live-reloading the browser on changes.
If you are using Ecto, add phoenix_ecto
to your mix deps to enjoy Ecto form/link builder support:
defp deps do
[...,
{:phoenix, "~> 0.10.0"},
{:phoenix_ecto, "~> 0.1"},
{:postgrex, ">= 0.0.0"}]
end
Happy coding!
Great stuff, thanks, the upgrade worked like a charm!
However, now I'm running in a bit of an issue with an umbrella app which includes one Phoenix app.
where the
web_ui
app is the Phoenix app and is using thepocketex
app.mix phoenix.server
from the umbrella app to makepocketex
available toweb_ui
[error] Could not start watcher "/Users/adrian/Dropbox/Projects/essenciary/essenciary/node_modules/brunch/bin/brunch", executable does not exist
Please advise, thanks!