Controllers

Controllers are the heart of glim framework. They are responsible for request handling, input validation and return appropriate responses to the requests. When a new glim app is created using $ glim new, app/controllers.py will be automatically generated to be used. In glim, all the controllers reside in controllers.py. The simplest controller definition would be the following;

# controllers.py
from glim import Controller
from glim import Response

class BaseController(Controller):
    def hello(self):
        return {'error': None, 'msg': 'Hello World'}
      	# OR
        # return 'Hello World Mate!'

As it might be noticed glim controllers can detect that the response should be a json response using application/json header. A raw string return would not perform any header set.
In this particular example, hello function can be called as an "action" from an mvc perspective. Notice that string based responses are also automatically handled by glim. The hello function can be mapped to a url in routes.py.

#routes.py
urls = {
    '/': 'BaseController.hello'
}

In this example, when you navigate to http://127.0.0.1:8080 in your browser, you will notice the Response string being returned.

Controllers are automatically instantiated by glim dispatcher. Dispatcher automatically generates an instance injected to request object so that you don't need to define any functions in the static scope. There exists further information about requests and response in the next section.