【问题标题】:Warning in php URL routing using codeigniter使用 codeigniter 的 php URL 路由中的警告
【发布时间】:2016-10-25 13:27:10
【问题描述】:

我是 CodeIgniter 的新手,我正在尝试一些练习代码。在那,我创建了一个带有 2 个参数的函数,$p1$p2。我的函数也使用默认参数。现在我试图在代码中给出一个参数,在 URL 中给出另一个参数,这里是$p2="Nimishan"

<?php
class Hello extends CI_Controller{
    public function first($p1,$p2="Nimishan"){
        echo "This is the first method<br>";
        echo "My parameters are : $p1 and $p2";
    }
}
?>

我的网址是: localhost/code/index.php/hello/first/hey 所以输出是

嘿,尼米山

但是当我将默认参数值更改为$p1 时收到警告。

<?php
class Hello extends CI_Controller{
public function first($p1="Hello",$p2){
    echo "This is the first method<br>";
    echo "My parameters are : $p1 and $p2";
    }
}
?>

我尝试了 URL 以通过不同方式获得正确的输出。

例如:localhost/code/index.php/hello/first/nimishan

但我收到警告

Severity: Warning
Message: Missing argument 2 for Hello::first()

Severity: Notice
Message: Undefined variable: p2

我不确定我错过了哪里。

我想要的是通过为第一个参数而不是第二个和正确的 URL 方式提供值来获得正确的输出。

【问题讨论】:

  • 您必须使用 codeigniter 中的段来从 url 获取路由的值,您对待控制器并将参数放入其中的方式,这是错误的。使用 $this->uri->segment();内部控制器并从函数中删除参数。 url 内的所有数据都在段内。
  • 无关:我在 stackoverflow.com/review/triage/20858659 上看到了您对 EDIT 的投票。错误的选择:这个问题太宽泛了,除了 OP 之外的任何人都没有机会解决这个问题。它应该因为太宽泛而被关闭。请研究分流队列的帮助,并更加小心您的投票。并且:您所做的编辑也是“错误的”。我们不编辑此类帖子,我们关闭投票,如果有的话,我们向提问者发表评论并要求他改善他的低质量输入。

标签: php codeigniter url


【解决方案1】:

我认为你正在尝试做这样的事情 -

public function first() {
    $p1 = $this->uri->segment(1);
    $p2 = $this->uri->segment(2);
    echo "This is the first method<br>";
    echo "My parameters are : $p1 and $p2";
    }
}

如果你的网址是http://localhost/code/index.php/hello/first/welcome/nimishan this->uri->segment(1) 将是“欢迎”,segment(2) 是“nimishan”等等。

如果您的 URL 是 `http://localhost/code/index.php/hello/first/,$p1 和 $p2 都将为 NULL。

如果您需要传递默认值,您可以提供默认值,例如

$p1 = $this->uri->segment(1, 'Hello');
$p2 = $this->uri->segment(2, 'Nimishan');

【讨论】:

  • 我想给代码中的第一个参数 p1 和 URL 中的下一个参数。问题是How?
【解决方案2】:

这是有效的。

public function first($p1,$p2="Nimishan"){}

但以下无效

public function first($p1="Hello",$p2){}

但这不是 Codeigniter 的问题,而是 PHP 的规则。始终必须从最后放置参数的默认值。如果其余参数不需要默认值,那么您可以分配 null 或其他值。所以你可以用作

public function first($p1="Hello",$p2 = NULL){}

【讨论】:

  • public function first($p1="Hello",$p2 = NULL){}如果我使用这个 URL 应该如何?
  • 无论您现在的 URL 是什么,都不会再显示错误/警告。像运行这个localhost/code/index.php/hello/first/nimishan 并检查没有错误anyomre
  • 没有错误,很好。但输出是nimishan , 。我要Hello, nimishan
  • 然后使用public function first($p1="Hello",$p2 = 'nimishan'){} 或者您可以根据需要在方法调用上更改$p1 & $p2
  • 那么您可以从参数中删除$p1 并在方法中设置它。所以现在你的方法可能是 public function first($p2 = 'nimishan'){$p1 = 'Hello';} 现在 URL localhost/code/index.php/hello/first/nimishan 将输出为 Hello, nimishan
【解决方案3】:

打开位于 application\config\config.php 中的 CodeIgniter config.php 文件,请检查下面的 $config 数组值是否配置正确。

$config['permitted_uri_chars'] = ''; // a-z 0-9~%.:_\-
$config['encryption_key'] = ''; //Some unique random string 

【讨论】:

    猜你喜欢
    • 2016-11-12
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多