我使用laravel-auth根据用户的类型重定向用户.但我被卡住了.我无法根据用户的类型重定向用户


I am using laravel auth for redirecting my users according to their type . But there i get stuck. I could not redirect user according to their type

这是我的routes.php文件

    <?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
    return view('welcome');
});
Route::get('/login', function(){
    return view('auth.login');
});
Route::post('/login', 'Auth'AuthController@authenticate');
Route::get('/home', 'HomeController@index');

这是我的AuthController.php文件

<?php
namespace App'Http'Controllers'Auth;
use App'User;
use Validator;
use App'Http'Controllers'Controller;
use Illuminate'Foundation'Auth'ThrottlesLogins;
use Illuminate'Foundation'Auth'AuthenticatesAndRegistersUsers;
use Auth;
class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    /**
     * Where to redirect users after login / registration.
     *
     * @var string
     */
    protected $redirectTo = '/';
    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }
    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return 'Illuminate'Contracts'Validation'Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'user_name' => 'required|max:255|unique:users',
            'full_name' => 'required|max:255',
            'password' => 'required|min:6|confirmed',
        ]);
    }
    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'user_name' => $data['user_name'],
            'full_name' => $data['full_name'],
            //'password' => bcrypt($data['password']),
            'password' => $data['password'],
        ]);
    }

    //Auth::attempt(['user_name' => $user_name, 'password' => $password])
    public function authenticate()
    {
        if (Auth::attempt($request->all()) {
            var_dump(Auth::user())
            if(Auth::user()->type == 'admin') {
                return "Welcome company admin let's create some user for your company";
                # code...
            } elseif(Auth::user()->type == manager) {
                return "Welcome manager let's manage your coaches";
            }elseif(Auth::user()->type == 'counterman'){
                return "Welcome counter person let's sell some ticket's";
            }else
            {
                return "Welcome online user let's make a relationship with me";
            }
            return "Gonnnaaa";
            //return redirect()->intended('dashboard');
        }else
        {
            return "you are fucked";
        }
    }
}

在我的项目中,我想根据类型将我的用户重定向到不同的页面。我试着用不同的方式来实现这一点。最后,我尝试在AuthController中使用laravel文档中建议的authenticate方法,但我得到了AuthController不存在的错误。我做错了什么?在我的情况下,还有什么更好的方法?提前谢谢。除了AuthController和路由之外,我没有编辑过任何内容。

如果您不想自己实现新的东西,可以创建一个新的TypeBasedAuthController来扩展AuthController

然后,您将通过实现一个调用父postLoginpostLogin方法来装饰它的父级。登录逻辑之后,您可以根据需要更改$redirectTo属性。

它应该有意义…:)

编辑:如果您想了解更多关于PHP中装饰器模式的信息,请查看此链接:)

EDIT2-重要:经过另一次深入搜索,我找到了更好的解决方案。您所要做的就是在新的TypeBasedAuthController中覆盖此方法。

protected function handleUserWasAuthenticated(Request $request, $throttles)
{
    if ($throttles) {
        $this->clearLoginAttempts($request);
    }
    if (method_exists($this, 'authenticated')) {
        return $this->authenticated($request, Auth::guard($this->getGuard())->user());
    }
    // HERE: control the user that has logged in and change the value
    // of $this-redirectTo property accordingly.
    return redirect()->intended($this->redirectPath());
}

现在应该很清楚了。