Laravel 控制器认为我的函数未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23664822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel controller thinks my function is undefined
提问by Vince
Pretty simple. I'm trying to call a function from within my controller. The function is, in fact, defined. Yet, when the function is called I get "PHP Fatal Error: Call to undefined function validate() ..."
很简单。我正在尝试从我的控制器中调用一个函数。实际上,该函数已定义。然而,当该函数被调用时,我得到“PHP 致命错误:调用未定义的函数 validate() ...”
Here's my code. Any ideas? Thanks.
这是我的代码。有任何想法吗?谢谢。
<?php
class HomeController extends BaseController {
/**
* Controller for the index action of the home page. Displays the landing page.
*/
public function index()
{
return View::make('landing', array('success' => false));
}
/**
* Controller to handle processing the contact form and re-displaying the landing page
*/
public function processForm()
{
$form_array = array();
$errors = array();
foreach (array('email','fname','lname','message','newsletter') as $val)
{
if (isset($_POST[$val]))
$form_array[$val] = $_POST[$val];
else
$form_array[$val] = null;
}
$form_ok = validate();
if ($form_ok)
{
echo "GOOD!";
}
else
{
echo "BAD!";
}
}
/**
* Helper function for validating the form. Returns true if the form was
* submitted without errors.
*/
public function validate()
{
return true;
}
}
回答by Jason
It looks like you're trying to call $this->validate()
, instead of validate()
. You've defined validate()
as a class method, not a stand alone function.
看起来您正在尝试调用$this->validate()
,而不是validate()
。您已定义validate()
为类方法,而不是独立的函数。
回答by Wistar
You should try to refer to the actual controller.
您应该尝试参考实际控制人。
Both of these will work.
这两个都会起作用。
$form_ok = self::validate();
or
或者
$this->validate();
回答by shammy
It should be $this->validate as its referring to a method within the class.
它应该是 $this->validate 作为它引用类中的方法。