开发者

CodeIgniter __remap() routing more than one controller

I am trying to create a URL shortener using CodeIgniter 2.

I have 2 controllers: main and api.

For redirecting a short link through the router, I am using this setting in config/routes:

$route['(.*)'] = "main/$1";

along with a method in the main controller which should work. However, the controllers won't start. Please help me to so开发者_如何学Clve this problem.


You controller "any" isnt called because it falls into that regex, so it's routed to main. In order to exclude "any" from this rule you need to create a special rule for that, keeping in mind that for CI rules are cascading , so they're executed in the order they're presented

Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

So, you would have:

// reserved routes must come before custom routes
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['any'] = //your rule here. maybe "any". ?
$route['(.*)'] = "main/$1";  // CI also provides you with `(:any)` rule, that mateches any character.

More on this here: Uri routing

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜