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 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析