覆盖核心Laravel 4类方法

Rus*_*ack 5 laravel laravel-4

抱歉,如果这是一个愚蠢的问题,因为很多Laravel 4对我来说都是新手.我试图覆盖核心密码功能中的几个方法,因为我想定义自己的密码验证规则(在发布时硬编码到核心),并更改错误报告的方法($ errors数组用于其他形式,而不是基于会话的).

所以我的方法是在/ app/lib/MyProject/User中创建一个名为Password.php的新类,如下所示:

<?php namespace MyProject\User;

use Closure;
use Illuminate\Mail\Mailer;
use Illuminate\Routing\Redirector;
use Illuminate\Auth\UserProviderInterface;

    class Password extends \Illuminate\Support\Facades\Password
    {
        /**
     * Reset the password for the given token.
     *
     * @param  array    $credentials
     * @param  Closure  $callback
 * @return mixed
 */
public function reset(array $credentials, Closure $callback)
{
    // If the responses from the validate method is not a user instance, we will
    // assume that it is a redirect and simply return it from this method and
    // the user is properly redirected having an error message on the post.
    $user = $this->validateReset($credentials);

    if ( ! $user instanceof RemindableInterface)
    {
        return $user;
    }

    $pass = $this->getPassword();

    // Once we have called this callback, we will remove this token row from the
    // table and return the response from this callback so the user gets sent
    // to the destination given by the developers from the callback return.
    $response = call_user_func($callback, $user, $pass);

    $this->reminders->delete($this->getToken());

    return $response;
}

}
Run Code Online (Sandbox Code Playgroud)

我已经从/vendor/laravel/framework/src/Illuminate/Auth/Reminders/PasswordBroker.php复制了重置方法,这似乎是核心密码门面解析的地方.

然后在我的composer.json文件中,我将以下内容添加到autoload:classmap数组中:

"app/lib/MyProject/User"
Run Code Online (Sandbox Code Playgroud)

最后,在我的/app/config/app.php文件中,我修改了密码别名:

'Password' => 'MyProject\User\Password',
Run Code Online (Sandbox Code Playgroud)

好.在我的routes.php文件中,我有以下几个直接来自文档:

Route::post('password/reset/{token}', function()
{
    $credentials = array('email' => Input::get('email'));

    return Password::reset($credentials, function($user, $password)
    {
        $user->password = Hash::make($password);

        $user->save();

        return 'saved - login';
    });
});
Run Code Online (Sandbox Code Playgroud)

当这个reset()方法运行时,我收到以下错误:

不应静态调用非静态方法MyProject\User\Password :: reset()

我扩展的类中的reset()方法不是静态的,所以让我感到惊讶,但是将我的reset方法设置为static会清除该错误.接下来,我收到以下错误:

不在对象上下文中时使用$ this

尝试运行$ this-> validateReset($ credentials)时会出现这种情况.

我现在完全超出了我的深度.我是以正确的方式走这条路还是走完正确的道路?

谢谢你的建议

Mig*_*ges 2

您应该扩展Illuminate\Auth\Reminders\PasswordBroker类,而不是\Illuminate\Support\Facades\Password.

该类\Illuminate\Support\Facades\Password是一个 Facade,而不是最终类。

您可以通过以下方式查看立面的最终类别:

get_class(Password::getFacadeRoot());
Run Code Online (Sandbox Code Playgroud)