php 路由[password.reset]没有在laravel 5.4中定义

ulydmbyx  于 2023-03-07  发布在  PHP
关注(0)|答案(5)|浏览(114)

我的自定义应用程序中出现此错误:

InvalidArgumentException in UrlGenerator.php line 304: 
Route [password.reset] not defined.

我知道laravel提供了一个开箱即用的密码重置功能,但我想写我自己的类和路由。

    • 这是我在web.php中的路线**
// Password reset link request routes...
Route::get('password/email', 'Auth\PasswordController@getResetEmail');
Route::post('password/email', 'Auth\PasswordController@postResetEmail');

// Password reset routes...
Route::get('password/reset/{token}', 'Auth\PasswordController@getReset');
Route::post('password/reset', 'Auth\PasswordController@postReset');
    • 这是我的密码控制器:**
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Foundation\Auth\ResetsPasswords;

class PasswordController extends Controller {


    use ResetsPasswords;

    /**
     * Create a new password controller instance.
     *
     * @param  \Illuminate\Contracts\Auth\Guard  $auth
     * @param  \Illuminate\Contracts\Auth\PasswordBroker  $passwords
     * @return void
     */
    public function __construct(Guard $auth, PasswordBroker $passwords)
    {
        $this->auth = $auth;
        $this->passwords = $passwords;
        $this->middleware('guest');
    }
}
    • 这是我的ResetPasswords.php特性:**
namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

trait ResetsPasswords
{
    use RedirectsUsers;

    /**
     * Display the form to request a password reset link.
     *
     * @return \Illuminate\Http\Response
     */
    public function getResetEmail()
    {
        return view('public.auth.password');
    }

    /**
     * Send a reset link to the given user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postResetEmail(Request $request)
    {
        $this->validate($request, ['email' => 'required|email']);

        $response = Password::sendResetLink($request->only('email'), function (Message $message) {
            $message->subject($this->getEmailSubject());
        });

        switch ($response) {
            case Password::RESET_LINK_SENT:
                return redirect()->back()->with('status', trans($response));

            case Password::INVALID_USER:
                return redirect()->back()->withErrors(['email' => trans($response)]);
        }
    }

    /**
     * Get the e-mail subject line to be used for the reset link email.
     *
     * @return string
     */
    protected function getEmailSubject()
    {
        return property_exists($this, 'subject') ? $this->subject : 'Your Password Reset Link';
    }

    /**
     * Display the password reset view for the given token.
     *
     * @param  string  $token
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getReset($token = null)
    {
        if (is_null($token)) {
            throw new NotFoundHttpException;
        }

        return view('public.auth.reset')->with('token', $token);
    }

    /**
     * Reset the given user's password.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postReset(Request $request)
    {
        $this->validate($request, [
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:6',
        ]);

        $credentials = $request->only(
            'email', 'password', 'password_confirmation', 'token'
        );

        $response = Password::reset($credentials, function ($user, $password) {
            $this->resetPassword($user, $password);
        });

        switch ($response) {
            case Password::PASSWORD_RESET:
                return redirect($this->redirectPath())->with('status', trans($response));

            default:
                return redirect()->back()
                            ->withInput($request->only('email'))
                            ->withErrors(['email' => trans($response)]);
        }
    }

    /**
     * Reset the given user's password.
     *
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
     * @param  string  $password
     * @return void
     */
    protected function resetPassword($user, $password)
    {
        $user->password = bcrypt($password);

        $user->save();

        Auth::login($user);
    }
}

问题是当我按下密码重置表单按钮时,它会导致那个错误。
任何帮助都将不胜感激...

oipij1gg

oipij1gg1#

这些路由需要一个名称。
这里的代码。

// Password reset link request routes...
Route::get('password/email', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.email');
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');

// Password reset routes...
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.request');
Route::post('password/reset', 'Auth\ResetPasswordController@postReset')->name('password.reset');
avkwfej4

avkwfej42#

laravel 5.4中sendResetLink只有一个参数 * 凭证 *
因此,您必须将方法实现更改为:

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

然后,您必须将回调放到User模型(或者您所拥有的模型)中,该模型具有

trait CanResetPassword

此模型必须具有方法:

public function sendPasswordResetNotification($token) {
    // do your callback here
}
gwo2fgha

gwo2fgha3#

途径

{{ route('password.email') }}

返回/password/reset/{token}
和路由

{{ route('password.request') }}

返回/password/reset
不管是谁写的,那天晚上一定很累,但我想现在改变框架已经太晚了

0mkxixxg

0mkxixxg4#

如果重置链接的url中包含localhost,请记住更改. env文件中的APP_URL,然后运行:php工匠配置:清除

ffvjumwh

ffvjumwh5#

使用强化和圣所的API

如果有人在使用API时遇到这种问题,应该使用ResetPassword::createUrlUsing()方法。
AppServiceProvider中添加:

public function boot()
{
    // Password reset link in email template...
    ResetPassword::createUrlUsing(static function ($notifiable, $token) {
       // Url of the fronted app for resetting password...
       return config('app.app_url').'/reset-password/'.$token;
    });
}

相关问题