Search code examples
phpfunctioncodeigniterroutesdefault-value

Using a default array when remaping a controller


How to make the controller work same as the following two ways example.com/page/user1 and example.com/page/user1/all

public function _remap($slug = "",$params = array("all")){
    $sections = array("all","texts","photos");

    if ($slug != "" && in_array($params[0],$sections)) {                                                                            
      return $this->router($slug, $params[0]);
    }else{
      echo "something went wrong";
    }
  }

But example.com/page/user1 gives an error (Undefined offset: 0). I need to do this without touching config/routes.php


Solution

  • My quick fix is :

        $params[0] = count($params) == 0 ? "all" : $params[0];
    

    Have you any suggestions ?