ユーザーコントローラーに使用していますLaravel 5.3
。Auth
だから基本的に私はListener
for Auth
Eventを作成します
'Illuminate\Auth\Events\Login' => [
'App\Listeners\LogSuccessfulLogin',
],
LogSuccessfulLogin
handle()
の基本でユーザーをリダイレクトする機能でrole
。しかし、リダイレクト機能が機能していません。デフォルトのページ '\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('/', 'HomeController@index')->name('homepage');
Route::get('/', function () {return view('welcome');});
Route::group(['middleware' => ['auth', 'checkrole:Admin'],'namespace' => 'Admin','prefix' => 'admin'], function () {
Route::get('/123','DashboardController@index')->name('dashboard');
#Route::get('/','DashboardController@index')->name('dashboard');
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
ここに LogSuccessfulLogin.php があります
<?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");
}
}
}
これは Auth のデフォルトの logincontroller です
<?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']);
}
}
私が何か間違ったことをしている場合はお知らせください。LogSuccessfulLogin
実際、ページをリダイレクトするためにリスナーでも試しましreturn redirect()->route('login');
たが、機能しません。