【问题标题】:URL segment - User Profiles - CodeigniterURL 段 - 用户配置文件 - Codeigniter
【发布时间】:2010-11-08 17:51:02
【问题描述】:

我正在尝试为我的网站创建用户配置文件,其中 URL 类似于

mysite.com/user/ChrisSalij

目前我的控制器是这样的:

<?php   
class User extends Controller {
   function user(){
      parent::Controller();
   }
   
   function index(){
      $data['username'] =  $this->uri->segment(2);

      if($data['username'] == FALSE) {
         /*load default profile page*/
      } else {
         /*access the database and get info for $data['username']*/
         /*Load profile page with said info*/
      }//End of Else
   }//End of function
}//End of Class
?>

目前我每次去都会收到 404 错误;

mysite.com/user/ChrisSalij

我认为这是因为它期望有一个名为 ChrisSalij 的方法。 虽然我确定我也在滥用$this-&gt;uri-&gt;segment(); 命令

任何帮助将不胜感激。 谢谢

【问题讨论】:

    标签: php codeigniter user-profile


    【解决方案1】:

    这是因为控制器正在寻找一个名为 ChrisSalij 的函数。

    解决这个问题的几种方法:

    1. 改变

      函数索引(){ $data['username'] = $this->uri->segment(2);

    成为

    function index($username){ 
    $data['username'] =  $username; 
    

    并使用 mysite.com/user/index/ChrisSalij 的 URL

    1. 如果您不喜欢将索引放在 URL 中的想法,您可以将函数更改为配置文件等,或者考虑使用 routing

    并使用类似的东西

    $route['user/(:any)'] = "user/index/$1";
    

    正确映射mysite.com/user/ChrisSalij的URL

    【讨论】:

    • 像魅力一样工作。谢谢你。我觉得自己像个白痴,没有考虑通过函数:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多