Laravel重置电子邮件重定向 [英] Laravel reset email redirection

查看:90
本文介绍了Laravel重置电子邮件重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一页来处理登录表单,注册表单和密码重置表单.

In my project I have one page to handle login form, register form and password reset form.

对于前两个,一切正常.最后,我找到了一种方法来发送自己的电子邮件,而不是laravel的默认电子邮件,但现在我想在成功时停止重定向.

For the first two, everything work well. For the last, I find a way to send my own email instead of laravel's default email but now I want to stop redirect on success.

对于这个故事,当我单击提交"时,我会发出一个甜蜜的通知,用Ajax调用reset控制器并等待响应.

For the story, I launch a sweetalert notification when I click on submit, call reset controller with Ajax and wait for response.

但是,页面会在成功加载时重新加载.我找不到阻止重定向的方法,只是将json发送到我的Ajax.

But instead, page is reload on success. I don't find a way to prevent redirect and just send json to my Ajax.

我的login.js:

My login.js :

$(".m_login_forget_password_submit").click(function(l){

    var t=$(this),r=$(this).closest("form");
    t.addClass("m-loader m-loader--right m-loader--light");

    swal({
        title:"Mot de passe",
        text:"Génération du lien.",
        type:"info"
    });
        var $this = $(this);
        $.ajax({
            type: 'POST',
            url: 'password/request',
            data: $this.serializeArray(),
            success:function(data){
                $("#m_login_signup_submit").toggleClass("m-loader m-loader--right m-loader--light");
                swal({
                    title:"Succès",
                    text:"Votre demande de compte a été transmise à nos équipe.",
                    type:"success"
                });

            },
            error: function(data){
                $("#m_login_signup_submit").toggleClass("m-loader m-loader--right m-loader--light");
                if( data.status === 422 ) {
                    var error = "Les erreurs suivantes ont été trouvées :";
                    var errors = data.responseJSON;
                    $.each( errors, function( key, value ) {
                        error += '<br>&#9656 ' + value[0];
                    });
                    swal("Erreur",error,"error")
                }
            }
        });

我的自定义ResetPasswordController:

My custom ResetPasswordController :

class ResetPasswordController extends Controller

{

use ResetsPasswords;

/**
 * Where to redirect users after resetting their password.
 *
 * @var string
 */
protected $redirectTo = '/home';

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

/**
 * Get the response for a successful password reset.
 *
 * @param  string  $response
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
 */
protected function sendResetResponse($response)
{
    return 'ok';
}


    }

有人知道如何防止在发送电子邮件时重置重定向密码成功并发送json吗?

Someone know how to prevent redirect on send email reset password success and send json instead ?

感谢您的帮助.

推荐答案

重置密码时,reset函数将在您的ResetPasswordController中调用,因此您必须将sendResetResponse函数重命名为reset(因为您要覆盖特征函数),然后将默认特征函数指向未使用的函数.另外,如果您想要JSON响应,则需要在返回值中指定它.

When resetting a password, the reset function will be called in your ResetPasswordController, so you'll have to rename your sendResetResponse function to reset (since you want to override the trait function) and point the default trait function to an unused function. Plus, if you want a JSON response, you'll need to specify that with the return.

示例:

class ResetPasswordController extends Controller
{
    use ResetsPasswords {
        reset as unused;
    }

    ...

    //Override the trait function
    public function reset(Request $request) {
        //Call default validation and handle password change

        $this->validate($request, $this->rules(), $this->validationErrorMessages());

        // Here we will attempt to reset the user's password. If it is successful we
        // will update the password on an actual user model and persist it to the
        // database. Otherwise we will parse the error and return the response.
        $response = $this->broker()->reset(
            $this->credentials($request), function ($user, $password) {
                $this->resetPassword($user, $password);
            }
        );

        //Return the response
        if ($response == Password::PASSWORD_RESET) {
            return response()->json(array(
                'result' => 'ok',
            ), 200);
        } else {
            return response()->json(array(
                'result' => 'fail',
            ), 500);
        }
    }
}

很抱歉,以为您是在谈论重置密码重定向.要在发送电子邮件后处理重定向,您将采用类似的方法并修改您的ForgotPasswordController并覆盖sendResetLinkEmail函数:

Sorry about that, thought you were talking about the reset password redirection. To handle the redirect after sending the email, you will take a similar approach and modify your ForgotPasswordController and override the sendResetLinkEmail function instead:

class ForgotPasswordController extends Controller
{
    use SendsPasswordResetEmails {
        sendResetLinkEmail as unused;
    }

    ...

    public function sendResetLinkEmail(Request $request)
    {
        $this->validateEmail($request);

        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );

        //Return the response
        if ($response == Password::RESET_LINK_SENT) {
            return response()->json(array(
                'result' => 'ok',
            ), 200);
        } else {
            return response()->json(array(
                'result' => 'fail',
            ), 500);
    }
    }
}

这篇关于Laravel重置电子邮件重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆