Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用codeigniter的php URL路由中的警告_Php_Codeigniter_Url - Fatal编程技术网

使用codeigniter的php URL路由中的警告

使用codeigniter的php URL路由中的警告,php,codeigniter,url,Php,Codeigniter,Url,我是CodeIgniter的新手,我正在尝试一些练习代码。在这里,我创建了一个带有两个参数的函数,$p1和$p2。我的函数也使用默认参数。现在我试图在代码中给出一个参数,在URL中给出另一个参数,这里是$p2=“Nimishan” 我尝试了URL以不同的方式获得正确的输出 例如:localhost/code/index.php/hello/first/nimishan 但我收到了警告 Severity: Warning Message: Missing argument 2 for Hello:

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

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

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

但我收到了警告

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

Severity: Notice
Message: Undefined variable: p2
我不知道我在哪里失踪了


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

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

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

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 = $this->uri->segment(1);
    $p2 = $this->uri->segment(2);
    echo "This is the first method<br>";
    echo "My parameters are : $p1 and $p2";
    }
}

您必须使用codeigniter中的段从url获取路由值,您处理控制器并将参数放入其中的方式是错误的。使用$this->uri->segment();在控制器内部,从函数中删除参数。url中的所有数据都在段内。无关:我看到你投票支持编辑。选择错误:这个问题太宽泛了,除了OP之外,任何人都不可能解决这个问题。它应该关闭,因为太宽了。请研究分流队列的帮助,并更加小心您的投票。而且:你的编辑也是“错误的”。我们不会编辑此类帖子,我们会关闭投票,如果有,我们会向提问者发表评论,要求他改进其低质量的输入。
public function first($p1=“Hello”,$p2=NULL){}
如果我使用这个URL,URL应该是什么样子?无论你现在的URL是什么,都不会再显示错误/警告。像这样运行
localhost/code/index.php/hello/first/nimishan
并检查无错误anyomreno error,很好。但是输出是
nimishan,
。我想要
Hello,nimishan
然后首先使用
public函数($p1=“Hello”,$p2='nimishan'){}
,或者您可以根据需要在方法调用上更改
$p1
$p2
,然后您可以从参数中删除
$p1
,并在方法中设置它。因此,现在您的方法可以是
public function first($p2='nimishan'){$p1='Hello';}
now URL
localhost/code/index.php/Hello/first/nimishan
将输出为
Hello,nimishan
,我想给出代码中的第一个参数p1和URL中的下一个参数。问题是怎么做?
public function first($p1="Hello",$p2 = NULL){}
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";
    }
}
$p1 = $this->uri->segment(1, 'Hello');
$p2 = $this->uri->segment(2, 'Nimishan');