CakePHP 3.x如何在映射的资源中包含参数? [英] CakePHP 3.x how do I include the params in the mapped resources?

查看:80
本文介绍了CakePHP 3.x如何在映射的资源中包含参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我在路线中所拥有的:

this is what I have in my routes:

Router::scope('/', function ($routes) {
    $routes->extensions(['json']);
    $routes->resources('CustomerCompanies', [
        'map' => [
            'get_by_account' => [
                'action' => 'get_by_account',
                'method' => 'GET',
            ]
        ]
    ]);  

这是我的方法 get_by_account CustomerCompaniesController.php

/**
 * Pagination method by Customer Account id
 *
 * @param string|null $id Customer Account id.
 * @return void Redirects to index.
 * @throws \Cake\Network\Exception\NotFoundException When record not found.
 */
public function get_by_account($customer_account_id = null)
{

}

关系:


  1. CustomerAcc ounts hasMany CustomerCompanies

  2. CustomerCompanies ownersTo CustomerAccounts

  1. CustomerAccounts hasMany CustomerCompanies
  2. CustomerCompanies belongsTo CustomerAccounts

我想返回属于特定CustomerAccount的CustomerCompany的列表通过提供CustomerAccount ID,我会返回json结果。

I want to return a list of CustomerCompanies that belong to a particular CustomerAccount by supplying the CustomerAccount id and I get back in json results.

我是否需要更改get_by_account中的内容以表明参​​数?

Do I need to change something in the get_by_account to indicate the param? and if so, how?

UPDATE

这就是我所看到的文档:

This is what I saw in the documents:

推荐答案

您必须定义:id 路由元素,该元素是 默认通过 (目前无法传递其他自定义路由元素)。您可以在地图条目的数组键中完成该操作

You have to define the :id route element, which is being passed by default (there is currently no way to pass further custom route elements). You can do that either in the array key of your map entry

'map' => [
    'get_by_account/:id' => [
        'action' => 'get_by_account',
        'method' => 'GET',
    ]
]

或通过 path 选项(默认情况下,键设置为 path 选项的值)

or via the path option (by default, the key is set as the value for the path option)

'map' => [
    'get_by_account' => [
        'action' => 'get_by_account',
        'method' => 'GET',
        'path' => 'get_by_account/:id'
    ]
]

另请参见

  • Cookbook > Routing > Mapping Additional Resource Routes
  • Source > \Cake\Routing\RouteBuilder::$_resourceMap

这篇关于CakePHP 3.x如何在映射的资源中包含参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆