我在codeigniter中有一个默认控制器
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
如你所知,我们可以调用此函数
sitename/index.php
或
sitename/
因为我在.htaccess中重写了网址
现在我想用参数传递调用相同的索引函数,如
sitename/seo-services
其中seo-services将参数传递给索引函数以加载seo-services页面。
作为codeigniter的默认语法
example.com/class/function/ID
我想要这个formet
example.com/class/ID
我想跳过类
的默认(索引)函数的函数我该怎么做?
由于
你可以看到我正在尝试这个目的。
答案 0 :(得分:4)
附加到您网址的参数会自动转换为您方法的参数。
例如,如果在欢迎控制器中有一个名为index index的方法,那么http://www.example.com/index.php/welcome/index/[parametervalue]会自动将[parametervalue]传递给您的方法,假设您定义函数以接收参数
class Welcome extends CI_Controller {
public function index($parameterValue=null)
{
//do whatever you need here
//note the default value just in case it is null
}
}
如果您想缩短时间,则必须在config / routes.php文件中定义别名。此外,如果你想摆脱index.php,你必须相应地配置你的web服务器(如果使用apache则通过.htaccess,如果使用iis则通过web.config)