简体   繁体   English

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

[英]Laravel : Undefined variable in a POST function

I'm trying to send a post request containing a variable, but it says that the variable is undefined, here is my code .. The first function is the view containing the form. 我正在尝试发送包含变量的后请求,但它说该变量未定义,这是我的代码..第一个功能是包含表单的视图。 The second function is the callback of the post request of the form. 第二个函数是表单的发布请求的回调。 I echo'ed out the variable in the view and it succeeded but the error shows up in the second function .. 我在视图中回显了变量,它成功了,但是第二个函数中显示了错误。

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.'));
        }
    }
}

Here is the error log 这是错误日志

[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} [] []

Since $code isn't defined in that function or handed to it by a parameter, it doesn't exist. 由于$code未在该函数中定义或未通过参数传递给它,因此它不存在。 Hence the 'undefined variable' error you are getting. 因此,您会收到“未定义的变量”错误。

If you send $code to the view, and want get $code back when the view posts a form, then add the value of $code to a form-element and read it with Input::get() . 如果将$code发送到视图,并希望在视图发布表单时取回$code ,则将$code的值添加到表单元素中,并使用Input::get()读取。

View: 视图:

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

Model/Controller: 型号/控制器:

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

Alternatively, you could create a session containing $code and read the value once the method postResetPassword() is called. 另外,您可以创建一个包含$code的会话,并在调用postResetPassword()方法后读取该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM