亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

由于沖突,我無法訪問以占位符為前綴的路由

由于沖突,我無法訪問以占位符為前綴的路由

PHP
森林海 2021-10-15 15:26:23
這是我正在處理的路線(由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                    );                }            });        }    };}
查看完整描述

2 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

路由器很難通過任一側的變量來確定。在第一個/像something/{tenant}/customers. 但是,錯誤的原因很可能是根據您的路由列表到達路由器的第一個 GET 路徑是:


{tenant}/customers/{customer}

因為這是第一個,Laravel預計會有一個客戶變量進來。如果將此行放在更高的位置,則不會每次都期望變量。所以:


{tenant}/customers/{customer}

{tenant}/customers/

這應該會有所幫助......但它可能不是由于兩邊的通配符 - 你必須測試。


如果您將這些設置為 a resource,我建議您將它們分解為單獨的路由方法進行測試


查看完整回答
反對 回復 2021-10-15
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

終于在三天后,我找到了來源,異常消息誤導了我。


/**

     * 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'] ]

                )

            )

        ];

    }

無論如何感謝您的幫助!


查看完整回答
反對 回復 2021-10-15
  • 2 回答
  • 0 關注
  • 123 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號