0

我试图在查看他们的页面时将用户 ID 附加到 URL,或者在查看另一个用户页面时将用户 ID 附加到 URL。我收到此错误Unable to load the requested file: account/profile/218.php代码:

//路由.php

$route['default_controller'] = "home";
$route['404_override'] = '';
$route['profile/:num'] = "account/profile"; 
//I've also tried doing 
$route['profile/([a-z]+)/(\d+)'] = "profile/$1/id_$2"; 

//没有产生上述错误的uri段的控制器:

public function profile() 
    {

        $this->load->helper('date');
        $this->load->library('session');
        $session_id = $this->session->userdata['id'];
        $this->load->model('account_model');
            $user = $this->account_model->user();
        $data['user'] = $user;
        $data['profile_icon'] = 'profile';
        $data['main_content'] = 'account/profile/'.$user['id'];
        $this->load->view('includes/templates/profile_template', $data);

    }

当我使用这个时:

public function profile() 
{

    $this->load->helper('date');
    $this->load->library('session');
    $session_id = $this->session->userdata['id'];
    $this->load->model('account_model');
        $user = $this->account_model->user();
    $data['user'] = $user;
    $user['id'] = $this->uri->segment(4);
    $data['profile_icon'] = 'profile';
    $data['main_content'] = 'account/profile/'.$user['id'];
    $this->load->view('includes/templates/profile_template', $data);

}

它会产生此错误:

无法加载请求的文件:account/profile/.php

* *编辑

HTACCESS

Deny from all
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
4

2 回答 2

1

利用:

$route['profile/:num'] = "account/profile/$1";

并在您的配置文件中删除 index.php

于 2013-02-07T21:32:48.803 回答
1

application/config/routes.php文件中添加

$route['profile/(:num)'] = "account/profile/$1";

有关路由的更多信息,您可以在此处阅读

public function profile( $user_id ) 
{

    $this->load->helper('date');
    $this->load->library('session');
    $session_id = $this->session->userdata['id'];
    $this->load->model('account_model');
    $user = $this->account_model->user();
    $data['user'] = $user;
    $user['id'] = $user_id;
    $data['profile_icon'] = 'profile';
    $data['main_content'] = 'account/profile/'.$user['id'];
    $this->load->view('includes/templates/profile_template', $data);

}

将额外参数添加到配置文件方法 - 女巫将是用户 ID,因此$user['id'] = $user_id;无需使用该$this->uri->segment(4);方法。

我建议您像这样修改 htaccess 文件:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
于 2013-02-08T21:29:03.343 回答