ds211107 2018-10-10 08:57
浏览 251
已采纳

无法向Laravel端点发出HTTP请求

I'm having a really strange issue when trying to make a HTTP request in one of my tests. My api.php routes file looks like this:

$router->group(['prefix' => '/v1', 'middleware' => ['auth:api']], 
    function (\Illuminate\Routing\Router $router) {
        $router->apiResource('/contacts', 'ContactsController');

        $router->group(['prefix' => '/contacts'], 
            function (\Illuminate\Routing\Router $router) {
                $router->apiResource('/groups', 'Contacts\\GroupsController');
        });
});

Running php artisan route:list shows the following route as being registered

+------------+------------------------+--------------+------------------------------------------------------+----------------+
|   Method   |           URI          |      Name    |                        Action                        |   Middleware   |
+------------+------------------------+--------------+------------------------------------------------------+----------------+
| GET|HEAD   | api/v1/contacts/groups | groups.index | App\Http\Controllers\Contacts\GroupsController@index |  api auth:api  |
+------------+------------------------+--------------+------------------------------------------------------+----------------+

However when I run my test, I get a 404 response. The test is as follows:

public function testICanGetAllOfTheGroups()
{
    factory(Group::class)->times(3)->create();

    $this->json('GET', '/api/v1/contacts/groups')
            ->assertStatus(200)->assertJsonCount(3);
}

I have also tried running php artisan route:clear and php artisan cache:clear but it hasn't made a difference.

For authentication, I have created the following trait which I am using.

trait Authenticated
{
    /** @var \App\Models\User */
    protected $user;

    public function setUp()
    {
        parent::setUp();

        $this->user = factory(User::class)->create();
        Passport::actingAs($this->user);
    }
}
  • 写回答

3条回答 默认 最新

  • dsxrq28228 2018-10-10 09:12
    关注

    I appear to have figured this out. If I re-arrange the routes from

    $router->group(['prefix' => '/v1', 'middleware' => ['auth:api']], function (\Illuminate\Routing\Router $router) {
        $router->apiResource('/contacts', 'ContactsController');
    
        $router->group(['prefix' => '/contacts'], function (\Illuminate\Routing\Router $router) {
            $router->apiResource('/groups', 'Contacts\\GroupsController');
        });
    });
    

    to

    $router->group(['prefix' => '/v1', 'middleware' => ['auth:api']], function (\Illuminate\Routing\Router $router) {
        $router->group(['prefix' => '/contacts'], function (\Illuminate\Routing\Router $router) {
            $router->apiResource('/groups', 'Contacts\\GroupsController');
        });
    
        $router->apiResource('/contacts', 'ContactsController');
    });
    

    It then works. I suspect this is a bug with how Laravel is registering it's routes as POST and PUT requests still work fine.

    I have raised this as a bug on GitHub - https://github.com/laravel/framework/issues/26038


    UPDATE

    I have now identified the issue. Basically the apiResource translates one of the endpoints to:

    /contacts/{contact}.

    Following on from that one of the routes is contacts/groups. However in order for Laravel to match it with contacts/groups it needs to be before /contacts/{contact} as it is a wildcard. Whether or not it is intentional behaviour I'm not sure, but at the moment just having the routes the other way around fixes this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀