Resource routing and REST behaviour for Phoenix web applications.
PhoenixRest integrates the
PlugRest library into
your Phoenix application by making a new resource
macro available in
the existing router.
Instead of writing a Phoenix Controller
and implementing an action
function for each HTTP verb and path, use this library to create a
Resource
and define one or more optional callbacks that describe
your resource's RESTful behavior.
Documentation for PhoenixRest is available on hexdocs.
Source code is available on Github.
Package is available on hex.
Let's assume you've created a new Phoenix site with mix phx.new hello
.
Add a new route to your router.ex
to match a path with a resource handler:
defmodule HelloWeb.Router do
use HelloWeb, :router
resource "/hello", HelloWeb.HelloResource
end
Create a resource at lib/hello_web/resources/hello_resource.ex
defining the
resource handler, and implement the optional callbacks:
defmodule HelloWeb.HelloResource do
use PhoenixRest.Resource
def to_html(conn, state) do
{"Hello world!", conn, state}
end
end
The docs
for PhoenixRest.Resource
list all of the supported REST callbacks
and their default values.
Add PhoenixRest to your Phoenix project:
- Add
:phoenix_rest
to your list of dependencies inmix.exs
:
```elixir
defp deps do
[
{:phoenix_rest, "~> 0.7"}
]
end
```
- Edit
hello_web.ex
and addPhoenixRest.Router
:
```elixir
def router do
quote do
use Phoenix.Router
use PhoenixRest.Router
import Plug.Conn
import Phoenix.Controller
end
end
```
You can generate a new PhoenixRest resource (with all of the callbacks implemented) by using a Mix task:
mix phx_rest.gen.resource HelloResource
PhoenixRest is still in an initial development phase. Expect breaking changes at least in each minor version.
See the CHANGELOG for more information.
PhoenixRest copyright © 2016, Christopher Adams