Laravel 5 - 中间件在登录时始终限制访问

时间:2015-06-17 08:57:38

标签: authentication laravel-5 middleware

此刻我有一个非常恼人的问题:(希望你能帮助我...

我目前正在使用laravel的基本登录表单,其中包含非常未触及的用户模型。 因此,登录显然有效(跟踪它),但每当我尝试在中间件的帮助下限制内容访问(限制路由)时,它都会拒绝访问。

那么,如果我明显登录了,为什么它会拒绝我?

我真的没有看到我做错了什么。

提前感谢您的帮助! (有关更多信息,我复制了您的中间件,authcontroller和我的用户模型)

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;


class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{

  use Authenticatable, CanResetPassword;

  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'user';


  /**
   * The attributes excluded from the model's JSON form.
   *
   * @var array
   */
  protected $hidden = ['password', 'remember_token'];

  public $timestamps = false;

  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = ['user_properties_ID', 'email', 'password'];

}

我也使用标准的AuthController

<?php namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

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;

  /**
   * Create a new authentication controller instance.
   *
   * @param  \Illuminate\Contracts\Auth\Guard  $auth
   * @param  \Illuminate\Contracts\Auth\Registrar  $registrar
   * @return void
   */
  public function __construct(Guard $auth, Registrar $registrar)
  {
    $this->auth = $auth;
    $this->registrar = $registrar;

    // $this->middleware('guest', ['except' => 'getLogout', 'getLogin']);
  }

}

我的中间件看起来像这样:

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated {

    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */

    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }


    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
    if (! $this->auth->check())
    {
      // return $next($request);
      return response('Unauthorized.', 401);
      // return redirect()->intended();
      // return new RedirectResponse(url('/trainingsplan-hinzufuegen'));
    }

    return $next($request);
    }

}

routes.php文件

<?php

/*
|--------------------------------------------------------------------------
| Home
|--------------------------------------------------------------------------
*/

Route::group(['prefix' => '', 'middleware' => 'guest', 'namespace' => 'Modules\TrainaryCore\Http\Controllers', 'as' => 'home'], function()
{
  Route::get('/home', 'TrainaryCoreController@render');
});

/*
|--------------------------------------------------------------------------
| Registrierung
|--------------------------------------------------------------------------
*/

Route::group(['prefix' => '', 'namespace' => 'Modules\TrainaryCore\Http\Controllers', 'as' => 'registrieren'], function()
{
  Route::get('/registrieren', 'TrainaryCoreController@render');
  Route::post('/registrieren', 'RegistrationController@validateForm');
});


/*
|--------------------------------------------------------------------------
| Trainingsplan
|--------------------------------------------------------------------------
*/

Route::group(['prefix' => '', 'middleware' => 'guest', 'namespace' => 'Modules\TrainaryCore\Http\Controllers', 'as' => 'schedule'], function()
{
  Route::get('/trainingsplan-hinzufuegen', 'TrainaryCoreController@render');
  Route::post('/trainingsplan-hinzufuegen', 'ScheduleController@validateForm');
});

2 个答案:

答案 0 :(得分:0)

嗯,发生的事情还可以。如果您使用GUEST中间件,它只允许您进入,如果您是访客。你必须使用AUTH中间件。

答案 1 :(得分:0)

好吧,Laravel用作表格的标准'id'... 所以,如果你想重命名它,你必须在模型中添加以下内容:

protected $primaryKey  = 'ID';

这就是解决方案男孩