CodeIgniter 中的可选参数

发布于 2024-08-17 03:45:15 字数 147 浏览 1 评论 0原文

我正在尝试在 CodeIgniter 控制器中编写一个可以接受可选参数的函数。但是,我总是收到缺少参数警告。我并不是试图抑制警告 - 我试图将参数声明为可选(如果它们不存在的话,也许它们可能是空字符串,或者其他)。

我缺少什么?

谢谢
马拉

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生未歇 2024-08-24 03:45:15
  public function my_optional_test($not_optional_param, $optional_param = NULL)
  { $this->stuff(); }

你试过这个吗?

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

have you tried this?

微凉 2024-08-24 03:45:15

例如,假设您有一个如下所示的 URI:

  1. example.com/index.php/mycontroller/myfunction/hello/world
  2. example.com/index.php/mycontroller/myfunction/hello

您的方法将传递 URI 段 3 和 4 (“hello”和“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.
}

}

参考:https://codeigniter .com/user_guide/general/controllers.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文