![](/img/trans.png)
[英]Laravel 5.1/AngularJS: Reset password in Angular view (how to validate CSRF token?)
[英]Laravel 5 Password Reset with Angular View
我正在嘗試在我的應用中使用Laravel內置密碼重置功能,其中Laravel 5.1充當后端api,而Angular 1.3充當所有前端視圖。 我已經按照文檔設置了密碼重置,並執行以下操作:
1)創建表
php artisan migrate
2)將此添加到路線:
Route::post('password/email', 'Auth/PasswordController@postEmail');
Route::post('password/reset', 'Auth/PasswordController@postReset');
由於我將使用Angular顯示前端表單,因此沒有為GET
添加視圖。 我還沒有對Auth/PasswordController.php
進行任何更改,現在就像它來的一樣。 但是,當我從Postman POST
請求測試上述URL時,出現錯誤:
View [emails.password] not found.
如何讓Angular Handle視圖而不讓Laravel擔心視圖? 我必須擁有Laravel View才能重置內置密碼才能正常工作嗎? 我該如何處理?
重寫postEmail
和postReset
方法,以便它們返回JSON響應(不要讓它重定向)。 隨后通過xhr從Angular發布到/password/email
和/password/reset
。
打開app/Http/Controllers/Auth/PasswordController.php
<?php namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
class PasswordController extends Controller
{
use ResetsPasswords;
//add and modify this methods as you wish:
/**
* Send a reset link to the given user.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postEmail(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)]);
}
}
/**
* Reset the given user's password.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postReset(Request $request)
{
$this->validate($request, [
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed',
]);
$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());
default:
return redirect()->back()
->withInput($request->only('email'))
->withErrors(['email' => trans($response)]);
}
}
}
在“視圖”部分的app \\ bootstrap \\ cache \\ config.php中,插入您的views文件夾路徑
'view' =>
array (
'paths' =>
array (
0 => '/home/vagrant/Code/app/resources/views',
),
'compiled' => '/home/vagrant/Code/app/storage/framework/views',
),
此路徑必須在SERVER上! 如果您使用宅基地,請不要使用本地的mashine,例如“ D:\\ WebServers \\ home \\ Laravel \\ app \\ bootstrap \\ cache”。 並且您必須在SERVER上使用以下命令:“ php artisan config:clear | cache”!
我和你有同樣的問題。 如果您在resources / views / emails / password.blade.php中沒有另一個視圖,則可以設法在config/auth.php
更改視圖。
因為默認情況下未創建此視圖,所以才出現錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.