[英]URL segment - User Profiles - Codeigniter
大家好,我正在嘗試為我的網站創建用戶個人資料,其中的網址類似於
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的網址
2)如果您不喜歡索引在URL中的想法,則可以將函數更改為配置文件或類似名稱,或者考慮使用路由
並按照以下方式使用
$route['user/(:any)'] = "user/index/$1";
正確映射mysite.com/user/ChrisSalij的URL
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.