1

我对 CI 相当陌生,并且一直在尝试如何生成干净的 URL。我之前通过编辑我的 .htaccess 文件完成了这项任务,但没有使用框架,如下所示。

RewriteCond %{REQUEST_URI} !^/(css|js|img)/
RewriteRule ^profile/([^/]*)$ profile.php?id=$1 [L]

使用 CI,我尝试了以下方法:

#Get rid of the index.php that's in the URL by default
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

# Profile page
RewriteCond %{REQUEST_URI} !^/(css|js|img)/
RewriteRule ^profile/([^/]*)$ profile?id=$1 [L]

我知道默认情况下,URL 中控制器名称后的值(在本例中为 Profile 控制器)将调用控制器类中具有相同名称的函数。但是,如果在控制器后指定的 URL 中没有值,默认情况下会调用 index 函数。我计划将函数名称留空,以便默认调用索引函数。但是,重写规则不起作用。

有任何想法吗?

4

1 回答 1

1

有了.htaccess你可以这样做

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

# Profile page
RewriteCond %{REQUEST_URI} !^/(css|js|img)/
RewriteRule ^profile/([^/]*)$ profile/index/$1 [L]

在重写时,您必须提及函数名称,无论它是索引函数还是任何其他函数

与您可以使用 CI 路由相同routes.php

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

现在在配置文件的索引功能中,您可以获得参数

function index($id) {
echo $id;
echo $this->uri->segment(3);
//Both will result the same 
}
于 2013-07-11T06:16:07.753 回答