• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Putting (hardcoded) ids in routes

In the tutorial, in the Routing section (https://codeigniter4.github.io/userguide...uting.html), the following code is used to map a route 'blog/joe' to a hardcoded id 34.

PHP Code:

I want to do something similiar, mapping page slugs to my Page controller with a certain id. I am using the following code to do this, where $page is the page variable.

PHP Code:
$routes->get$page->slug'Pages::id/' $page->id ); 

I can't get this to work. When I visit localhost:8080/my-page-slug I get a 404-error, saying Controller method is not found: id\123.

Am I doing something wrong with the mapping? Because if I visit localhost:8080/pages/id/123 it does work as expected.

I was wondering if it would be a problem if I add multiple slashes to the slug, for example, "/parent/child". However, I don't think that that is the problem since the error message seems to indicate that the mapping at least partially succeeded.

I hope someone can explain me what I am doing wrong or at least verify that I am not the only one that is having a problem with this.

OK, seems that I found the cause. In checkRoutes() in codeigniter4->framework->system->Router->Router.php the following code replaces all slashes with backslashes (lines 471 to 474).
PHP Code:
elseif (strpos($val'/') !== false)
$val str_replace('/''\\'$val);

Does anybody now why this code is there? As in: why should all slashes be translated to backslashes when they are also used for adding parameters to the routes...? Anyways, commenting these for lines of code prevents the uri from being translated to id\123 and ensures that I can access the page with id 123 at the page_slug.

It would be great if anybody could explain why this code is necessary and if I am doing something wrong with my routing that gives me these issues.

Messages In This Thread
Putting (hardcoded) ids in routes - by WhiteListed - 03-14-2019, 07:12 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.