漂亮的URL与CodeIgniter [英] Pretty URL's with CodeIgniter

查看:78
本文介绍了漂亮的URL与CodeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对CI还是陌生的,一直在尝试如何生成干净的URL。在不使用框架的情况下,我通过如下编辑我的.htaccess文件来完成此任务。

I'm fairly new to CI and have been trying out how to produce clean URL's. I have accomplished this task before without using a framework by editing my .htaccess file as follows.

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

使用CI,我尝试了以下操作:

With CI, I have tried the following:

#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函数。我计划将函数名称保留为空白,以便默认情况下将调用索引函数。但是,重写规则不起作用。

I know that by default, the value after the name of the controller in the URL (in this case, the Profile controller), will invoke the function with the same name inside the controller class. But, if there is no value in the URL specified after the controller, by default, the index function will be invoked. I plan on leaving the function name blank so that the index function will be invoked by default. But, the rewrite rule isn't working.

有什么想法吗?

推荐答案

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

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

In rewrite you have to mention the function name whether it is the index function or any other

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

Same as you can utilize the CI routing routes.php

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

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

Now in index function of profile you can get the parameter

function index($id) {
echo $id;
echo $this->uri->segment(3);
//Both will result the same 
}

这篇关于漂亮的URL与CodeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆