0

사용자 컨트롤러에 Auth을 사용하는 Laravel 5.3을 사용하고 있습니다. 그래서 기본적으로 내가 만들 Listener 이벤트 Auth에 대한 Laravel Auth의 LogSuccessfulLogin 핸들에서 리디렉션이 작동하지 않습니다.

'Illuminate\Auth\Events\Login' => [ 
    'App\Listeners\LogSuccessfulLogin', 
], 

role의 기본에 LogSuccessfulLoginhandle() 기능 리디렉션 사용자의

. 리디렉션 기능이 작동하지 않습니다. 그것은 기본 페이지 '\ home'경로를 렌더링합니다.

나는 내 파일을 공유하고있다 : - 여기

EventServiceProvider.php

<?php 

namespace App\Providers; 

use Illuminate\Support\Facades\Event; 
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; 

class EventServiceProvider extends ServiceProvider 
{ 
    /** 
    * The event listener mappings for the application. 
    * 
    * @var array 
    */ 
    protected $listen = [ 
     'App\Events\SomeEvent' => [ 
      'App\Listeners\EventListener', 
     ], 
     'Illuminate\Auth\Events\Login' => [ 
      'App\Listeners\LogSuccessfulLogin', 
     ], 
    ]; 

    /** 
    * Register any events for your application. 
    * 
    * @return void 
    */ 
    public function boot() 
    { 
     parent::boot(); 

     // 
    } 
} 

항로/web.php

<?php 
//Auth::routes(); 
Route::get('logout', function(){ 
    Auth::logout(); // logout user 
    return Redirect::to('/'); 
}); 
#Route::get('/', '[email protected]')->name('homepage'); 
Route::get('/', function() {return view('welcome');}); 

Route::group(['middleware' => ['auth', 'checkrole:Admin'],'namespace' => 'Admin','prefix' => 'admin'], function() { 
    Route::get('/123','[email protected]')->name('dashboard'); 
    #Route::get('/','[email protected]')->name('dashboard'); 

    Route::get('user/profile', function() { 
     // Uses Auth Middleware 
    }); 
}); 

<?php 

namespace App\Listeners; 

use Illuminate\Auth\Events\Login; 
use Illuminate\Queue\InteractsWithQueue; 
use Illuminate\Contracts\Queue\ShouldQueue; 
use Illuminate\Support\Facades\Auth; 
use Illuminate\Support\Facades\Redirect; 
class LogSuccessfulLogin 
{ 
    /** 
    * Create the event listener. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     // 
    } 

    /** 
    * Handle the event. 
    * 
    * @param Login $event 
    * @return void 
    */ 
    public function handle(Login $event) 
    { 

     $user=Auth::User()->role->role_name; 
     if($user=="Admin"){ 
      return Redirect::route('dashboard'); 
      #return redirect()->route('login'); 
     } 
     elseif($user == "Employer"){ 
      dd("hello"); 
     } 
    } 
} 

LogSuccessfulLogin.php

입니다 그리고 이것은 일이다. Auth의 기본 로그인 컨트롤러

<?php 

namespace App\Http\Controllers\Auth; 

use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\AuthenticatesUsers; 

class LoginController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Login Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles authenticating users for the application and 
    | redirecting them to your home screen. The controller uses a trait 
    | to conveniently provide its functionality to your applications. 
    | 
    */ 

    use AuthenticatesUsers; 

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

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

내가 잘못하고있는 경우 알려주십시오. 실제로 나는 또한 시도했다 LogSuccessfulLoginreturn redirect()->route('login');와 함께 페이지를 리디렉션하는 청취자이지만 작동하지 않습니다.

+0

가능한 중복 : //stackoverflow.com/questions/39532803/laravel-5-3-custom-path-for-post-authentication-redirect – Silwerclaw

+0

어쨌든 이벤트에서 리디렉션 객체를 반환하는 것이 잘못되었습니다. 앱은 이벤트 핸들러가 반환하는 것을 수신하지 않습니다. 컨트롤러, 미들웨어 또는 경로 콜백에서 Redirect :: to()를 반환하는 사용자 만 리디렉션 할 수 있습니다. – Silwerclaw

+0

사실 저는'symfony' 개발자이고 최근에 저는 Laravel에서 일하고 있습니다. sf에서 LoginsuccessHanlder에서 리디렉션 할 수 있습니다. –

답변

2

가능한 해결책 중 하나는 로그인 경로에 첨부 될 응답 미들웨어를 제공하는 것입니다. 응답을 얻은 후에는 사용자가 로그인했는지 확인하고 예인 경우 사용자 지정 리디렉션 로그인을 적용해야합니다. 이 경우 기본 Laravel 5.3 리디렉션 논리를 덮어 씁니다. 이 같은 매끄러운 : 당신은

protected $routeMiddleware = [ 
    'custom-redirect' => \YourNamespace\PostLoginMiddleware::class 
]; 

에서 응용 프로그램/HTTP/Kernel.php이 미들웨어를 정의한 다음 로그인 노선에 적용해야

class PostLoginMiddleware 
{ 

    public function handle($request, Closure $next, $guard = null) 
    { 
     // here you've already have response and it's usually redirect 
     $response = $next($request); 

     // if user logged in 
     if (\Auth::id()) { 
      $customRedirectResponse = // ...apply your custom logic 

      return $customRedirectResponse; 
     } 

     return $response; 
    } 
} 

/* Authentication Routes */ 
$this->get('login', 'Auth\[email protected]'); 
$this->post('login', ['middleware' => 'custom-redirect', 'uses' => 'Auth\[email protected]']); 
$this->get('logout', 'Auth\[email protected]'); 
HTTP의
+0

답변을 주셔서 감사합니다. 'customRedirectResponse'와 관련된 혼란이 있습니다. 조금 더 설명 할 수 있습니다. 여기서는 리디렉션 라우팅 URL이나 다른 것을 넣어야합니다. –

+0

이벤트 처리기에 쓴 내용을 삽입해야합니다. – Silwerclaw

+0

알았어. 그래서 기본적으로 내 이벤트 리스너의 전체 코드가 나타납니다. –