laravel :登录时重定向到管理员 用户是使用身份验证脚手架的管理员


laravel : redirect to admin when logged in user is admin using the Auth scaffold

我正在尝试使用laravel为后端构建一个Web应用程序!我使用 php artisan make:auth 命令来生成基本的登录逻辑和视图。现在,我正在尝试根据数据库中用户表中的管理字段重定向用户。问题是这样的:它认为我没有从数据库中获得正确的值,所以重定向永远不会起作用......

这是路线.php

Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/register', function () {
    return redirect('/step1');
});
route::get('/dashboard', function () {
    return view('admin.dashboard');
});
// after login --> else "welcome"
Route::get('/home', 'HomeController@index');
// register user + agency
Route::get('/step1', 'AgencyController@showStep1');
Route::post('/step1', 'Auth'AuthController@register');
// complete registration step 2 --> 9
Route::get('/step{stepId}', ['middleware' => 'auth', 'uses' => 'AgencyController@show']);
Route::post('/step{stepId}', 'AgencyController@store');});

这是重定向的代码(在AuthController.php中)

<?php
namespace App'Http'Controllers'Auth;
use App'Agency;
use App'Http'Controllers'Controller;
use App'User; 
use Auth;
use Illuminate'Foundation'Auth'AuthenticatesAndRegistersUsers;
use Illuminate'Foundation'Auth'ThrottlesLogins;
use Illuminate'Http'Request;
use Validator;
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']);
}
public function login() {
    if (Auth::check()) {
        if (Auth::User()->admin != '1') {
            return redirect('/dashboard');
        } else {
            return redirect('/step1');
        }
    } else {return 'doesnt work :(';}
}

当我运行它并登录时,我总是收到"不起作用"文本。

注意:身份验证命令生成的路由:

$this->get('login', 'Auth'AuthController@showLoginForm');
$this->post('login', 'Auth'AuthController@login');
$this->get('logout', 'Auth'AuthController@logout');
// Registration Routes...
$this->get('register', 'Auth'AuthController@showRegistrationForm');
$this->post('register', 'Auth'AuthController@register');

提前感谢!

你使用的是 laravel 4.2 吗?似乎您正在手动登录,如果您想登录用户,尤寿乐

if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
    if(Auth::User()->admin != 1){
        #your logic
    }
    return Redirect::intended('dashboard');
}