{"id":28765,"date":"2024-04-09T07:35:35","date_gmt":"2024-04-09T06:35:35","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/domina-las-rutas-en-codeigniter-4-guia-esencial-para-comenzar\/"},"modified":"2024-06-03T18:40:55","modified_gmt":"2024-06-03T17:40:55","slug":"domina-las-rutas-en-codeigniter-4-guia-esencial-para-comenzar","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/php\/master-routes-in-codeigniter-4-essential-guide-to-get-started\/","title":{"rendered":"Master Routes in CodeIgniter 4: Essential Guide to Get Started"},"content":{"rendered":"<p>CodeIgniter 4, the powerful PHP framework, is widely recognized for its lightweight, ease of use, and optimal performance. One of the most crucial features that any developer needs to master in this framework is the routing system. Routes in CodeIgniter 4 are essential for defining how application requests are handled and directed. In this article, we will thoroughly explore how to configure basic routes in CodeIgniter 4, accompanied by practical and simple examples.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/php\/master-routes-in-codeigniter-4-essential-guide-to-get-started\/#%C2%BFQue_es_el_Enrutamiento_en_CodeIgniter_4\" >What is Routing in CodeIgniter 4?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/php\/master-routes-in-codeigniter-4-essential-guide-to-get-started\/#Configuracion_Basica_de_Rutas\" >Basic Route Configuration<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/php\/master-routes-in-codeigniter-4-essential-guide-to-get-started\/#Paso_1_Abriendo_el_Archivo_de_Rutas\" >Step 1: Opening the Route File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/php\/master-routes-in-codeigniter-4-essential-guide-to-get-started\/#Paso_2_Definiendo_Rutas_Simples\" >Step 2: Defining Simple Routes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/php\/master-routes-in-codeigniter-4-essential-guide-to-get-started\/#Paso_3_Rutas_con_Parametros\" >Step 3: Routes with Parameters<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/php\/master-routes-in-codeigniter-4-essential-guide-to-get-started\/#Paso_4_Rutas_Agrupadas\" >Step 4: Grouped Routes<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/php\/master-routes-in-codeigniter-4-essential-guide-to-get-started\/#Buenas_Practicas_en_el_Enrutamiento\" >Good Practices in Routing<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_el_Enrutamiento_en_CodeIgniter_4\"><\/span>What is Routing in CodeIgniter 4?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Routing is the process by which you define a URL and determine which controller and method will be called in response. This system allows great flexibility and control over the behavior of applications. Routing is intended to improve organization and accessibility in an application, allowing its structure to be more intuitive for both developers and end users.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Configuracion_Basica_de_Rutas\"><\/span>Basic Route Configuration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In CodeIgniter 4, routes are defined in the file <code>app\/Config\/Routes.php<\/code>. This file is your command center for managing how HTTP requests are handled. Let&#039;s explore the basic configuration step by step:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Paso_1_Abriendo_el_Archivo_de_Rutas\"><\/span>Step 1: Opening the Route File<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Firstly, you need to open the file <code>Routes.php<\/code> located in the directory <code>app\/config<\/code>. This file contains a set of default instructions and is where you will customize the routes.<\/p>\n<pre><code class=\"&quot;language-php&quot;\">$routes-&gt;setDefaultNamespace(&#039;AppControllers&#039;); $routes-&gt;setDefaultController(&#039;Home&#039;); $routes-&gt;setDefaultMethod(&#039;index&#039;); $routes-&gt;setTranslateURIDashes(false); $routes-&gt;set404Override(); $routes-&gt;setAutoRoute(true);<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Paso_2_Definiendo_Rutas_Simples\"><\/span>Step 2: Defining Simple Routes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Let&#039;s define a simple route. Suppose you want the URL <code>\/articles<\/code> display a list of items. You need to associate this URL with a specific controller and method. Here&#039;s how you can do it:<\/p>\n<pre><code class=\"&quot;language-php&quot;\">$routes-&gt;get(&#039;\/articles&#039;, &#039;ArticulosController::index&#039;);<\/code><\/pre>\n<p>In this example, when a user accesses the URL <code>\/articles<\/code>, the request will be directed to the method <code>index<\/code> controller <code>ArticlesController<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Paso_3_Rutas_con_Parametros\"><\/span>Step 3: Routes with Parameters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Frequently, you will need to pass parameters through routes. For example, if you want to display a specific item by its ID, your path might look like this:<\/p>\n<pre><code class=\"&quot;language-php&quot;\">$routes-&gt;get(&#039;\/article\/(:num)&#039;, &#039;ArticlesController::view\/$1&#039;);<\/code><\/pre>\n<p><code>(:num)<\/code> is a construct that tells CodeIgniter to expect a numeric parameter at that position in the URL. He <code>$1<\/code> refers to the first parameter captured by the regular expression <code>(:num)<\/code>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Paso_4_Rutas_Agrupadas\"><\/span>Step 4: Grouped Routes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To keep your routes file organized, especially in large applications, you can group routes that share certain characteristics, such as the same initial URL segment or the same namespace:<\/p>\n<pre><code class=\"&quot;language-php&quot;\">$routes-&gt;group(&#039;admin&#039;, function($routes) { $routes-&gt;add(&#039;users&#039;, &#039;AdminController::users&#039;); 9;configuration&#039;, &#039;AdminController::configuration&amp;#039 ;); });<\/code><\/pre>\n<p>This grouping causes all URLs within the group to start with <code>\/admin<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Buenas_Practicas_en_el_Enrutamiento\"><\/span>Good Practices in Routing<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ol>\n<li><strong>Name the Routes:<\/strong> CodeIgniter 4 allows you to name routes, making it easy to reference them in other parts of the application without duplicating the URL itself.<\/li>\n<\/ol>\n<pre><code class=\"&quot;language-php&quot;\">$routes-&gt;get(&#039;profile&#039;, &#039;UsersController::profile&#039;, [&#039;as&#039; =&gt; &#039;user.profile&#039;]);<\/code><\/pre>\n<ol start=\"&quot;2&quot;\">\n<li>\n<p><strong>Keep Routes Clear and Concise:<\/strong> Make sure your routes are easy to understand and modify. A well-organized setup helps other developers understand the structure of the application quickly.<\/p>\n<\/li>\n<li>\n<p><strong>Security:<\/strong> Always validate and clean input parameters in your controllers to protect your application against injections and other vulnerabilities.<\/p>\n<\/li>\n<\/ol>\n<p>CodeIgniter 4&#039;s routing system offers a powerful tool for managing how HTTP requests are processed and responded to in your application. Knowing and correctly using this system will not only allow you to build more robust and flexible applications, but will also significantly improve the user experience when interacting with your site.<\/p>\n<p>To learn more about web development and stay up to date with best practices, visit regularly <a href=\"https:\/\/nelkodev.com\/en\/\">NelkoDev<\/a>. If you have questions or need to contact me directly, feel free to visit my <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">contact page<\/a>.<\/p>\n<p>Mastering routing in CodeIgniter 4 is just the beginning of the journey towards developing effective and efficient web applications. Make the most of this powerful framework and transform the way you work in the world of software development.<\/p>","protected":false},"excerpt":{"rendered":"<p>CodeIgniter 4, the powerful PHP framework, is widely recognized for its lightweight, ease of use, and optimal performance. One of the most crucial features that any developer needs to master in this framework is the routing system. Routes in CodeIgniter 4 are essential to define how application requests are handled [\u2026]<\/p>","protected":false},"author":1,"featured_media":28766,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2206,420,1913],"tags":[1344,205,233,357,1473,544,358,48,60,15,1008,1935],"class_list":["post-28765","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-blog","category-pruebas-de-backend","tag-backend","tag-blog","tag-codeigniter","tag-comenzar","tag-domina","tag-esencial","tag-guia","tag-las","tag-para","tag-php","tag-pruebas","tag-rutas"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=28765"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28765\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/28766"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=28765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=28765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=28765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}