大家好,我正在尝试为我的网站创建用户配置文件,其中 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->uri->segment();也有命令:P

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

有帮助吗?

解决方案

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

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

1)改变

function index(){ 
$data['username'] =  $this->uri->segment(2);

成为

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

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

2) 如果您不喜欢 URL 中索引的想法,您可以更改名为 profile 等的函数,或者考虑使用 路由

并使用类似的东西

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top