1

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?

4
  • 3
    Have you tried inheriting the class and overiding the postEmail method? Commented May 24, 2019 at 14:13
  • The code is from a trait, should I extend the class that uses that trait? The class PasswordController use use Illuminate\Foundation\Auth\ResetsPasswords; and which is a trait, the method postEmail()is there Commented May 24, 2019 at 15:05
  • 1
    If some endpoint is calling this function post Email, one way would be to override this function as you wish and then define this route in your routes file with your overriden method. Commented May 24, 2019 at 15:09
  • 1
    The trait "add" methods to the class kinda like inheritance. You can override postEmail by creating your own postEmail method. When the route will call the action ResetPasswordController@postEmail (or whatever was is name on 5.1), it will call your instead of vendor. Commented May 24, 2019 at 15:18

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

It's not only the route, it's also the parameters of with that I need to change. Teh default has only status and it's string, I need more parameters (an array of them)
Once you are in the login controller action, you can add to your session other parameters. Something like (it's pseudo-code): if (session()->has('status')) { session()->put('message', 'your message'); }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.