Php 在特定的用户页面上,如何结合路由和uri段将用户id放入url中?

Php 在特定的用户页面上,如何结合路由和uri段将用户id放入url中?,php,codeigniter,uri,Php,Codeigniter,Uri,我试图在查看他们的页面时将用户id附加到url,或者在查看其他用户页面时将用户id附加到url。我收到此错误无法加载请求的文件:account/profile/218.php,代码如下: //Routes.php $route['default_controller'] = "home"; $route['404_override'] = ''; $route['profile/:num'] = "account/profile"; //I've also tried doing $rout

我试图在查看他们的页面时将用户id附加到url,或者在查看其他用户页面时将用户id附加到url。我收到此错误
无法加载请求的文件:account/profile/218.php
,代码如下:

//Routes.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]
使用:


并在应用程序/config/routes.php文件中删除配置文件中的index.php

$route['profile/(:num)]=“account/profile/$1”

有关路由的更多信息,请阅读

在profile方法中添加额外的参数,即用户ID 因此,
$user['id']=$user\u id无需使用
$this->uri->段(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>

重新启动发动机
重写cond%{SCRIPT_FILENAME}-d[或]
重写cond%{SCRIPT_FILENAME}-f
重写规则“(^ |/)\”-[F]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)$index.php?/$1[L]

我应该用什么替换$config['index_page']='index.php';具有如果我把它拿出来,它就不会加载我的任何页面。“删除index.php文件”我能够删除index.php,这很好,但是,我仍然无法加载请求的文件:account/profile/218.php错误,当我删除$user['id']=$this->uri->segment(3);但是,当我添加$user['id']=$this->uri->segment(3)时;我无法加载请求的文件:account/profile/.php错误。另外,请注意,当htaccess出现错误时,它会在没有任何cssupon进一步调查的情况下加载我的标题nav结构。去掉index.php会杀死我的css链接。我对它们的路径使用base_url(),其中不包括index.php$config['url_suffix']='';我想这就是你的意思$数据['user']=$user;$user['id']=$user\u id;$data['profile_icon']='profile';$data['main_content']='account/profile/'。$user_id;$this->load->view('includes/templates/profile\u template',$data);将“$user_id”附加到url,而不是上面示例中的$user['id']。在这两个版本中,我仍然会收到一个页面加载错误以及您的确切代码。它不会继续加载页面,并且会收到以下错误严重性:警告消息:Account::profile()文件名:controllers/Account.php行号:140缺少参数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);

}
<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>