Question

I am a newbee to codeigniter I have a website with three pages. (Home about Contact) I want to put an anchor to each of them and catch last segment using $this->uri->segment() in controller's index function. Then using a switch I want to direct to exact pages.

This is one of my anchor:

<h3  id="anchor_storefront"><?php echo anchor('jstorecontroll/home', 'Home'); ?></h3>

And this is my code in index at controller:

 switch( $this->uri->segment(2) )
        {
            case "":
            case "home":
                $this->load->view('public/home');
                break;
        }

Can an expert guide me? Thanks

Était-ce utile?

La solution

How about just having a function for each page? This follows CodeIgniter's usual URI pattern example.com/class/function/id/ - something like this:

class Jstorecontroller extends CI_Controller
{

    function index()
    {
        //Do what you want... load the home page?
    }

    //Load the 'home' page
    function home()
    {
        $this->load->view('public/home');
    }

    //Load the 'about' page
    function about()
    {
        $this->load->view('public/about');
    }

    //Load the 'contact' page
    function contact()
    {
        $this->load->view('public/contact');
    }

}

Routing can be used to map URLs: To map jstorecontroll as the first segment and anything as the second segment to the index function in your jstorecontroll controller, you could use this route (in application/config/routes.php):

$route['jstorecontroll/(:any)'] = "jstorecontroll/index/$1";

You may want to use regex to restrict what is mapped though, for example:

$route['jstorecontroll/([a-z]+)'] = "jstorecontroll/index/$1";

You could then have a function in your controller that would filter through and load the corresponding page. However, be wary of the user input - don't trust them! Make sure you sanitise the input.

class Jstorecontroll extends CI_Controller
{

    function index($page = FALSE)  //Default value if a page isn't specified in the URL.
    {
        if($page === FALSE)
        {
            //Do what you want if a page isn't specified. (Load the home page?)
        }
        else
        {
            switch($page)
            {
                case "home":
                    $this->load->view('public/home');
                    break;
                case "about":
                    $this->load->view('public/about');
                    break;
                case "contact":
                    $this->load->view('public/contact');
                    break;
            }
        }
    }

}

The use of the above route may produce undesired results if you have other function in this controller that you want to be called from a URI, they won't be called but instead mapped as a parameter to the index function. Unless you look at changing (or adding) the routes, or you could look into remapping functions. Personally, I'd just use a function for each page!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top