Sugar.Router
Sugar.Router
defines an alternate format for Plug.Router
routing. Supports all HTTP methods that Plug.Router
supports.
Routes are defined with the form:
method route [guard], controller, action
method
is get
, post
, put
, patch
, or delete
, each
responsible for a single HTTP method. method
can also be any
, which will
match on all HTTP methods. options
is yet another option for method
, but
when using options
, only a route path and the methods that route path
supports are needed. controller
is any valid Elixir module name, and
action
is any valid public function defined in the controller
module.
get/3
, post/3
, put/3
, patch/3
, delete/3
, options/2
, and any/3
are already built-in as described. resource/2
exists but will need
modifications to create everything as noted.
raw/4
allows for using custom HTTP methods, allowing your application to be
HTTP spec compliant.
Example
defmodule Router do
use Sugar.Router
alias Controllers, as: C
# Define your routes here
get "/", C.Pages, :index
get "/pages", C.Pages, :create
post "/pages", C.Pages, :create
put "/pages/:page_id" when id == 1,
C.Pages, :update_only_one
get "/pages/:page_id", C.Pages, :show
# Auto-create a full set of routes for resources
#
resource :users, C.User, arg: :user_id
#
# Generates:
#
# get "/users", C.User, :index
# post "/users", C.User, :create
# get "/users/:user_id", C.User, :show
# put "/users/:user_id", C.User, :update
# patch "/users/:user_id", C.User, :patch
# delete "/users/:user_id", C.User, :delete
#
# options "/users", "HEAD,GET,POST"
# options "/users/:_user_id", "HEAD,GET,PUT,PATCH,DELETE"
raw :trace, "/trace", C.Tracer, :trace
end
Summary↑
any(route, controller, action) | Macro for defining |
delete(route, controller, action) | Macro for defining |
get(route, controller, action) | Macro for defining |
options(route, allows) | Macro for defining |
patch(route, controller, action) | Macro for defining |
post(route, controller, action) | Macro for defining |
put(route, controller, action) | Macro for defining |
raw(method, route, controller, action) | Macro for defining routes for custom HTTP methods |
resource(resource, controller, opts \\ []) | Creates RESTful resource endpoints for a route/controller combination |
Macros
Macro for defining ANY
routes.
Arguments
route
-String|List
controller
-Atom
action
-Atom
Macro for defining DELETE
routes.
Arguments
route
-String|List
controller
-Atom
action
-Atom
Macro for defining GET
routes.
Arguments
route
-String|List
controller
-Atom
action
-Atom
Macro for defining OPTIONS
routes.
Arguments
route
-String|List
allows
-String
Macro for defining PATCH
routes.
Arguments
route
-String|List
controller
-Atom
action
-Atom
Macro for defining POST
routes.
Arguments
route
-String|List
controller
-Atom
action
-Atom
Macro for defining PUT
routes.
Arguments
route
-String|List
controller
-Atom
action
-Atom
Macro for defining routes for custom HTTP methods.
Arguments
method
-Atom
route
-String|List
controller
-Atom
action
-Atom
Creates RESTful resource endpoints for a route/controller combination.
Example
resource :users, Handlers.User
expands to
get, "/users", Handlers.User, :index
post, "/users", Handlers.User, :create
get, "/users/:id", Handlers.User, :show
put, "/users/:id", Handlers.User, :update
patch, "/users/:id", Handlers.User, :patch
delete, "/users/:id", Handlers.User, :delete
options, "/users", "HEAD,GET,POST"
options, "/users/:_id", "HEAD,GET,PUT,PATCH,DELETE"