CodeIgniter - Optional parameters
I've found the correct result:
$route['controllername/(:any)/(:any)/(:num)'] = 'ddd/index/$1/$2/$3';$route['controllername/(:any)/(:num)'] = 'ddd/index/$1/null/$2'; // try 'null' or '0' (zero)$route['controllername/(:any)'] = 'ddd/index/$1';
The Index method (inside "ControllerName") should be:
public function Index($uf = '', $slug = '', $pag = 0){ // some code... if (intval($pag) > 0) { // pagination } if (!empty($slug)) { // slug manipulation }}
Hope it helps someone.Thank you all.
public function my_test_function($not_optional_param, $optional_param = NULL) { //do your stuff here }
have you tried this?
For example, let’s say you have a URI like this:
- example.com/index.php/mycontroller/myfunction/hello/world
- example.com/index.php/mycontroller/myfunction/hello
Your method will be passed URI segments 3 and 4 (“hello” and “world”):
class MyController extends CI_Controller {
public function myFunction($notOptional, $optional = NULL){ echo $notOptional; // will return 'hello'. echo $optional; // will return 'world' using the 1st URI and 'NULL' using the 2nd.}
}
Reference: https://codeigniter.com/user_guide/general/controllers.html