Laravel:POST函数中未定义的变量


Laravel : Undefined variable in a POST function

>我正在尝试发送一个包含变量的post请求,但它说变量未定义,这是我的代码..第一个函数是包含窗体的视图。第二个函数是表单的 post 请求的回调。我在视图中回显了变量,它成功了,但错误出现在第二个函数中......

public function reset($code)
{
    return View::make('recover', array('code' => $code, 'passwordReset' => 1,'noLoginForm' => 1));
}
public function postResetPassword()
{
    $validator = Validator::make(Input::all(), array('temppassword' => 'required','newpassword' => 'required|min:6','cnewpassword' =>'required|same:newpassword'));
    if($validator->fails())
        return View::make('recover', array('passwordReset' => 1,'noLoginForm' => 1))->withErrors($validator);
    else {
// The error shows up in the following line in the code variable comparison
        $user = User::where('temp_password','!=','')->where('code','=',$code)->first();
        if($user) {
            $user->password = Hash::make(Input::get('newpassword'));
            $user->temp_password = '';
            if($user->save())
                return Redirect::route('login')->with('msg','Password changed correctly, use your new password to login.');
        } else {
            return View::make('recover', array('passwordReset' => 1,'noLoginForm' => 1,'error' =>'Invalid temporary password, please make sure you typed the password sent to you correctly.'));
        }
    }
}

这是错误日志

[2014-11-13 09:50:15] production.ERROR: exception 'ErrorException' with message 'Undefined variable: code' in C:'wamp'www'buddyly'app'controllers'UserController.php:155
Stack trace:
#0 C:'wamp'www'buddyly'app'controllers'UserController.php(155): Illuminate'Exception'Handler->handleError(8, 'Undefined varia...', 'C:'wamp'www'bud...', 155, Array)
#1 [internal function]: UserController->postResetPassword()
#2 C:'wamp'www'buddyly'vendor'laravel'framework'src'Illuminate'Routing'Controller.php(231): call_user_func_array(Array, Array)
#3 C:'wamp'www'buddyly'bootstrap'compiled.php(5776): Illuminate'Routing'Controller->callAction('postResetPasswo...', Array)
#4 C:'wamp'www'buddyly'bootstrap'compiled.php(5764): Illuminate'Routing'ControllerDispatcher->call(Object(UserController), Object(Illuminate'Routing'Route), 'postResetPasswo...')
#5 C:'wamp'www'buddyly'bootstrap'compiled.php(4971): Illuminate'Routing'ControllerDispatcher->dispatch(Object(Illuminate'Routing'Route), Object(Illuminate'Http'Request), 'UserController', 'postResetPasswo...')
#6 [internal function]: Illuminate'Routing'Router->Illuminate'Routing'{closure}()
#7 C:'wamp'www'buddyly'bootstrap'compiled.php(5329): call_user_func_array(Object(Closure), Array)
#8 C:'wamp'www'buddyly'bootstrap'compiled.php(4996): Illuminate'Routing'Route->run(Object(Illuminate'Http'Request))
#9 C:'wamp'www'buddyly'bootstrap'compiled.php(4984): Illuminate'Routing'Router->dispatchToRoute(Object(Illuminate'Http'Request))
#10 C:'wamp'www'buddyly'bootstrap'compiled.php(715): Illuminate'Routing'Router->dispatch(Object(Illuminate'Http'Request))
#11 C:'wamp'www'buddyly'bootstrap'compiled.php(696): Illuminate'Foundation'Application->dispatch(Object(Illuminate'Http'Request))
#12 C:'wamp'www'buddyly'bootstrap'compiled.php(7744): Illuminate'Foundation'Application->handle(Object(Illuminate'Http'Request), 1, true)
#13 C:'wamp'www'buddyly'bootstrap'compiled.php(8351): Illuminate'Session'Middleware->handle(Object(Illuminate'Http'Request), 1, true)
#14 C:'wamp'www'buddyly'bootstrap'compiled.php(8298): Illuminate'Cookie'Queue->handle(Object(Illuminate'Http'Request), 1, true)
#15 C:'wamp'www'buddyly'bootstrap'compiled.php(10957): Illuminate'Cookie'Guard->handle(Object(Illuminate'Http'Request), 1, true)
#16 C:'wamp'www'buddyly'bootstrap'compiled.php(657): Stack'StackedHttpKernel->handle(Object(Illuminate'Http'Request))
#17 C:'wamp'www'buddyly'public'index.php(49): Illuminate'Foundation'Application->run()
#18 C:'wamp'www'buddyly'server.php(19): require_once('C:'wamp'www'bud...')
#19 {main} [] []

由于$code不是在该函数中定义的,也不是由参数传递给它的,因此它不存在。因此,您会收到"未定义的变量"错误。

如果您将$code发送到视图,并希望在视图发布表单时取回$code,则将 $code 的值添加到表单元素并使用 Input::get() 读取它。

视图:

  <!-- inside your form -->
  <input type="hidden" name="code" value="{{ $code }}" />

型号/控制器:

$code = Input::get('code');

或者,您可以创建一个包含 $code 的会话,并在调用方法 postResetPassword() 后读取该值。