Search code examples
phpcodeigniterinline-functions

Can codeigniter support Inline functions.?


Can we write multiple functions inside another function in Codeigniter.? here is my controlller

class Products extends CI_Controller {

  public function myproduct() {
      $this->load->view('myproduct'); // call myproduct.php

           public function features() {
              $this->load->view('features');  // call "myproduct/features"
            }

           public function screenshots() {
              $this->load->view('screenshots');  // call "myproduct/screenshots"
            }
    }
}

as per my controller there are 2 inline functions inside myproduct(). my aim isto display the url as

localhost/mysite/products/myproduct
localhost/mysite/products/myproduct/features
localhost/mysite/products/myproduct/screenshots

i already tried it but it gives me a error

Parse error: syntax error, unexpected 'public' (T_PUBLIC) in D:\...........\application\controllers\mysite\products.php on line 5

and the line 5 was

public function features() { .........

Solution

  • You can just treat this as a uri paramater in the url:

    public function myproduct($param = null) 
    {
        if($param == null) {
            $this->load->view('myproduct'); 
        } elseif($param == 'features') {
            $this->load->view('features');
        } elseif ($param == 'screenshots') {
            $this->load->view('screenshots');
        }
    }