controller.rst 4.4 KB

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