Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel 5.8中扩展或创建自定义PasswordBroker sendResetLink()方法?_Php_Laravel_Laravel 5_Laravel 5.8 - Fatal编程技术网

Php 如何在Laravel 5.8中扩展或创建自定义PasswordBroker sendResetLink()方法?

Php 如何在Laravel 5.8中扩展或创建自定义PasswordBroker sendResetLink()方法?,php,laravel,laravel-5,laravel-5.8,Php,Laravel,Laravel 5,Laravel 5.8,目前,重置密码背后的逻辑是用户必须提供有效/注册的电子邮件才能接收密码恢复电子邮件 在我的情况下,出于安全考虑,我不想验证电子邮件是否已注册,我只想在后端执行签入并告诉用户“如果他已提供注册电子邮件,他应该很快收到恢复电子邮件” 为了实现这一点,我在vendor\laravel\framework\src\illumb\Auth\PasswordBroker\PasswordBroker.phpsendResetLink()方法中编辑了以下内容: /** *向用户发送密码重置链接。 * *@pa

目前,重置密码背后的逻辑是用户必须提供有效/注册的电子邮件才能接收密码恢复电子邮件

在我的情况下,出于安全考虑,我不想验证电子邮件是否已注册,我只想在后端执行签入并告诉用户“如果他已提供注册电子邮件,他应该很快收到恢复电子邮件”

为了实现这一点,我在
vendor\laravel\framework\src\illumb\Auth\PasswordBroker\PasswordBroker.php
sendResetLink()
方法中编辑了以下内容:

/**
*向用户发送密码重置链接。
*
*@param数组$credentials
*@返回字符串
*/
公共函数sendResetLink(数组$credentials)
{
//首先,我们将检查是否在给定的凭据和
//如果没有,我们将使用一段
//会话中的“闪存”数据,以向开发人员指示错误。
$user=$this->getUser($credentials);
if(为空($user)){
返回静态::无效的用户;
}
//一旦我们有了重置令牌,我们就准备好将消息发送到该服务器
//使用链接重置密码的用户。然后我们将重定向回
//会话中未设置任何指示错误的当前URI。
$user->sendPasswordResetNotification(
$this->tokens->create($user)
);
返回静态::重置链接发送;
}
为此:

/**
*向用户发送密码重置链接。
*
*@param数组$credentials
*@返回字符串
*/
公共函数sendResetLink(数组$credentials)
{
//首先,我们将检查是否在给定的凭据和
//如果没有,我们将使用一段
//会话中的“闪存”数据,以向开发人员指示错误。
$user=$this->getUser($credentials);
//if(为空($user)){
//返回静态::无效的用户;
//        }
//一旦我们有了重置令牌,我们就准备好将消息发送到该服务器
//使用链接重置密码的用户。然后我们将重定向回
//会话中未设置任何指示错误的当前URI。
如果(!为null($user)){
$user->sendPasswordResetNotification(
$this->tokens->create($user)
);
}
返回静态::重置链接发送;
}
此硬编码选项不是最佳解决方案,因为它将在更新后消失。。因此我想知道如何在
App
文件夹中的项目范围内扩展或实施此更改,以便始终保留此更改

另外,我尝试过这里提到的解决方案:但它不起作用。。目录树也不一样,我不知道把新的
PasswordBroker.php
文件放在哪里


提前谢谢

以下是您需要遵循的步骤

创建新的自定义
密码重置服务提供程序
。我有一个名为
Extensions
的文件夹(名称空间),我将在其中放置此文件:

<?php

namespace App\Extensions\Passwords;

use Illuminate\Auth\Passwords\PasswordResetServiceProvider as BasePasswordResetServiceProvider;

class PasswordResetServiceProvider extends BasePasswordResetServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerPasswordBroker();
    }

    /**
     * Register the password broker instance.
     *
     * @return void
     */
    protected function registerPasswordBroker()
    {
        $this->app->singleton('auth.password', function ($app) {
            return new PasswordBrokerManager($app);
        });

        $this->app->bind('auth.password.broker', function ($app) {
            return $app->make('auth.password')->broker();
        });
    }
}
同样,此PasswordBrokerManager从laravel扩展了基本管理器。这里唯一的区别是新的resolve方法,它从同一名称空间返回一个新的自定义
PasswordBroker
。因此,最后一个文件我们将在同一名称空间中创建一个自定义的
PasswordBroker

<?php

namespace App\Extensions\Passwords;

use Illuminate\Auth\Passwords\PasswordBrokerManager as BasePasswordBrokerManager;

class PasswordBrokerManager extends BasePasswordBrokerManager
{
    /**
     * Resolve the given broker.
     *
     * @param  string  $name
     * @return \Illuminate\Contracts\Auth\PasswordBroker
     *
     * @throws \InvalidArgumentException
     */
    protected function resolve($name)
    {
        $config = $this->getConfig($name);

        if (is_null($config)) {
            throw new InvalidArgumentException(
                "Password resetter [{$name}] is not defined."
            );
        }

        // The password broker uses a token repository to validate tokens and send user
        // password e-mails, as well as validating that password reset process as an
        // aggregate service of sorts providing a convenient interface for resets.
        return new PasswordBroker(
            $this->createTokenRepository($config),
            $this->app['auth']->createUserProvider($config['provider'] ?? null)
        );
    }
}
<?php

namespace App\Extensions\Passwords;

use Illuminate\Auth\Passwords\PasswordBroker as BasePasswordBroker;

class PasswordBroker extends BasePasswordBroker
{
 /**
     * Send a password reset link to a user.
     *
     * @param  array  $credentials
     * @return string
     */
    public function sendResetLink(array $credentials)
    {
        // First we will check to see if we found a user at the given credentials and
        // if we did not we will redirect back to this current URI with a piece of
        // "flash" data in the session to indicate to the developers the errors.
        $user = $this->getUser($credentials);

//        if (is_null($user)) {
//            return static::INVALID_USER;
//        }

        // Once we have the reset token, we are ready to send the message out to this
        // user with a link to reset their password. We will then redirect back to
        // the current URI having nothing set in the session to indicate errors.
        if(!is_null($user)) {
            $user->sendPasswordResetNotification(
                $this->tokens->create($user)
            );
        }

        return static::RESET_LINK_SENT;
    }
}

这就是注册自定义密码代理所需的全部内容。希望这会有所帮助。

这里最简单的解决方案是将自定义代码放在
app\Http\Controllers\Auth\ForgotPasswordController
中-这是一个控制器,它将
SendsPasswordResetEmails
特性拉入

您的方法将覆盖该trait提供的方法,因此将调用该方法而不是trait中的方法。您可以使用代码覆盖整个
sendResetLinkEmail
方法,以始终返回相同的响应,而不管成功与否

公共函数sendResetLinkEmail(请求$Request)
{
$this->validateEmail($request);
//我们将向该用户发送密码重置链接。一旦我们尝试
//要发送链接,我们将检查响应,然后查看我们收到的消息
//需要向用户显示。最后,我们将发送正确的响应。
$response=$this->broker()->sendResetLink(
$request->only('email')
);
return back()->带有('status',“如果您提供了注册电子邮件,您应该很快就会收到恢复电子邮件。”);
}

您可以在
ForgetPasswordController
类中重写
sendResetLinkFailedResponse
方法

protected function sendResetLinkFailedResponse(Request $request, $response)
{
    return $this->sendResetLinkResponse($request, Password::RESET_LINK_SENT);
}

即使验证失败,我们也会发送成功的响应。

这与上面的解决方案类似,但确实有效。还是太乱了,但这比我的好。非常感谢。我会继续寻找更简单的解决方案。谢谢!您已经写了一个清晰、简洁但完全编码的描述,说明了如何做到这一点。我有一个自定义代理,它正在根据API重置密码。在Laravel 5.6中工作良好。这正是我所寻找的。谢谢你,德怀特
protected function sendResetLinkFailedResponse(Request $request, $response)
{
    return $this->sendResetLinkResponse($request, Password::RESET_LINK_SENT);
}