0

I wanted to minimize my URL but I am having a problem. My route in Routes.php looks like this:

$route["news/(:num)"] = "home/news/$news";

In Home controller I have news function which takes a paramater $news. This is working http://localhost/bestcarsinfo/home/news/7

But when I use new URL version http://localhost/bestcarsinfo/news/7 I am getting error saying:

Undefined variable: newsinconfig/routes.php

1 Answer 1

2

Well, as the error says, $news is undefined. The router uses the $1 (or other numeric values) as a reference in the replacement for the regex that parses the route, you can't pass it arbitarty variables.

it should be:

$route["news/(:num)"] = "home/news/$1";

A snippet from the Router.php core class:

if (preg_match('#^'.$key.'$#', $uri))
{
   // Do we have a back-reference?
   if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
   {
      $val = preg_replace('#^'.$key.'$#', $val, $uri);
   }
   return $this->_set_request(explode('/', $val));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It is working, but I don't understand the idea itself. My function should take paramater $news. Here I am writing $1. Or is $1 something default variable?
$1 is a reference for the 1st element that matches the regex (in this case, (:num) ). You don't pass the actual variable, since the router doesn't know and doesn't care.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.