optional parameters in CodeIgniter


Question

I'm trying to write a function in a CodeIgniter controller which can take optional parameters. However, I always get Missing Argument warnings. I'm not trying to suppress the warnings - I'm trying to declare the parameters as optional (maybe they could be empty strings if they don't exist, or something).

What is it I'm missing?

Thanks
Mala

1
7
1/6/2010 3:19:08 PM

Accepted Answer

  public function my_optional_test($not_optional_param, $optional_param = NULL)
  { $this->stuff(); }

have you tried this?

20
1/6/2010 3:18:03 PM

For example, let’s say you have a URI like this:

  1. example.com/index.php/mycontroller/myfunction/hello/world
  2. 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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon