• 0
name Punditsdkoslkdosdkoskdo

How to Routes in Codeigniter - Automatically in php

I have a problem with Codeigniter routes. I would like to all registered users on my site gets its own "directory", for example: www.example.com/username1www.example.com/username2. This "directory" should map to the controller "polica", method "ogled", parameter "username1".

If I do like this, then each controller is mapped to this route: "polica/ogled/parameter". It's not OK:

$route["(:any)"] = "polica/ogled/$1";

This works, but I have always manually entered info in routes.php:

$route["username1"] = "polica/ogled/username1";

How do I do so that this will be automated?

UPDATE: For example, I have controller with name ads. For example, if you go to www.example.com/ads/ there will be listed ads. If you are go to www.example.com/username1 there are listed ads by user username1. There is also controller userprofilelatest,...

My Current routes.php:

$route['oglasi'] = 'oglasi';
$route['(:any)'] = "polica/ogled/$1"
$route['default_controller'] = 'domov';
$route['404_override'] = '';

I solved problem with this code:

$route['oglasi/(:any)'] = 'oglasi/$1';
$route['(:any)'] = "polica/ogled/$1"
$route['default_controller'] = 'domov';
$route['404_override'] = '';
  • 1

The problem with your route is that by using :any you match, actually...ANY route, so you're pretty much stuck there. I think you might have two solutions:

1)You can selectively re-route all your sites controller individually, like:

$route['aboutus'] = "aboutus";
$route['where-we-are'] = "whereweare";
//And do this for all your site's controllers
$route['(:any)'] = "polica/ogled/$1";

All these routes must come BEFORE the ANY, since they are read in the order they are presented, and if you place the :any at the beginning it will happily skip all the rest.

  • 1
Reply Report