Routes in Codeigniter - Automatically

Routes in Codeigniter - Automatically

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
//Finally:
$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.

EDIT after comment:

What I mean is, if you're going to match against ANY segment, this means that you cannot use any controller at all (which is, by default, the first URI segment), since the router will always re-route you using your defined law.
In order to allow CI to execute other controllers (whatever they are, I just used some common web pages, but can be literally everything), you need to allow them by excluding them from the re-routing. And you can achieve this by placing them before your ANY rule, so that everytime CI passed through your routing rules it parses first the one you "escaped", and ONLY if they don't match anything it found on the URL, it passes on to the :ANY rule.

I know that this is a code verbosity nonetheless, but they'll surely be less than 6K as you said.
Since I don't know the actual structure of your URLs and of your web application, it's the only solution that comes to my mind. If you provide further information, such as how are shaped the regular urls of your app, then I can update my answer

/end edit

This is not much a pratical solution, because it will require a lot of code, but if you want a design like that it's the only way that comes to my mind.
Also, consider you can use regexes as the $route index, but I don't think it can work here, as your usernames are unlikely matchable in this fashion, but I just wanted to point out the possibility.

OR

2) You can change your design pattern slightly, and assign another route to usernames, something along the line of

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

This will generate quite pretty (and semantic) URLs nonetheless, and will avoid all the hassle of escaping the other routes.

Automating routes with Codeigniter

There are two ways how to do that

1) is set $config['base_url'] = 'http://www.domain.com/folder/m/memberships';
and clear routes

2) set route $route['m/memberships/(:any)/(:any)'] = '$1/$2';

the first one is much cleaner

default routing in codeigniter

I dont know if you already removed index.php from your url pattern, assuming that's the case you should type inside browser address field index.php/controller/method. (if you manually type url as you describe)

If you on the other hand do not want to use index.php on every link you can consider to remove that, more info here.

How to route the URI with parameters to a method in codeigniter?

In CI 3.x the (:any) parameter matches only a single URI segment. So for example:

$route['method/(:any)/(:any)'] = 'controller/method/$1/$2';

will match exactly two segments and pass them appropriately. If you want to match 1 or 2 you can do this (in order):

$route['method/(:any)/(:any)'] = 'controller/method/$1/$2';
$route['method/(:any)'] = 'controller/method/$1';

You can pass multiple segments with the (.+) parameter like this:

$route['method/(.+)'] = 'controller/method/$1';

In that case the $1 will contain everything past method/. In general I think its discouraged to use this since you should know what is being passed and handle it appropriately but there are times (.+) comes in handy. For example if you don't know how many parameters are being passed this will allow you to capture all of them. Also remember, you can set default parameters in your methods like this:

public function method($param=''){}

So that if nothing is passed, you still have a valid value.

You can also pass to your index method like this:

$route['method/(:any)/(:any)'] = 'controller/method/index/$1/$2';
$route['method/(:any)'] = 'controller/method/index/$1';

Obviously these are just examples. You can also include folders and more complex routing but that should get you started.



Related Topics



Leave a reply



Submit