简体   繁体   中英

Laravel 5.2 password reset returns the fatal error: “Call to a member function getEmailForPasswordReset() on null”

There is a very similar question , but I couldn't comment on it or follow the question author's advice to fix my problem.

Password reset was already set up and worked, I got back to it to check translation strings, and started getting this error: Fatal error: Call to a member function getEmailForPasswordReset() on null (View: /home/vagrant/code/ecc/resources/views/emails/password.blade.php)

I didn't use source control for the project, so I couldn't find out when exactly it stopped working. The only things that I changed in my password controller, were getSendResetLinkEmailSuccessResponse() and getSendResetLinkEmailFailureResponse() to AJAXify my forms. Almost the same exact code runs in another project, but on the same Homestead, and it works. I checked the whole chain of calls, and unsurprisingly, there's nothing wrong with it—a user object becomes null somewhere during view rendering:

8. at Mailer->getView('emails.password', array('token' => 'd81d3190958330e2a4e0552db07a1efc80d1768eddde0447f8efb9e588719ff4', 'user' => object(User), 'message' => object(Message))) in Mailer.php line 323

4. at CompilerEngine->get('/home/vagrant/code/ecc/resources/views/emails/password.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'token' => 'd81d3190958330e2a4e0552db07a1efc80d1768eddde0447f8efb9e588719ff4', 'user' => null, 'message' => object(Message), 'locale' => 'en')) in View.php line 149

3. at PhpEngine->evaluatePath('/home/vagrant/code/ecc/storage/framework/views/469f1ba57eac0cc812c6c63e33641df67f64c100.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'token' => 'd81d3190958330e2a4e0552db07a1efc80d1768eddde0447f8efb9e588719ff4', 'user' => null, 'message' => object(Message), 'locale' => 'en')) in CompilerEngine.php line 59

So, Mailer gets a user as a retreived object, but then it becomes null during view rendering. I tried reinstalling everything, vagrant destroy , dump-autoload , clear-compiled , removing all compiled views, I'm also not using Laravel Auto Presenter, or anything similar that tries to automatically wrap the user model in a decorator when it's passed into a view.

Can you please advise something? Thanks!

EDIT: Here's my controller, as requested:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;

class PasswordController extends Controller
{
    use ResetsPasswords;


    protected $subject;

    protected $redirectTo = '/';

    /**
     * Create a new password controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');

        $this->subject = trans('emails.password_reset.subject');
    }

    /**
     * Display the password reset view for the given token.
     *
     * If no token is present, display the link request form.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  string|null  $token
     * @return \Illuminate\Http\Response
     */
    public function showResetForm($request, $token = null)
    {
        if (is_null($token)) {
            abort(403, 'Unauthorized action.');
        }

        $email = $request->input('email');

        return view('reset')->with(compact('token', 'email'));
    }

    /**
     * Get the response for after the reset link has been successfully sent.
     *
     * @param  string  $response
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function getSendResetLinkEmailSuccessResponse($response)
    {
        return response()->json([
            'success' => true,
        ]);
    }

    /**
     * Get the response for after the reset link could not be sent.
     *
     * @param  string  $response
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function getSendResetLinkEmailFailureResponse($response)
    {
        return response()->json([
            'success' => false,
        ]);
    }
}

I use a view composer class to append a couple of variables to all views, one of them being $user that stores Auth::user() . Needless to say, this variable—which is obviously null, because a user is not logged in when it's returned during password reset—overwrites a $user variable that Mailer passes further down the chain.

The solution is to either rename a $user variable in a view composer, use a custom password broker to overwrite emailResetLink() and change the variable name there, or pass an array of specific views in ComposerServiceProvider instead of a “*” as a wildcard.

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