我对 CodeIgniter 有疑问;我有一个名为 的控制器site
,在这个控制器中有两种方法:production
和story
.
production
通过创建production/slug
.
我想要实现的是创建以下 URL:
site/production/slug/story
我该如何做到这一点?随着 slug 的变化,在故事功能中,我想使用$this->uri->segment(3)
.
我对 CodeIgniter 有疑问;我有一个名为 的控制器site
,在这个控制器中有两种方法:production
和story
.
production
通过创建production/slug
.
我想要实现的是创建以下 URL:
site/production/slug/story
我该如何做到这一点?随着 slug 的变化,在故事功能中,我想使用$this->uri->segment(3)
.
您可以发布多个参数:
URI: site/production/slug/story/5
public function production($one, $two, $there)
{
echo $one."<br />";
echo $two."<br />";
echo $there."<br />";
}
# OUTPUT
slug
story
5
将method
名称作为第二个参数传递给第一个方法。例如,如果 URI 是site/production/slug/story
,则传递story
给production
方法并进行必要的检查,如下所示:
class Site extends CI_Controller {
public function __construct()
{
parent::__construct()
}
public function story($text) {
echo $text;
}
public function production($slug = '', $callback = NULL)
{
// Do something with $slug
if (isset($callback) && method_exists(__CLASS__, $callback)) {
$this->{$callback}($slug);
}
}
}