strapi/docs/.vuepress/dist/assets/js/41.5ef681df.js
2018-10-05 17:24:17 +02:00

1 line
6.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(window.webpackJsonp=window.webpackJsonp||[]).push([[41],{198:function(t,s,e){"use strict";e.r(s);var n=e(0),a=Object(n.a)({},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"content"},[t._m(0),t._v(" "),e("p",[t._v("See the "),e("router-link",{attrs:{to:"./../concepts/concepts.html#controllers"}},[t._v("controllers' concepts")]),t._v(" for details.")],1),t._v(" "),t._m(1),t._v(" "),e("p",[t._v("There are two ways to create a controller:")]),t._v(" "),e("ul",[e("li",[t._v("Using the CLI "),e("code",[t._v("strapi generate:controller user")]),t._v(". Read the "),e("router-link",{attrs:{to:"./../cli/CLI.html#strapi-generatecontroller"}},[t._v("CLI documentation")]),t._v(" for more information.")],1),t._v(" "),t._m(2)]),t._v(" "),t._m(3),t._v(" "),e("p",[t._v("Each controllers action must be an "),e("code",[t._v("async")]),t._v(" function and receives the "),e("code",[t._v("context")]),t._v(" ("),e("code",[t._v("ctx")]),t._v(") object as first parameter containing the "),e("router-link",{attrs:{to:"./../guides/requests.html"}},[t._v("request context")]),t._v(" and the "),e("router-link",{attrs:{to:"./../guides/responses.html"}},[t._v("response context")]),t._v(". The action has to be bounded by a route.")],1),t._v(" "),t._m(4),t._v(" "),t._m(5),t._v(" "),t._m(6),t._v(" "),t._m(7),t._m(8),t._v(" "),t._m(9),t._m(10)])},[function(){var t=this.$createElement,s=this._self._c||t;return s("h1",{attrs:{id:"controllers"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#controllers","aria-hidden":"true"}},[this._v("#")]),this._v(" Controllers")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"how-to-create-a-controller"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#how-to-create-a-controller","aria-hidden":"true"}},[this._v("#")]),this._v(" How to create a controller?")])},function(){var t=this.$createElement,s=this._self._c||t;return s("li",[this._v("Manually create a JavaScript file named "),s("code",[this._v("User.js")]),this._v(" in "),s("code",[this._v("./api/**/controllers")]),this._v(" which contains at least one "),s("a",{attrs:{href:"#adding-endpoints"}},[this._v("endpoint")]),this._v(".")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h2",{attrs:{id:"adding-endpoints"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#adding-endpoints","aria-hidden":"true"}},[this._v("#")]),this._v(" Adding Endpoints")])},function(){var t=this.$createElement,s=this._self._c||t;return s("h4",{attrs:{id:"example"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#example","aria-hidden":"true"}},[this._v("#")]),this._v(" Example")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("p",[t._v("In this example, we are defining a specific route in "),e("code",[t._v("./api/hello/config/routes.json")]),t._v(" that takes "),e("code",[t._v("Hello.index")]),t._v(" as handler. It means that every time a web browser is pointed to the "),e("code",[t._v("/hello")]),t._v(" URL, the server will called the "),e("code",[t._v("index")]),t._v(" action in the "),e("code",[t._v("Hello.js")]),t._v(" controller. Our "),e("code",[t._v("index")]),t._v(" action will return "),e("code",[t._v("Hello World!")]),t._v(". You can also return a JSON object.")])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[s("strong",[this._v("Path —")]),this._v(" "),s("code",[this._v("./api/hello/config/routes.json")]),this._v(".")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-json extra-class"},[e("pre",{pre:!0,attrs:{class:"language-json"}},[e("code",[e("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{attrs:{class:"token property"}},[t._v('"routes"')]),e("span",{attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),e("span",{attrs:{class:"token punctuation"}},[t._v("[")]),t._v("\n "),e("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{attrs:{class:"token property"}},[t._v('"method"')]),e("span",{attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),e("span",{attrs:{class:"token string"}},[t._v('"GET"')]),e("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{attrs:{class:"token property"}},[t._v('"path"')]),e("span",{attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),e("span",{attrs:{class:"token string"}},[t._v('"/hello"')]),e("span",{attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{attrs:{class:"token property"}},[t._v('"handler"')]),e("span",{attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),e("span",{attrs:{class:"token string"}},[t._v('"Hello.index"')]),t._v("\n "),e("span",{attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n "),e("span",{attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n"),e("span",{attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])},function(){var t=this.$createElement,s=this._self._c||t;return s("p",[s("strong",[this._v("Path —")]),this._v(" "),s("code",[this._v("./api/hello/controllers/Hello.js")]),this._v(".")])},function(){var t=this,s=t.$createElement,e=t._self._c||s;return e("div",{staticClass:"language-js extra-class"},[e("pre",{pre:!0,attrs:{class:"language-js"}},[e("code",[t._v("module"),e("span",{attrs:{class:"token punctuation"}},[t._v(".")]),t._v("exports "),e("span",{attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{attrs:{class:"token comment"}},[t._v("// GET /hello")]),t._v("\n index"),e("span",{attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{attrs:{class:"token keyword"}},[t._v("async")]),t._v(" "),e("span",{attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx"),e("span",{attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{attrs:{class:"token operator"}},[t._v("=>")]),t._v(" "),e("span",{attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n ctx"),e("span",{attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{attrs:{class:"token function"}},[t._v("send")]),e("span",{attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{attrs:{class:"token string"}},[t._v("'Hello World!'")]),e("span",{attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),e("span",{attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n")])])])},function(){var t=this.$createElement,s=this._self._c||t;return s("div",{staticClass:"note custom-block"},[s("p",[this._v("A route handler can only access the controllers defined in the "),s("code",[this._v("./api/**/controllers")]),this._v(" folders.")])])}],!1,null,null,null);a.options.__file="controllers.md";s.default=a.exports}}]);