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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题