Codeigniter 代码点火器控制器页面
如何制作控制器,该控制器可通过以下url地址访问:Codeigniter 代码点火器控制器页面,codeigniter,controller,Codeigniter,Controller,如何制作控制器,该控制器可通过以下url地址访问: /index.php/my_controller/controller_function/another_function 或 我知道这是可能的,但我找不到任何网站来解释这一点,从手册中我读到控制器应该在文件夹中,但是接下来呢?制作一个名为的控制器,类帐户扩展CI_控制器,它具有配置文件,方法,使用参数$action-当您访问/account/profile/edit时,该参数将自动填入edit public function profile
/index.php/my_controller/controller_function/another_function
或
我知道这是可能的,但我找不到任何网站来解释这一点,从手册中我读到控制器应该在文件夹中,但是接下来呢?制作一个名为
的控制器,类帐户扩展CI_控制器
,它具有配置文件
,方法,使用参数$action
-当您访问/account/profile/edit
时,该参数将自动填入edit
public function profile ($action = '')
{
switch ($action)
{
case 'edit':
$this->show_edit_account();
break;
default:
$this->show_default_action();
break;
}
}
protected function show_edit_account ()
{
echo 'Edit account!';
}
作为Joe答案的另一种选择,我发现当我将一个控制器函数直接映射到URL路径中的内容而不是调用其他函数时,我的代码更容易组织。取决于您计划在account类中执行多少操作,从这个控制器维护可能会很麻烦。所以,如果您所要做的只是实现特定的URL结构,那么您可以将控制器嵌套在CodeIgniter的controllers文件夹中的子文件夹中 因此
/index.php/account/profile/edit
将映射到/application/controllers/account/profile.php
,而edit
将是profile.php
中的一个函数。这样,您还可以为帐户中的不同功能维护单独的控制器。e、 g.除了配置文件之外,您还可以创建一个单独的delete.php
控制器来处理帐户删除
您可以在手册中阅读更多关于CodeIgniter如何将URL映射到控制器、函数和变量的内容:是否可以从这两个方面访问它(如果可以,我建议您在问题中使用“and”,而不是“OR”)?你看过吗?
public function profile ($action = '')
{
switch ($action)
{
case 'edit':
$this->show_edit_account();
break;
default:
$this->show_default_action();
break;
}
}
protected function show_edit_account ()
{
echo 'Edit account!';
}