控制器中的方法如果有形参就一定得传参数?
比如我一个控制器中有一个方法<?php
class Hello extends Controller {
function Hello() {
parent::Controller();
}
function index($str1, $str2) {
$str1 = empty($str1) ? "hello" : '';
$str2 = empty($str2) ? "world" : '';
$data = Array('str' => $str1." ".$str2);
$this->load->view('hello',$data);
}
}
?>
View中直接
<?php echo $str ?>
然后通过 http://localhost/hello 访问
虽然可以显示出结果,但是页面最上方会出错误提示
提示说没有参数
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Hello::index()
Filename: controllers/hello.php
Line Number: 7
该怎么写才不会出错误提示?? 这是PHP的基础知识,可以设置默认参数值。如:
function test($myargument = "默认值"){
...
} 这是PHP的基础知识,可以设置默认参数值。如:
function test($myargument = "默认值"){
...
} ...
yinzhili 发表于 2010-2-7 11:13 http://codeigniter.org.cn/forums/images/common/back.gif
不行啊...
即使这样也是得用
http://localhost/hello/index
这样的链接来访问才行
我想要的是
http://localhost/hello
{:3_65:} index 是不用写的,如果你不需要 index.php/hello/index/xxx/yyy 的话,index 是可以省略的,如果不可以就是你的 CI 的问题,因为我这里一直都是没问题的。
对于 URL 分段传参,必须用默认参数,否则就报错,这是规定,不容侵犯。 index 是不用写的,如果你不需要 index.php/hello/index/xxx/yyy 的话,index 是可以省略的,如果不可以就 ...
Hex 发表于 2010-2-7 11:49 http://codeigniter.org.cn/forums/images/common/back.gif
我知道,我的配置没问题
访问http://localhost/hello的话就相当于访问http://localhost/hello/index
但是像上面那样不传参数的话,即使参数默认赋值了也无法用http://localhost/hello来访问,一定要用http://localhost/hello/index才行 {:3_68:}{:3_68:}
原....原来是IE缓存导致的...
我把IE关掉再开就可以了.... -_- 本来就是没问题的东西。。。。。。
不清缓存就调试网站是不良习惯。。。。 -_- 本来就是没问题的东西。。。。。。
不清缓存就调试网站是不良习惯。。。。 ...
Hex 发表于 2010-2-7 14:01 http://codeigniter.org.cn/forums/images/common/back.gif
我都是用Ctrl+F5来刷新进行调试的...
有时候可以...但有时候一定得关掉浏览器再开才行...甚至有时候得清空缓存才行...
页:
[1]