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条)

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上