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

如何动态创建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条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集