简体   繁体   English

Laravel 中间件授权组不工作

[英]Laravel Middleware Auth Group is not working

Good Day.再会。 I had a problem with my authentication which I always redirected back into the login page.我的身份验证有问题,我总是将其重定向回登录页面。 I found out that when I add in routes/web.php with this code below我发现当我使用下面的代码添加 routes/web.php 时

Route::group(['middleware' => 'auth'], function() {

}

the page is always redirected back to the login page.该页面总是重定向回登录页面。 But when I remove that code above, I can proceed to the home page.但是当我删除上面的代码时,我可以进入主页。 I trying to wonder how to solve this.我想知道如何解决这个问题。 I use route group in my past projects and I don't have any problems with that.我在过去的项目中使用了路由组,我对此没有任何问题。

UPDATE: I used php artisan test and remodify my ExampleTest.php codes.更新:我使用了 php 工匠测试并重新修改了我的 ExampleTest.php 代码。

<?php

namespace Tests\Unit;

use PHPUnit\Framework\TestCase;
use App\User;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }

    public function testApplication()
    {
        $user = factory(User::class)->create();

        $response = $this->actingAs($user)
                         ->withSession(['foo' => 'bar'])
                         ->get('/');
    }
}

These are the results这些是结果

C:\xampp\htdocs\nuadu_helpdesk\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:273
    269|      */
    270|     protected function getRawAttributes(array $attributes = [])
    271|     {
    272|         if (! isset($this->definitions[$this->class])) {
  > 273|             throw new InvalidArgumentException("Unable to locate factory for [{$this->class}].");
    274|         }
    275|
    276|         $definition = call_user_func(
    277|             $this->definitions[$this->class],

  1   C:\xampp\htdocs\nuadu_helpdesk\vendor\laravel\framework\src\Illuminate\Database\Eloquent\FactoryBuilder.php:296
      Illuminate\Database\Eloquent\FactoryBuilder::getRawAttributes([])

  2   C:\xampp\htdocs\nuadu_helpdesk\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\GuardsAttributes.php:155
      Illuminate\Database\Eloquent\FactoryBuilder::Illuminate\Database\Eloquent\{closure}()

inside routes.php file you not write where to redirect.在 routes.php 文件中,您没有写重定向的位置。 so please add like below..所以请像下面一样添加..

Route::group(['middleware' => 'auth'], function() {
  return redirect('/');  //by default you can change it as your requirments.
}

I found out about my problem.我发现了我的问题。 I checked using the auth and dd.我检查了使用 auth 和 dd。 I used a different primary key with user_id and it is a string type.我对 user_id 使用了不同的主键,它是一个字符串类型。 I forgot to declare it in my user.php我忘了在我的用户中声明它。php

protected $primaryKey = 'user_id';
protected $keyType = 'string';
public $incrementing = false;

By default, the primary key will always be the id.默认情况下,主键始终是 id。 If you declare a different column name that will serve as your primary key, don't forget to declare the codes that I stated above, or else you end not reading the auth or not logging in.如果您声明一个不同的列名作为您的主键,请不要忘记声明我上面提到的代码,否则您将无法读取授权或无法登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM