2016-10-26 16 views
0

Laravel 5.3 LoginControllerの応答メッセージを変更したいと思います。現在、ログインしようとすると、ログインページにリダイレクトされ、上部にメッセージが表示されます。Laravel 5.3 modify LoginControllerの応答

代わりに、メッセージを表示するためにajax通知を使用したいと思います。 LoginControllerの応答ロジックを変更するにはどうすればよいですか? AJAX呼び出しのための

サンプルコード私は現在、PS

$.ajax({ 
    data: data, 
    url: 'http://mywebsite.com/login', 
    success:function(response){ 
     console.log(response); 
    } 
}); 

を使用しています - 私はLaravelに初心者ですし、これはそのための枠組み

+0

ログインフォームを送信するには、ajaxを使用していますか? –

+0

@RossWilson:はい私のフォームを提出するためにjQueryのajaxを使用しています – SanketR

+0

あなたのPHPスクリプトも提供します – Beginner

答えて

1

一つの方法は、からsendLoginResponse()sendFailedLoginResponse()メソッドをオーバーライドすることですあなたは、JavaScriptからそれを得るために、AJAXの応答を解析する必要があり、このようになります。リクエストがajaxであるかどうか(またはjsonが必要であるかどうか)を確認できるようにします。あなたに

追加Auth/LoginController

/** 
* Send the response after the user was authenticated. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\Response 
*/ 
protected function sendLoginResponse(Request $request) 
{ 
    $request->session()->regenerate(); 

    $this->clearLoginAttempts($request); 

    if ($request->ajax() || $request->wantsJson()) { 
     return response()->json([ 
      'user' => $this->guard()->user(), 
     ]); 
    } 

    return $this->authenticated($request, $this->guard()->user()) 
     ?: redirect()->intended($this->redirectPath()); 
} 

/** 
* Get the failed login response instance. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\RedirectResponse 
*/ 
protected function sendFailedLoginResponse(Request $request) 
{ 
    if ($request->ajax() || $request->wantsJson()) { 
     return response()->json([ 
      $this->username() => Lang::get('auth.failed'), 
     ], 422); 
    } 

    return redirect()->back() 
     ->withInput($request->only($this->username(), 'remember')) 
     ->withErrors([ 
      $this->username() => Lang::get('auth.failed'), 
     ]); 
} 

これはあなたのプロジェクトのために同じroutesに維持できるようにする必要があります。

また、あなたがdataType: 'json'を追加する場合、それはすなわちあなたのための応答を解析する必要があります。:

$.ajax({ 
    data: data, 
    url: 'http://mywebsite.com/login', 
    dataType: 'json', 
    method: 'post', 
    success:function(response){ 

     console.log('success', response); 
    }, 
    error: function() { 
     console.log('error', response) 
    } 
}); 

・ホープこのヘルプ!

+0

ええ。出来た。ありがとう。注記のために、関数で渡された 'Request $ request'を' $ request'に更新しました。Request :: all()関数の結果の代わりに新しい要求オブジェクトを渡す必要があるためです。 – SanketR

1

使用laravel認証コンポーネントを使用して私の最初のアプリです

最初にこの

use Illuminate\Contracts\Auth\Guard; 
use App\Http\Requests; 
use Auth; 
を追加

コントローラ内部のPHPのアクションは、この
使用laravel認証のようにする必要があります::ユーザー資格情報が正しい
であり、それはjavascriptので読み取ることができるようにちょうどJSON

public function index(Request $request) { 

    $response = array(); 
    $conditions = array(
     'username' => $request->input('username'), 
     'password' => $request->input('password') 
    ); 
    /* check if user credentials is okay */ 
    if (Auth::attempt($conditions)) { 
     $response['message'] = 'Successfully login'; 
    } else { 
     $response['message'] = 'Incorrect user credentials'; 
    } 
    echo json_encode($response); 
} 

として結果を表示した場合、それはチェックしようそして、JavaScriptの関数あなたのアヤックスは

$.ajax({ 
    data: data, 
    url: 'http://mywebsite.com/login', 
    success:function(response){ 
    response = JSON.parse(response); // parse your json data from ajax response your result like this {"message": "Successfully login"} 
    console.log(response.message); // 
    } 
});