2

我对 CodeIgniter 有疑问;我有一个名为 的控制器site,在这个控制器中有两种方法:productionstory.

production通过创建production/slug.

我想要实现的是创建以下 URL:

site/production/slug/story 

我该如何做到这一点?随着 slug 的变化,在故事功能中,我想使用$this->uri->segment(3).

4

2 回答 2

1

您可以发布多个参数:

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
于 2013-07-19T11:49:31.257 回答
0

method名称作为第二个参数传递给第一个方法。例如,如果 URI 是site/production/slug/story,则传递storyproduction方法并进行必要的检查,如下所示:

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);
        }
    }
}

PHPFiddle 演示

于 2013-07-19T12:02:38.783 回答