2017-05-18 22 views
6

Tôi nhận được lỗi này trong ứng dụng tùy chỉnh của tôi:Route [password.reset] không quy định tại laravel 5.4 trong ResetPasswords.php

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

Tôi biết laravel cung cấp một chức năng thiết lập lại mật khẩu ra khỏi hộp nhưng tôi muốn viết lớp và lộ trình của riêng tôi.

Đây là tuyến đường của tôi trong web.php

// Password reset link request routes... 
Route::get('password/email', 'Auth\[email protected]'); 
Route::post('password/email', 'Auth\[email protected]'); 

// Password reset routes... 
Route::get('password/reset/{token}', 'Auth\[email protected]'); 
Route::post('password/reset', 'Auth\[email protected]'); 

Và đây là PasswordController tôi:

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'); 
    } 
} 

Đây là ResetPasswords.php đặc điểm của tôi:

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); 
    } 
} 

Vấn đề là khi tôi nhấn nút đặt lại mật khẩu, nó dẫn đến lỗi đó. Tôi không biết vấn đề là gì nhưng có vẻ như phpstorm không thể nhận ra mật khẩu :: phương pháp sendResetLink trong lớp PasswordBroker và nêu bật những phương pháp với màu vàng và cho thấy thông điệp bong bóng này:

Method sendResetLink not found in Illuminate\Support\Facades\Password 
Refreced method is not found in subject class 

tôi không biết mối quan hệ giữa phương pháp đó và các tuyến đường là gì?

Bất kỳ trợ giúp sẽ được đánh giá cao ...

Trả lời

2

Trong laravel 5.4sendResetLink chỉ có một tham số credentials

Vì vậy, bạn cần phải thay đổi phương pháp thực hiện của bạn như thế này:

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

Sau đó, bạn phải gọi lại số điện thoại User (hoặc chính xác bạn có) có

trait CanResetPassword 

Mô hình này phải có phương pháp:

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

những tuyến đường cần một cái tên.

Ở đây mã ..

// Password reset link request routes... 
Route::get('password/email', 'Auth\[email protected]')->name('password.email'); 
Route::post('password/email', 'Auth\[email protected]'); 

// Password reset routes... 
Route::get('password/reset/{token}', 'Auth\[email protected]')->name('password.request'); 
Route::post('password/reset', 'Auth\[email protected]')->name('password.reset'); 
Các vấn đề liên quan