覆盖Laravel控制器中的特征方法

[英]Overriding a trait method in Laravel controller


I cannot override a method of a trait in my Controller class. Here is an example of my trait method:

我无法在Controller类中覆盖特征的方法。这是我的特质方法的一个例子:

protected function resetPassword($user, $password)
{
    $user->password = bcrypt($password);

    $user->save();

    Auth::login($user);
}

And here is my Controller:

这是我的控制器:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\ResetsPasswords;


class PasswordController extends Controller
{
    use ResetsPasswords {
        resetPassword as newReset;
    }

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

    protected function resetPassword($user, $password)
    {

    }
}

I found one example, but I am not sure that I am on a right way.

我找到了一个例子,但我不确定我是否正确。

1 个解决方案

#1


1  

As you have already done it yourself, you need to alias framework trait's resetPassword method and then implement your own:

正如您自己已经完成的那样,您需要为框架特征的resetPassword方法添加别名,然后实现自己的:

namespace App\Http\Controllers\Auth;

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

class PasswordController extends Controller
{
    use ResetsPasswords {
        resetPassword as frameworkResetPassword;
    }

    protected function resetPassword($user, $password)
    {
        // Do your custom password reset logic here
        $user->password = bcrypt($password);
        $user->save();
        Auth::login($user);

        // Then you can pass the work over to framework's implementation
        // if that's needed.
        // $this->frameworkResetPassword($user, $password);
    }
}

By the way, Laravel's own password reset functionality supports logging users in.

顺便说一句,Laravel自己的密码重置功能支持登录用户。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/06/10/d3514784d2482402a621c40de5567c44.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号