{"_id":"553d394e9660ba0d00569d17","category":{"_id":"553d394d9660ba0d00569d08","pages":["553d394e9660ba0d00569d13","553d394e9660ba0d00569d14","553d394e9660ba0d00569d15","553d394e9660ba0d00569d16","553d394e9660ba0d00569d17","553d394e9660ba0d00569d18","553d394e9660ba0d00569d19","553d394e9660ba0d00569d1a","553d394e9660ba0d00569d1b","553d394e9660ba0d00569d1c","553d394e9660ba0d00569d1d","553d394e9660ba0d00569d1e","553d394e9660ba0d00569d1f","553d394e9660ba0d00569d20","553d394e9660ba0d00569d21","553d394e9660ba0d00569d22","553d394e9660ba0d00569d23"],"project":"543467200ef9c00800164ecc","version":"553d394d9660ba0d00569d07","__v":1,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-15T18:00:23.862Z","from_sync":false,"order":0,"slug":"glim","title":"GLIM"},"project":"543467200ef9c00800164ecc","user":"543466ea0e8e2b0e00341818","version":{"_id":"553d394d9660ba0d00569d07","project":"543467200ef9c00800164ecc","forked_from":"54bedb2fffb79c320030db1f","__v":1,"createdAt":"2015-04-26T19:15:25.245Z","releaseDate":"2015-04-26T19:15:25.245Z","categories":["553d394d9660ba0d00569d08","553d394d9660ba0d00569d09"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"0.11.2","version":"0.11.2"},"githubsync":"","__v":8,"metadata":{"title":"","description":"","image":[]},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-15T18:05:29.317Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"never","params":[],"url":""},"isReference":false,"order":4,"body":"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;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# controllers.py\\nfrom glim import Controller\\nfrom glim import Response\\n\\nclass BaseController(Controller):\\n    def hello(self):\\n        return {'error': None, 'msg': 'Hello World'}\\n      \\t# OR\\n        # return 'Hello World Mate!'\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nAs 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.\nIn 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`.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"#routes.py\\nurls = {\\n    '/': 'BaseController.hello'\\n}\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nIn this example, when you navigate to [http://127.0.0.1:8080](http://127.0.0.1:8080) in your browser, you will notice the Response string being returned.\n\nControllers 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](doc:request--response) section.","excerpt":"","slug":"controllers","type":"basic","title":"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; [block:code] { "codes": [ { "code": "# controllers.py\nfrom glim import Controller\nfrom glim import Response\n\nclass BaseController(Controller):\n def hello(self):\n return {'error': None, 'msg': 'Hello World'}\n \t# OR\n # return 'Hello World Mate!'", "language": "python" } ] } [/block] 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`. [block:code] { "codes": [ { "code": "#routes.py\nurls = {\n '/': 'BaseController.hello'\n}", "language": "python" } ] } [/block] In this example, when you navigate to [http://127.0.0.1:8080](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](doc:request--response) section.