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

时间:2014-11-13 10:21:18

标签: php variables laravel undefined

我正在尝试发送包含变量的发布请求,但它说该变量未定义,这是我的代码.. 第一个函数是包含表单的视图。 第二个函数是表单的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} [] []

1 个答案:

答案 0 :(得分:0)

由于$code未在该函数中定义或由参数传递给它,因此它不存在。因此,未定义的变量'你得到的错误。

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

查看:

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

模型/控制器:

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

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