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: stringNow to generate json
mix phx.gen.json Accounts User users email:string hashed_password: string --web Api --no-context --no-schemaHere 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- [https://www.kickinespresso.com/posts/generating-html-and-json-scaffold-with-phoenix-1-3-1-3-2-generorators](scaffold html and json using phoenix generators via cli)