简体   繁体   中英

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. 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() .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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