Al Kasih
Al Kasih

Reputation: 886

One Method Controller, Multiple View, Codeigniter

So, lets say I have a global view and specific view.

In global view, the url may look like this (http://example.com/index.php/controller/method/)

Where when it come to the specific page view, the url will turn like this: (http://example.com/index.php/controller/method/1989-03-25)

The difference between the global view and the specific page view is, if in the global view it shows the information in general, but in the specific page view it shows based on the detail or the date.

Of course, not only have different view, but also they will have different function of models.

The point is, I just want to make the url keep in order (which it mean there is no change in the name of the controller method).

How to do this. Thanks in advance.

Upvotes: 0

Views: 1418

Answers (3)

Cedric
Cedric

Reputation: 1256

You can add additional parameter in your method like:

class Your_controller extends CI_controller {
    public function method($params = null)
    {
        // Your Code Here with the condition for processing of the $params variable
    }
}

in which that $params can be something in your URL like:

http://example.com/controller/method/your-dynamic-params

So if the $params is null you will call the model the views the general and if the $params has a specific value you can call other model by using if or switch conditional statements. Hope this helps...

Update with Example

you can use the $params variable like this:

if ($params == "1991") {
    $this->load->view('general.html', $params);
} elseif ($params == "1992") {
    $this->load->view('year_1992.html', $params);
} else {
    $this->load->view('other_years.html', $params)
}

in this way you can use the $params as a conditional variable to load different views.

or using switch:

switch($params) {
    case '1991':
        $this->load->view('general.html', $params);
    break;
    case '1992':
        $this->load->view('year_1992.html', $params);
    break;
    default:
        $this->load->view('other_years.html', $params)
}

Note: Use a helper method so you can avoid fat controllers because it will be hard to test your methods if you have a several lines of codes in a function.

Upvotes: 1

TomerM
TomerM

Reputation: 365

This class used to wrap CI_controller, my_base_controller will override CI_controller methods for depends to your project needs

class my_base_controller extends CI_controller {

}

You can load models by known states and define it in my_base_controller class.

class my_controller extends my_base_controller{
    public function method($params=null) //null is option value
    {

    }
}

Good luck!

Upvotes: 1

Padmanathan J
Padmanathan J

Reputation: 4620

You create just one param into your function. And set the param value is null. like this

class YOUR_CLASS extends CI_controller {
    public function method($params=null) //null is Empty value
    {
           if($params)
           {
              //load your modal and view with your param
           }
           else
           {
              //load your modal and view
           }
    }
}

This method supports the following type of URL's without any issue.

http://example.com/index.php/YOUR_CLASS/method/

http://example.com/index.php/YOUR_CLASS/method/1989-03-25

Hope this will help you....

Upvotes: 1

Related Questions