当我做这样的事情时,我有一个错误:
public function _remap()
{
$firstURISegment = $this->uri->segment(1);
$secondURISegment = $this->uri->segment(2);
if($firstURISegment == "user")
{
if($secondURISegment == "")
{
echo "USER INDEX";
}
else
{
$this->$secondURISegment();
}
}
else
{
echo $firstURISegment;
}
}
当我调用此类www.example.com/user/abcd
未在类中定义的函数时,我收到此错误。
Fatal error: Call to undefined method user::abcd() in C:\xampp\htdocs\livears\application\controllers\user.php on line 17
A PHP Error was encountered
Severity: Error
Message: Call to undefined method user::abcd()
Filename: controllers/user.php
Line Number: 17
Backtrace:
我希望重定向到此类的index()
函数,而不是此错误。我怎么能够 ?
请注意,我已经这样做了:
$route[':any'] = 'user/$1';
在routes.php
中获取www.example.com/username
而不是www.example.com/user/username
。并且由于我的User类有其他函数,我已经重新调用它们,但是当你看到我调用一个未定义的函数时,我得到一个Fatal error
。请帮我重定向。
答案 0 :(得分:0)
首先,控制器的名称应以大写字母
开头您的_remap没有做任何重定向操作。像这样使用它
public function _remap()
{
$firstURISegment = $this->uri->segment(1);
$secondURISegment = $this->uri->segment(2);
if($firstURISegment == "user")
{
if($secondURISegment == "")
{
echo "USER INDEX";
}
else
{
If ( function_exists ($secondURISegment))
{ $this->$secondURISegment();}
else {
redirect('index');
}
}
}
else
{
echo $firstURISegment;
}
}
你明白了吗?现在在你的代码中实现它?