dongzhuang6247 2014-05-20 04:42
浏览 49
已采纳

如何动态创建codeigniter路由?

I have a CI routes.php as below.

$route['default_controller'] = "site";
$route['404_override'] = 'site';
$route['admin'] = 'admin';
$route[':any'] = 'site/user';

The thing required is when any user name (which I will check with database) is typed with URL like - localhost/CIproj/username then it should go to site/user controller.

If 'admin' is there in URL like - localhost/CIproj/admin it will go to admin controller (index function). URL will change to localhost/CIproj/admin/dashboard.

So the error I am getting is when I go to localhost/CIproj/admin, I get login screen. But after login it will go to admin/dashboard. This is where I get redirected to site/user.

Then I tried with below one.

$route['user/(:any)'] = 'site/user';

And now everything was working. But the URL will be like - localhost/CIproj/user/username.

So the question is , is there any way where I don't have to write user in the URL. Visitors can type anything in the URL.

 localhost/CIproj/anything

if anything== username from database it will go to site/user.

if anything== admin or any of its page it will go to respective controllers.

Or else 404pagenotfound.php.

Note: Admin is a huge module with many controllers. So I don't think I should write routes for every of admin controller here.

  • 写回答

3条回答 默认 最新

  • doumei9832 2014-05-30 07:30
    关注

    Just sitting blank. Gone through the question again and found the solution. So the question is , is there any way where I don't have to write user in the URL. Visitors can type anything in the URL.

    localhost/CIproj/anything

    if anything== username from database it will go to site/user.

    if anything== admin or any of its page it will go to respective admin controllers.

    Or else 404pagenotfound.php.

    The route.php will be like this ->

    $route['default_controller'] = "site";  // controller - site / method - index (404 page).  
    $route['404_override'] = 'site';  // same as above
    $route['(iam:any)'] = 'site/user/$1'; // appending 'iam' to anything user enters. then in 'site' controller and 'user' method I will remove 'iam' from the segment and then check with database username. if found it will proceed to respective user page or else 404 page. 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 添加组件无法加载页面,某块加载卡住
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用