由于冲突,我无法访问以占位符为前缀的路由

http://img1.mukewang.com/61692d330001ae3117760085.jpg

这是我正在处理的路线(由apiResourceLaravel 的方法生成)。如您所见,有 1 或 2 个占位符。当我尝试 GET 时,我的问题就出现了anything/customers。它引发了这个异常:


Missing required parameters for [Route: json-api.customers.show] [URI: {tenant}/customers/{customer}].


除非我遗漏了一个明显的东西,否则路由器应该接受这个请求,因为anything/customers匹配{tenant}/customers.


我真的很感激任何有关这方面的帮助。提前致谢。


编辑:我添加此代码来回答评论,但我认为这对理解这个问题没有帮助(我正在实现一个基于 JSON:API 规范的包)。


protected function jsonApiResource()

{

    return function (string $class, array $options = []) {

        if ($routerMethod = $class::getRouterMethod()) {

            $middleware = array_merge(

                $options['middleware'] ?? [],

                $class::getApiMiddlewares()

            );


            $as = $options['as'] ?? '';


            $prefix = $class::getRouterPrefix();


            $this->group(compact('middleware', 'as', 'prefix'), function ($router) use ($class, $routerMethod, $options) {

                $alias      = $class::getAlias();

                $controller = $class::getControllerClass();


                $router->{$routerMethod}(

                    $alias,

                    $controller,

                    Arr::only($options, ['only', 'except'])

                );


                foreach ($class::getRelationsRoutes() as $relationshipName => $relationshipMethods) {

                    $router->resourceRelationship(

                        $alias,

                        $relationshipName,

                        $controller,

                        $relationshipMethods

                    );

                }

            });

        }

    };

}


森林海
浏览 99回答 2
2回答

慕后森

路由器很难通过任一侧的变量来确定。在第一个/像something/{tenant}/customers. 但是,错误的原因很可能是根据您的路由列表到达路由器的第一个 GET 路径是:{tenant}/customers/{customer}因为这是第一个,Laravel预计会有一个客户变量进来。如果将此行放在更高的位置,则不会每次都期望变量。所以:{tenant}/customers/{customer}{tenant}/customers/这应该会有所帮助......但它可能不是由于两边的通配符 - 你必须测试。如果您将这些设置为 a resource,我建议您将它们分解为单独的路由方法进行测试

慕码人8056858

终于在三天后,我找到了来源,异常消息误导了我。/**     * Get links to fetch the model or one of its relationships.     *     * @param  string|null  $relationshipName     * @return array     */    public function getApiLinks(string $relationshipName = null)    {        $urlGenerator   = app()->make('url');        $identifiers    = $this->getApiIdentifiers();        if ($relationshipName) {            return [                'self'      => $urlGenerator->jsonApiRelationship($identifiers['type'], $identifiers['id'], $relationshipName, 'index'),                'related'   => $urlGenerator->jsonApiRelationship($identifiers['type'], $identifiers['id'], $relationshipName, 'related')            ];        }        return [            'self' => $urlGenerator->route(                'json-api.'.$identifiers['type'].'.show',                [ Str::singular($identifiers['type']) => $identifiers['id'] ]        ];问题来自返回时的 URL 生成,任何额外的 URL 占位符都不会包含在数组中,顺便说一下导致此消息。有了这个修复,它现在可以工作了:/**     * Get links to fetch the model or one of its relationships.     *     * @param  string|null  $relationshipName     * @return array     */    public function getApiLinks(string $relationshipName = null)    {        $urlGenerator   = app()->make('url');        $identifiers    = $this->getApiIdentifiers();        $otherParams    = $urlGenerator->getRequest()->route()->parameters();        if ($relationshipName) {            return [                'self'      => $urlGenerator->jsonApiRelationship($identifiers['type'], $identifiers['id'], $relationshipName, 'index', $otherParams),                'related'   => $urlGenerator->jsonApiRelationship($identifiers['type'], $identifiers['id'], $relationshipName, 'related', $otherParams)            ];        }        return [            'self' => $urlGenerator->route(                'json-api.'.$identifiers['type'].'.show',                array_merge(                    $otherParams,                    [ Str::singular($identifiers['type']) => $identifiers['id'] ]                )            )        ];    }无论如何感谢您的帮助!
打开App,查看更多内容
随时随地看视频慕课网APP