CJD
CJD

Reputation: 747

Codeigniter routing with forward slashes

Codeigniter routing rules don't seem to pick up anything after a slash for me - is this the default behaviour - how can I prevent it?

Example:

In routes.php:

$route['blog/read-post/(.+)'] = 'blog/lookup_blog_alias/$1';

In controller:

function lookup_blog_alias($str){
print $str;
}

If I enter a url such as:

http://localhost/blog/read-post/a-b-c/12

I only get the "a-b-c" part when what I would like is "a-b-c/12".

Thanks for any help.

Upvotes: 1

Views: 2571

Answers (1)

Knowledge Craving
Knowledge Craving

Reputation: 7993

No, by default, your requirement will only work if you change the route URL & the controller method to something like this:-

$route['blog/read-post/(.+)/(.+)'] = 'blog/lookup_blog_alias/$1/$2';

// Controller Method
function lookup_blog_alias($str1, $str2){
  print $str1.'/'.$str2;
}

You need to follow the basics of the CodeIgniter User Guide, where it says that it's the convention of CI to mark a limit of string with the character "/" (forward-slash). It simply means that in between two forward-slashes, the following things can / may be considered in a general MVC Framework:-

  • Module Name
  • Controller (Class) Name
  • Controller Method Name
  • (1st) Query String's Index
  • (1st) Query String's Value
  • (2nd) Query String's Index
  • (2nd) Query String's Value
  • ... (Query String's Index / Value Pair)

However, in the "routes.php" page of CodeIgniter, the logic can be changed by the virtue of HTAccess. Still the above concept stands straight & so it will be wise to follow the normal MVC architecture.

Hope it helps.

Upvotes: 2

Related Questions