{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","settings":"","params":[],"results":{"codes":[]}},"next":{"description":"","pages":[]},"title":"Routing","type":"basic","slug":"supporting-structure-routing","excerpt":"","body":"# Routing\n\nYou've probably learned how to use [Controller](doc:mvc-controller)? Let's try on how explicitly create a route for your modules.\n\nRouting is the place where you define how the system interact with the **URL** requests. Let's assume we have this page to login ``example.app/login``.\n\n## Advisable to know these firsts:\n- <a target=\"_blank\" href=\"/docs/mvc-module\">Module</a>\n- <a target=\"_blank\" href=\"/docs/mvc-controller\">Controller</a>\n- <a target=\"_blank\" href=\"/docs/mvc-view\">View</a>\n\n## Index:\n- [Basic Usage](#basic-usage)\n- [Dynamic Route](#dynamic-route)\n- [Naming Route](#naming-route)\n- [Route Group](#route-group)\n- [Learn More](#learn-more)\n\n<a name=\"basic-usage\"></a>\n# Basic Usage\nunder your **project-name/app/main/routes.php**, let's add this:\n```php\n<?php\n\nroute()->addGet('login', [\n 'controller' => 'Auth',\n 'action' => 'showLogin',\n]);\n```\n\nThe above code shows how to create a ``GET`` request, the first parameter is the ```/login``` uri, and if you will be attempting to visit the page ```example.app/login```, the dispatcher will analyze the request **URI**, if found a match, let us say it matches the route above.\n\nThe second parameter comes in, which we point it to ``Auth`` controller and the function ``showLogin`` will be called.\n\nGo to **project-name/app/main/controllers/AuthController.php** and write the function like below:\n```php\n<?php\nnamespace App\\Main\\Controllers;\n\nclass AuthController extends Controller\n{\n public function showLogin()\n {\n return view('auth.login');\n }\n}\n```\n\nThe above code will be triggered once the url is visited, then we call the ``view()`` function to show our template engine file.\n\n\n---\n\n\n<a name=\"dynamic-route\"></a>\n# Dynamic Route\n\nYou've learned the basic, now let's move on to this dynamic route.\n\n```php\nroute()->addGet('users/{id}/edit', [\n 'controller' => 'User',\n 'action' => 'edit',\n]);\n```\n\nand here is the controller sample\n\n```php\n<?php\n\nnamespace App\\Main\\Controllers;\n\nclass UserController extends Controller\n{\n public function edit($id)\n {\n # do a model query using the $id passed in\n dd($id);\n }\n}\n```\n\nThe above codes show how to handle url values, so attempting to run this url to your browser ``example.app/users/1/edit``.\n\nIt should die-and-dump the ``$id`` which value is **1**.\n\n\n---\n\n\n<a name=\"naming-route\"></a>\n# Naming Route\n\nLet's extend more about the ``login`` uri by adding **addPost** also.\n\nAdd a ``setName(<name>)`` chain on it.\n\n```php\nroute()->addPost('login', [\n 'controller' => 'Auth',\n 'action' => 'attemptLogin',\n])->setName('attemptLogin');\n```\n\nIn your ``controller`` or ``view``, you may call the function ``route('showLogin')`` to return the full url.\n\n```php\n// ...\nclass ... extends ...\n{\n public function showLogin()\n {\n $post_login_url = route('attemptLogin'); // example.app/login\n\n return view('auth.showLogin')\n ->withPostLoginUrl($post_login_url);\n }\n\n public function attemptLogin()\n {\n // code...\n }\n}\n```\n\nThe code above shows how to get the full url by just getting the route name.\n\nHow about the route ``users/{id}/edit``, let's inject a name **userEdit** in it, and call it this way by providing the ``id`` in the second parameter.\n\n```php\necho route('userEdit', [\n 'id' => 100,\n]);\n```\n\nIt must return ``example.app/users/100/edit``.\n\n\n---\n\n\n<a name=\"route-group\"></a>\n# Route Group\n\nRoute group is much more cleaner to use if you want to separate a scope into a class registrar \n- having a url like this ``http://example/users/../..`` into **UsersRoute** class\n- as well ``http://example/auth/..`` into **AuthRoute** class\n\nRun this to your console:\n```shell\nphp brood app:route Users main\n```\n\n```shell\n> Crafting Route...\n> UsersRoutes.php created!\n> Generating autoload files\n```\n\nThe above command generates a **UsersRoutes.php** inside your ***main*** module.\nGo to **project-name/app/main/routes/UserRoutes.php**, open the file, you should have this defined values.\n\n```php\n<?php\nnamespace App\\Main\\Routes;\n\nclass UsersRoutes extends RouteGroup\n{\n public function initialize()\n {\n $this->setPaths([\n 'namespace' => 'App\\Main\\Controllers',\n 'controller' => 'Users',\n ]);\n\n $this->setPrefix('/users');\n\n # - url as users/index\n $this->addGet('/index', [\n 'action' => 'index'\n ]);\n\n # - url as users/store\n $this->addPost('/store', [\n 'action' => 'store'\n ]);\n\n # - url as users/1/show\n $this->addGet('/{id}/show', [\n 'action' => 'show'\n ]);\n\n # - url as users/1/update\n $this->addPost('/{id}/update', [\n 'action' => 'update'\n ]);\n\n # - url as users/1/delete\n $this->addPost('/{id}/delete', [\n 'action' => 'delete'\n ]);\n }\n}\n```\n\nNow register this class in your **project-name/app/main/routes.php**.\n\n```php\nroute()->mount(new App\\Main\\Routes\\UsersRoutes);\n```\n\n\nCreate the ``UsersController`` at ``project-name/app/main/controllers`` or execute the brood console command instead.\n```php\n<?php\nnamespace App\\Main\\Controllers;\n\nclass UsersController extends Controller\n{\n // add a function\n}\n```\n\nThat's it, and you should have a simple resource URL.\n- /users/index\n- /users/store\n- /users/{id}/show\n- /users/{id}/update\n- /users/{id}/deleted\n\n\n---\n\n\n<a name=\"learn-more\"></a>\n# Learn More\n\nTo learn more, I have this reference that shows how fully Phalcon routing works.\n- <a target=\"_blank\" href=\"https://docs.phalconphp.com/en/latest/reference/routing.html\">https://docs.phalconphp.com/en/latest/reference/routing.html</a>","updates":[],"order":1,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"56e26e42353e060e00b96826","project":"56c111095abfe40d00be875a","version":{"version":"1.3.0","version_clean":"1.3.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["56c1110a5abfe40d00be875e","56c413a254b6030d00ec299d","56c4275048213b1700af6e33","56c42826c0c4630d004e86cb","56c4282cbc41330d009f2607","56c4284ad1f6d91700d3697e","56e271c195d1c60e00a969ee"],"_id":"56c111095abfe40d00be875d","releaseDate":"2016-02-14T23:43:05.566Z","__v":7,"createdAt":"2016-02-14T23:43:05.566Z","project":"56c111095abfe40d00be875a"},"__v":44,"user":"56c1105874f0b417004baadc","githubsync":"","parentDoc":null,"category":{"sync":{"isSync":false,"url":""},"pages":["56e26e3a8d79b50e0031d8f6","56e26e42353e060e00b96826","56e26e4d95d1c60e00a969db","56e26e555ab0871700957c04","56e26e6f5ab0871700957c06","56e26e7d7d54ba0e004635a3","56e26e85353e060e00b96828"],"title":"Supporting Structure","slug":"supporting-structure","order":3,"from_sync":false,"reference":false,"_id":"56c42826c0c4630d004e86cb","__v":7,"createdAt":"2016-02-17T07:58:30.542Z","project":"56c111095abfe40d00be875a","version":"56c111095abfe40d00be875d"},"createdAt":"2016-03-11T07:05:38.310Z"}