Ad

Laravel ResetsPasswords Trait

- 1 answer

Locally, I have updated this trait to do some different redirecting after the user submits the getEmail() method to request the reset password link. When pushed to production, my editions aren't there. I'm guessing this is because the ResetsPasswords trait is in the laravel framework which is installed separately from my repository on the server.

If this is the case, what's the best way to change how this ResetsPasswords trait functions. Do I make my own and include that in the repository and just change my controller? Below is the PasswordController.

Thanks!

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class PasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
    */
    use ResetsPasswords;

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

Update: So, in the ResetsPasswords trait, I modify the redirect getSendResetLinkEmailSuccessResponse() method. So, do I instead just put that method in my controller (with the same name) and my edited code?

protected function getSendResetLinkEmailSuccessResponse($response)
{
    ...modified code...
}
Ad

Answer

You should not be making changes to the Laravel vendor files for the reason you stated.

Instead, you should override any of the trait functions you need to modify in your controller.

So just add the method to your controller with your modified code like so:

<?php 
namespace App\Http\Controllers;

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

class PasswordController extends Controller {
    use ResetsPasswords;

    protected $redirectPath = '/main';

    public function __construct() {
        $this->middleware('guest');
    }

    protected function getSendResetLinkEmailSuccessResponse($response) {
        // modified code that sends an awesome flash message
    }
}

Also, if all you're trying to do is change where the user is redirected to then you don't have to override the functions at all. All you have to do is change the redirectPath property in your controller.

Ad
source: stackoverflow.com
Ad