"use strict";(self.webpackChunkgoffee_site=self.webpackChunkgoffee_site||[]).push([[4559],{7911:(e,n,o)=>{o.r(n),o.d(n,{assets:()=>i,contentTitle:()=>c,default:()=>h,frontMatter:()=>s,metadata:()=>l,toc:()=>a});var t=o(4848),r=o(8453);const s={title:"Controllers"},c=void 0,l={id:"controllers",title:"Controllers",description:"A Controller is nothing but a function to handle the HTTP requests to the matching route.",source:"@site/docs/controllers.md",sourceDirName:".",slug:"/controllers",permalink:"/goffee/docs/controllers",draft:!1,unlisted:!1,tags:[],version:"current",frontMatter:{title:"Controllers"},sidebar:"docsSidebar",previous:{title:"Routing",permalink:"/goffee/docs/routing"},next:{title:"Context",permalink:"/goffee/docs/context"}},i={},a=[{value:"Organizing controllers",id:"organizing-controllers",level:3}];function d(e){const n={code:"code",h3:"h3",p:"p",pre:"pre",...(0,r.R)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsx)(n.p,{children:"A Controller is nothing but a function to handle the HTTP requests to the matching route."}),"\n",(0,t.jsx)(n.p,{children:"Here is an example for a controller function to list users"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-go",metastring:'title="#file: controllers/users.go"',children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n)\n\nfunc ListUsers(c *core.Context) *core.Response {\n    //The logic for listing users\n}\n'})}),"\n",(0,t.jsx)(n.h3,{id:"organizing-controllers",children:"Organizing controllers"}),"\n",(0,t.jsxs)(n.p,{children:["The best way to organize controllers is to think of them like ",(0,t.jsx)(n.code,{children:"controller's actions"})," in ",(0,t.jsx)(n.code,{children:"MVC"}),".\nYou can simply group all controllers of the same resource in one file and prefix the name of the controller with the name of the resource like: ",(0,t.jsx)(n.code,{children:"CreateUsers"}),", ",(0,t.jsx)(n.code,{children:"ListUsers"})," and so on.\nhere is an example:\nImaging we are creating an app for handling ",(0,t.jsx)(n.code,{children:"todos"}),", in this app we have ",(0,t.jsx)(n.code,{children:"users"})," and ",(0,t.jsx)(n.code,{children:"todos"})," here is how we can organize the controllers of both the ",(0,t.jsx)(n.code,{children:"users"})," and the ",(0,t.jsx)(n.code,{children:"todos"})]}),"\n",(0,t.jsxs)(n.p,{children:["1- The controllers of ",(0,t.jsx)(n.code,{children:"users"})," related requests goes to the file ",(0,t.jsx)(n.code,{children:"controllers/users.go"})]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-go",metastring:'title="#file: controllers/users.go"',children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n)\n\nfunc CreateUsers(c *core.Context) *core.Response {\n    // the logic here...\n}\n\nfunc ListUsers(c *core.Context) *core.Response {\n    // the logic here...\n}\n\nfunc DeleteUsers(c *core.Context) *core.Response {\n    // the logic here...\n}\n\n'})}),"\n",(0,t.jsxs)(n.p,{children:["1- The controllers of ",(0,t.jsx)(n.code,{children:"todos"})," related requests goes to the file ",(0,t.jsx)(n.code,{children:"controllers/todos.go"})]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-go",children:'package controllers\n\nimport (\n    "git.smarteching.com/goffee/core"\n)\n\nfunc CreateTodos(c *core.Context) *core.Response {\n    // the logic here...\n}\n\nfunc ListTodos(c *core.Context) *core.Response {\n    // the logic here...\n}\n\nfunc DeleteTodos(c *core.Context) *core.Response {\n    // the logic here...\n}\n'})})]})}function h(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(d,{...e})}):d(e)}},8453:(e,n,o)=>{o.d(n,{R:()=>c,x:()=>l});var t=o(6540);const r={},s=t.createContext(r);function c(e){const n=t.useContext(s);return t.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function l(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:c(e.components),t.createElement(s.Provider,{value:n},e.children)}}}]);