code igniter routing

2019-08-08 21:55发布

问题:

I am trying to work out the code igniter routing

I have a url that will look like

http://example.com/register

I want that to hit

http://example.com/account/register/normal

I also have

http://example.com/invite/something_random_here

which I want to hit

http://example.com/account/register/invite/something_random_here

I also have

http://example.com/register/something_random_here

which I want to hit

http://example.com/account/register/subscribed/something_random_here

How do I setup these routes?

回答1:

Pretty much Straight out of the User Guide

$route['register'] = "account/register/normal";
$route['invite/(:any)'] = "account/register/invite/$1";

Basically anything after invite/ will get tacked onto the end of account/register/invite. I believe it only works for one segment, if you want multiple segment support you'll have to use a regular expression:

$route['invite/(.+)'] = "account/register/invite/$1";

Another usefull one (because I believe (:any) only works for characters) would be:

$route['invite/([a-zA-Z0-9_-]+)'] = "account/register/invite/$1";

This will let all alpha-numeric values (single segment) with _ or - go through, great for GUIDs :)



回答2:

I believe for your first URL, you would use the following route:

$route['register'] = "account/register/normal";

Your second URL, would make use of the following route:

$route['invite/(:any)'] = "account/register/invite/$1";

Both of these routes would need to be placed in your "config/routes.php" file.

Hope this helps?