dongzhuang6247 2014-05-19 20: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-29 23: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 爱快路由器端口更改错误导致无法访问
  • ¥20 安装catkin时遇到了如下问题请问该如何解决呢
  • ¥15 VAE模型如何输出结果
  • ¥15 编译python程序为pyd文件报错:{"source code string cannot contain null bytes"
  • ¥20 关于#r语言#的问题:广义加行模型拟合曲线后如何求拐点
  • ¥15 fluent设置了自动保存后,会有几个时间点不保存
  • ¥20 激光照射到四象线探测器,通过液晶屏显示X、Y值
  • ¥50 数据库开发问题求解答
  • ¥15 安装anaconda时报错
  • ¥15 小程序有个导出到插件方式,我是在分包下引入的插件,这个export的路径对吗,我看官方文档上写的是相对路径
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部