{"id":57731,"date":"2023-08-24T17:28:28","date_gmt":"2023-08-24T11:58:28","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=57731"},"modified":"2023-09-04T18:05:07","modified_gmt":"2023-09-04T12:35:07","slug":"create-a-dynamic-route-in-drupal-8-9","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/create-a-dynamic-route-in-drupal-8-9\/","title":{"rendered":"Create A Dynamic Route in Drupal 8 &amp; 9"},"content":{"rendered":"<p style=\"text-align: left;\">As we know providing routes in the routing.yml file is the simplest way to achieve the static routes. Creating dynamic routes is a bit different from the way we achieve static routes.<\/p>\n<p style=\"text-align: left;\">So In Drupal 8 or 9, we can achieve this by following below few steps below.<\/p>\n<h2 style=\"text-align: left;\"><strong>Step 1. Create an entry in custom.routing.yml<\/strong><\/h2>\n<p style=\"text-align: left;\">So we need to add an entry in the method &#8220;route_callbacks&#8221; in our routing file.<\/p>\n<blockquote>\n<pre>route_callbacks:\r\n- '\\Drupal\\custom\\Routing\\DynamicListingRoutes::routes'<\/pre>\n<\/blockquote>\n<div style=\"text-align: left;\">\n<h2><strong>Step2. Create a routing class<\/strong><\/h2>\n<p>Now we need to create a Routing class &#8220;DynamicListingRoutes&#8221;\u00a0 and add our logic.<\/p>\n<p>The dynamic routing method can return either an array of <strong>Route<\/strong> objects or a <strong>RouteCollection<\/strong> Object.<\/p>\n<p><strong>1.<\/strong> Symfony\\Component\\Routing\\Route object<\/p>\n<div><a href=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/imgonline-com-ua-twotoone-kDMkpd0dNIl0t-2.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-57735\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/imgonline-com-ua-twotoone-kDMkpd0dNIl0t-2.jpg\" alt=\"\" width=\"1223\" height=\"1441\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/07\/imgonline-com-ua-twotoone-kDMkpd0dNIl0t-2.jpg 1223w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/imgonline-com-ua-twotoone-kDMkpd0dNIl0t-2-255x300.jpg 255w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/imgonline-com-ua-twotoone-kDMkpd0dNIl0t-2-869x1024.jpg 869w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/imgonline-com-ua-twotoone-kDMkpd0dNIl0t-2-768x905.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/07\/imgonline-com-ua-twotoone-kDMkpd0dNIl0t-2-624x735.jpg 624w\" sizes=\"(max-width: 1223px) 100vw, 1223px\" \/><\/a><\/div>\n<p>&nbsp;<\/p>\n<p><strong>2.<\/strong> \\Symfony\\Component\\Routing\\RouteCollection object<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/route-collection.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-58121\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/route-collection.png\" alt=\"\" width=\"772\" height=\"1363\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/route-collection.png 772w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/route-collection-170x300.png 170w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/route-collection-580x1024.png 580w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/route-collection-768x1356.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/route-collection-624x1102.png 624w\" sizes=\"(max-width: 772px) 100vw, 772px\" \/><\/a><\/p>\n<\/div>\n<p>In the above routing class, I&#8217;ve tried to create multiple routes based on the location of the hospitals.<\/p>\n<h2 style=\"text-align: left;\"><strong>Step 3. Create a Controller class<\/strong><\/h2>\n<p style=\"text-align: left;\">So, the last and 3rd step is to write a controller in which we describe the functionality or our logic.<br \/>\n<a href=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-24-15-18-48.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignleft size-full wp-image-58122\" src=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-24-15-18-48.png\" alt=\"\" width=\"772\" height=\"390\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-24-15-18-48.png 772w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-24-15-18-48-300x152.png 300w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-24-15-18-48-768x388.png 768w, \/blog\/wp-ttn-blog\/uploads\/2023\/08\/Screenshot-from-2023-08-24-15-18-48-624x315.png 624w\" sizes=\"(max-width: 772px) 100vw, 772px\" \/><\/a><\/p>\n<p>So in this case we&#8217;ve rendered a hospital listing view based on the location which we&#8217;re getting from our dynamic routing.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nSo using this method, we can create dynamic routes and render data according to the dynamic values.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>As we know providing routes in the routing.yml file is the simplest way to achieve the static routes. Creating dynamic routes is a bit different from the way we achieve static routes. So In Drupal 8 or 9, we can achieve this by following below few steps below. Step 1. Create an entry in custom.routing.yml [&hellip;]<\/p>\n","protected":false},"author":1527,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":23},"categories":[3602],"tags":[5359,5358,5357],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57731"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1527"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=57731"}],"version-history":[{"count":5,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57731\/revisions"}],"predecessor-version":[{"id":58297,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/57731\/revisions\/58297"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=57731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=57731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=57731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}