pmiranda
pmiranda

Reputation: 8420

Laravel, how can I overwrite code from vendor

Laravel 5.1 has this code:

public function postEmail(Request $request)
{
    $this->validate($request, ['email' => 'required|email']);

    $response = Password::sendResetLink($request->only('email'), function (Message $message) {
        $message->subject($this->getEmailSubject());
    });

    switch ($response) {
        case Password::RESET_LINK_SENT:
            return redirect()->back()->with('status', trans($response));/*I HAVE TO CHANGE THIS*/
        case Password::INVALID_USER:
            return redirect()->back()->withErrors(['email' => trans($response)]);
    }
}

That code it's from a trait: app\vendor\laravel\framework\src\Illuminate\Foundation\Auth\ResetsPasswords.php

I need to change the line with the comment with another code:

return redirect()->route('login')->with(['message' => 'Se ha enviado a su email el link del reseteo, por favor verifique.',]);

But it's a vendor file. How can I do this? overwrite the method in another file? where?

Upvotes: 1

Views: 682

Answers (2)

Leonardo Rossi
Leonardo Rossi

Reputation: 3032

The back() function will check the referer header in request, so you can write a middleware that change that to url(route('login')).

If that header is not set, you can call $request->setPreviousUrl(url(route('login')));

So basically your Middleware code can be something like this

public function handle($request, Closure $next) {
    if (/* request is the reset password */) {
        if ($request->headers->has('referer')) {
            $request->headers->set('referer', url(route('login')));
        } else {
            $request->setPreviousUrl(url(route('login')));
        }

    }
}

Upvotes: 0

Flame
Flame

Reputation: 7580

One slightly hacky solution would be to copy the class you need to edit, and place it in somedir/fixed_class.php with the original namespace and class name. Then add to composer.json:

"autoload": {
     "files": ["somedir/fixed_class.php"]
  }

However you're better off to somehow try to extend the class and use your own improved version (or submit a bugfix/feature request for the original composer module).

Upvotes: 1

Related Questions