{"_id":"553d394e9660ba0d00569d16","user":"543466ea0e8e2b0e00341818","project":"543467200ef9c00800164ecc","githubsync":"","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"},"__v":19,"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"},"metadata":{"title":"","description":"","image":[]},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-15T18:01:32.580Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"never","params":[],"url":""},"isReference":false,"order":3,"body":"Glim has an easy to use powerful routing feature where developers can define routes in seconds. It uses werkzeug's great routing features and exposes all of them. The route definitions reside in `app/routes.py` file. The simplest route example would be the following;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# routes.py\\nurls = {\\n    '/': 'BaseController.hello'\\n}\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nThe first route definition simply means that `BaseController`'s `hello()` function is mapped to the root route ( / ). Although most of the popular web frameworks allow automatic detection of controller, action, glim doesn't allow for that because of security reasons.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"RESTful Routing\"\n}\n[/block]\nThere are two ways of RESTful routing in glim. The first one is to define Controllers as resources.\nConsider the following route definition;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# routes.py\\nurls = {\\n    '/users': 'UserController'\\n}\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\nThe second route definition is a typical example of a `RESTful` routing in glim. This means that the following http methods are automatically mapped to the functions;\n[block:parameters]\n{\n  \"data\": {\n    \"0-0\": \"/users\",\n    \"h-0\": \"URL\",\n    \"h-1\": \"HTTP Method\",\n    \"h-2\": \"Function\",\n    \"0-1\": \"POST\",\n    \"0-2\": \"UserController.post()\",\n    \"1-0\": \"/users\",\n    \"1-1\": \"PUT\",\n    \"1-2\": \"UserController.put()\",\n    \"2-0\": \"/users\",\n    \"2-1\": \"GET\",\n    \"2-2\": \"UserController.get()\",\n    \"3-0\": \"/users\",\n    \"3-1\": \"DELETE\",\n    \"3-2\": \"UserController.delete()\",\n    \"4-0\": \"/users\",\n    \"4-1\": \"PATCH\",\n    \"4-2\": \"UserController.patch()\"\n  },\n  \"cols\": 3,\n  \"rows\": 5\n}\n[/block]\nMoreover, glim has an option to define routes with single verbs. For instance;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# routes.py\\nurls = {\\n\\t'POST /authentication': 'UserController.login'\\n}\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nThis route will match /authentication endpoint with only POST method to UserController's login function\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"More examples on routing\"\n}\n[/block]\nGlim has features to validate variables inside the route level as werkzeug provides. A more complex route definition example would be the following;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# routes.py\\nurls = {\\n    '/feeds/<slug>' : 'FeedController.get'\\n}\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nIn this example, this route definition will be mapped into `FeedController`‘s `get(slug)` function.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"A GET | POST to /feeds route without <slug> variable will result in HTTP 404 - Not found response.\"\n}\n[/block]\nYou can also define types of variables in route level. The following example will also match types;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# routes.py\\nurls = {\\n    '/post/<int:year>' : 'PostController.get'\\n}\\n\\n# is mapping to\\n\\n# app/controllers.py\\nclass PostController(object):\\n\\t\\tdef get(self, year):\\n      # do stuff\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\n In this example, the route will only be mapped for integer typed year variables. The route will be mapped into `PostController`‘s `get()` function with year only being an integer.\n\nThe routing system also allows defining the length of variables. For example;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# routes.py\\nurls = {\\n    '/index/<string(length=2):lang_code>' : 'HomeController.index'\\n}\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nIn this definition, the route will be mapped `HomeController`‘s `index(lang_code)` function with `lang_code` being only strings with length of 2.\n\nFor mapping any of the routes, `<any>` keyword can be used in defining generic routes like the following;\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"# routes.py\\nurls = {\\n    '/<any(about, help, imprint):page' : 'PageController.resolve'\\n}\",\n      \"language\": \"python\"\n    }\n  ]\n}\n[/block]\nIn this definition, the route will be mapped into PageController‘s resolve(page) function with page variable being `about`, `help` or `imprint`.","excerpt":"","slug":"routing","type":"basic","title":"Routing"}
Glim has an easy to use powerful routing feature where developers can define routes in seconds. It uses werkzeug's great routing features and exposes all of them. The route definitions reside in `app/routes.py` file. The simplest route example would be the following; [block:code] { "codes": [ { "code": "# routes.py\nurls = {\n '/': 'BaseController.hello'\n}", "language": "python" } ] } [/block] The first route definition simply means that `BaseController`'s `hello()` function is mapped to the root route ( / ). Although most of the popular web frameworks allow automatic detection of controller, action, glim doesn't allow for that because of security reasons. [block:api-header] { "type": "basic", "title": "RESTful Routing" } [/block] There are two ways of RESTful routing in glim. The first one is to define Controllers as resources. Consider the following route definition; [block:code] { "codes": [ { "code": "# routes.py\nurls = {\n '/users': 'UserController'\n}", "language": "text" } ] } [/block] The second route definition is a typical example of a `RESTful` routing in glim. This means that the following http methods are automatically mapped to the functions; [block:parameters] { "data": { "0-0": "/users", "h-0": "URL", "h-1": "HTTP Method", "h-2": "Function", "0-1": "POST", "0-2": "UserController.post()", "1-0": "/users", "1-1": "PUT", "1-2": "UserController.put()", "2-0": "/users", "2-1": "GET", "2-2": "UserController.get()", "3-0": "/users", "3-1": "DELETE", "3-2": "UserController.delete()", "4-0": "/users", "4-1": "PATCH", "4-2": "UserController.patch()" }, "cols": 3, "rows": 5 } [/block] Moreover, glim has an option to define routes with single verbs. For instance; [block:code] { "codes": [ { "code": "# routes.py\nurls = {\n\t'POST /authentication': 'UserController.login'\n}", "language": "python" } ] } [/block] This route will match /authentication endpoint with only POST method to UserController's login function [block:api-header] { "type": "basic", "title": "More examples on routing" } [/block] Glim has features to validate variables inside the route level as werkzeug provides. A more complex route definition example would be the following; [block:code] { "codes": [ { "code": "# routes.py\nurls = {\n '/feeds/<slug>' : 'FeedController.get'\n}", "language": "python" } ] } [/block] In this example, this route definition will be mapped into `FeedController`‘s `get(slug)` function. [block:callout] { "type": "info", "body": "A GET | POST to /feeds route without <slug> variable will result in HTTP 404 - Not found response." } [/block] You can also define types of variables in route level. The following example will also match types; [block:code] { "codes": [ { "code": "# routes.py\nurls = {\n '/post/<int:year>' : 'PostController.get'\n}\n\n# is mapping to\n\n# app/controllers.py\nclass PostController(object):\n\t\tdef get(self, year):\n # do stuff", "language": "python" } ] } [/block] In this example, the route will only be mapped for integer typed year variables. The route will be mapped into `PostController`‘s `get()` function with year only being an integer. The routing system also allows defining the length of variables. For example; [block:code] { "codes": [ { "code": "# routes.py\nurls = {\n '/index/<string(length=2):lang_code>' : 'HomeController.index'\n}", "language": "python" } ] } [/block] In this definition, the route will be mapped `HomeController`‘s `index(lang_code)` function with `lang_code` being only strings with length of 2. For mapping any of the routes, `<any>` keyword can be used in defining generic routes like the following; [block:code] { "codes": [ { "code": "# routes.py\nurls = {\n '/<any(about, help, imprint):page' : 'PageController.resolve'\n}", "language": "python" } ] } [/block] In this definition, the route will be mapped into PageController‘s resolve(page) function with page variable being `about`, `help` or `imprint`.