user2599060
user2599060

Reputation: 37

How to call multiple methods by URI segments in CodeIgniter

I have an issue with CodeIgniter; I have a controller named site, and in this controller there are two methods: production and story.

production calls a specific production via a model which creates production/slug.

What I want to achieve is to create the following URL:

site/production/slug/story 

How do I achieve that? As the slug changes, in the story function I want to call a story from the database using $this->uri->segment(3).

Upvotes: 2

Views: 914

Answers (2)

Hashem Qolami
Hashem Qolami

Reputation: 99564

Pass the method name as second parameter to the first method. For example, if the URI is site/production/slug/story, pass story to the production method and do necessary checks as below:

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 Demo

Upvotes: 0

Bora
Bora

Reputation: 10717

You can post multi parameters:

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

Upvotes: 1

Related Questions