links: Phoenix MOC


Generate Api along with html

If you have generated html using phx.gen and want to add api also, you can do something like this

To generate html

mix phx.gen.html Accounts User users email:string hashed_password: string

Now to generate json

mix phx.gen.json Accounts User users email:string hashed_password: string --web Api --no-context --no-schema

Here we are creating new namespace called Api using —web flag and we are skipping context and schema creation, as they are already created during html generation

The last missing piece was to route Api. open routex.ex

Add Context as YourAppName.Api and process as api

scope "/api", KancheWeb.Api, as: :api do
	pipe_through :api
	resources "/users", UserController
end

tags: cli, generator source: