Skip to content

Latest commit

 

History

History
51 lines (39 loc) · 1.76 KB

controllers.md

File metadata and controls

51 lines (39 loc) · 1.76 KB

Controllers

Controllers are the bridge from the outside world to the app. They define handlers that listen to requested URLs, perform logic, and return a response. Typically a handler upon receiving a request will do the following:

  1. Retrieve entities (instances of a model stored in the data source)
  2. Invoke their logic (application logic should be encapsulated in the model files)
  3. Process a template using the retrieved entities
  4. Return the result of that template having been processed as the response
import appier

import models

class CatController(appier.Controller):

    @appier.route("/cats", "GET")
    def list(self):
        cats = models.Cat.find()
        for cat in cats: cat.meow()
        return self.template(
            "cat/list.html.tpl",
            cats = cats
        )

Controller responses can be a redirect, instead of an output:

@appier.route("/cats/<int:id>", "GET")
def show(self, id):
    return self.redirect(
        self.url_for("animal.show", id = id)
    )

There's not much else to say about controllers, other than stating the best practice of avoiding writing application logic in controllers. Controllers are only meant for invoking methods and returning results. They are meant to knit together existing logic in a meaningful way and make it available to the outside world; logic belongs to Models.

For further details on how to create routes to make controller methods accessible to the outside world read the Requests documentation.

Access control

The controller handlers can be protected so that they can be accessed only by authenticated users or only authenticated users that have certain access rights. To learn more, read the Access Control documentation.