controller.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. Controllers
  2. ===========
  3. Controllers live in ``/lib/gluon/web/controller``. They define which pages ("routes")
  4. exist under the ``/cgi-bin/gluon`` path, and what code is run when these pages are requested.
  5. Controller scripts usually start with a *package* declaration, followed by calls
  6. to the *entry* function, which each define one route:
  7. .. code-block:: lua
  8. package 'gluon-web-admin'
  9. entry({"admin"}, alias("admin", "info"), _("Advanced settings"), 10)
  10. entry({"admin", "info"}, template("admin/info"), _("Information"), 1)
  11. *package* defines the translation namespace for the titles of the defined
  12. pages as well as the referenced views and models. The entry function expects 4
  13. arguments:
  14. - `path`: Components of the path to define a route for.
  15. The above example defines routes for the paths ``admin`` and ``admin/info``.
  16. - `target`: Dispatcher for the route. See the following section for details.
  17. - `title`: Page title (also used in navigation). The underscore function is used
  18. to mark the strings as translatable for ``i18n-scan.pl``.
  19. - `order`: Sort index in navigation (defaults to 100)
  20. Navigation indexes are automatically generated for each path level. Pages can be
  21. hidden from the navigation by setting the `hidden` property of the node object
  22. returned by `entry`:
  23. .. code-block:: lua
  24. entry({"hidden"}, alias("foo"), _("I'm hidden!")).hidden = true
  25. Dispatchers
  26. -----------
  27. - *alias* (*path*, ...): Redirects to a different page. The path components are
  28. passed as individual arguments.
  29. - *call* (*func*, ...): Runs a Lua function for custom request handling. The given
  30. function is called with the HTTP object and the template renderer as first
  31. two arguments, followed by all additional arguments passed to `call`.
  32. - *template* (*view*): Renders the given view. See :doc:`view`.
  33. - *model* (*name*): Displays and evaluates a form as defined by the given model. See the
  34. :doc:`model` page for an explanation of gluon-web models.
  35. .. _web-controller-http:
  36. The HTTP object
  37. ---------------
  38. The HTTP object provides information about the HTTP requests and allows to add
  39. data to the reply. Using it directly is rarely necessary when gluon-web
  40. models and views are used.
  41. Useful functions:
  42. - *getenv* (*key*): Returns a value from the CGI environment passed by the webserver.
  43. - *formvalue* (*key*): Returns a value passed in a query string or in the content
  44. of a POST request. If multiple values with the same name have been passed, only
  45. the first is returned.
  46. - *formvaluetable* (*key*): Similar to *formvalue*, but returns a table of all
  47. values for the given key.
  48. - *status* (*code*, *message*): Writes the HTTP status to the reply. Has no effect
  49. if a status has already been sent or non-header data has been written.
  50. - *header* (*key*, *value*): Adds an HTTP header to the reply to be sent to to
  51. the client. Has no effect when non-header data has already been written.
  52. - *prepare_content* (*mime*): Sets the *Content-Type* header to the given MIME
  53. type, potentially setting additional headers or modifying the MIME type to
  54. accommodate browser quirks
  55. - *write* (*data*, ...): Sends the given data to the client. If headers have not
  56. been sent, it will be done before the data is written.
  57. HTTP functions are called in method syntax, for example:
  58. .. code-block:: lua
  59. http:write('Output!')
  60. .. _web-controller-template-renderer:
  61. The template renderer
  62. ---------------------
  63. The template renderer allows to render templates (views). The most useful functions
  64. are:
  65. - *render* (*view*, *scope*, *pkg*): Renders the given view, optionally passing a table
  66. with additional variables to make available in the template. The passed package
  67. defines the translation namespace.
  68. - *render_string* (*str*, *scope*, *pkg*): Same as *render*, but the template is passed
  69. directly instead of being loaded from the view directory.
  70. The renderer functions are called in property syntax, for example:
  71. .. code-block:: lua
  72. renderer.render('layout')
  73. Differences from LuCI
  74. ---------------------
  75. - Controllers must not use the *module* function to define a Lua module (*gluon-web*
  76. will set up a proper environment for each controller itself)
  77. - Entries are defined at top level, not inside an *index* function
  78. - The *alias* dispatcher triggers an HTTP redirect instead of directly running
  79. the dispatcher of the aliased route.
  80. - The *call* dispatcher is passed a function instead of a string with a function
  81. name.
  82. - The *cbi* dispatcher of LuCI has been renamed to *model*.
  83. - The HTTP POST handler support the multipart/form-data encoding only, so
  84. ``enctype="multipart/form-data"`` must be included in all *<form>* HTML
  85. elements.
  86. - Other dispatchers like *form* are not provided.