【问题标题】:CakePHP 3.x how do I include the params in the mapped resources?CakePHP 3.x 如何在映射资源中包含参数?
【发布时间】:2015-10-18 09:08:54
【问题描述】:

这就是我的路线:

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

这是我的方法get_by_accountCustomerCompaniesController.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. CustomerAccountshasMany CustomerCompanies
  2. CustomerCompanies 属于CustomerAccounts

我想通过提供 CustomerAccount id 返回属于特定 CustomerAccount 的 CustomerCompanies 列表,然后返回 json 结果。

我是否需要更改 get_by_account 中的某些内容以指示参数?如果有,怎么做?

更新

这是我在文档中看到的:

【问题讨论】:

    标签: rest cakephp cakephp-3.0


    【解决方案1】:

    您必须定义:id 路由元素,即passed by default(目前无法传递更多自定义路由元素)。您可以在地图条目的数组键中执行此操作

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

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

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

    另见

    【讨论】:

    • 我在get_by_account($customer_account_id = null) 中使用customer_account_id 是否重要?我可以将它用于嵌套资源吗?例如。而不是get_by_account/:id,我使用accounts/:id/companies 并将其放在CustomerAccounts 控制器中?
    • 我还阅读了您提供的食谱链接,但在同一部分中没有提及如何使用密钥。另外,我想知道我是否也可以进行验证,例如在book.cakephp.org/3.0/en/development/routing.html#route-elements?您可以看到他们将:id 限制为数字
    • @KimStacks 1.) No. 2.) 也许,也许不是,取决于您期望最终 URL 的样子。使用实际的嵌套资源将创建固定的父路径。 3.) 在第一个例子之后有。 4.) 查看RouteBuilder::resources()id 选项。如果您需要为同一资源中的其他路由保留现有 ID 验证,则必须创建、使用和传递 (connectOptions) 自定义元素(但不确定这是否可行)。
    • 只是想澄清一下:我已经拍摄了您为食谱提供的链接的快照并将其放在问题中。如您所见,食谱有一个 updateALl 和 deleteAll 示例。在path 键中提供:id 的情况下,您所写的内容没有示例。您认为我们应该将您的示例添加到本节的文档中吗?
    • @KimStacks 我指的是“默认情况下路径段将匹配键名”这句话,因为我认为这就是您所指的“如何使用密钥”。一个展示如何在路径中使用路由元素的示例肯定不会有什么坏处。
    【解决方案2】:

    我意识到业务逻辑明智,我试图创建一个嵌套资源。

    因此,我将在我自己的另一个question 中包含关于在此处创建嵌套资源的答案。

    ------- 答案-----------

    在您的 routes.php 中执行此操作

    $routes->resources('Parents', function ($routes) {
        $routes->resources('Children');
    });
    
    $routes->resources('Children');
    

    ChildrenController.php

    protected function _prepareConditions() {
        $parentId = isset($this->request->params['parent_id']) ? $this->request->params['parent_id'] : null;
    
        if ($parentId == null) {
            return [];
        }
    
        return [
            'Children.parent_id' => $parentId
        ];
    }
    
    public function index()
    {
        $conditions = $this->_prepareConditions();
    
        $this->paginate = [
            'contain' => ['Parents'],
            'conditions' => $conditions
        ];
      // ... and so on
    

    您将能够执行以下操作:

    1. /parents/1/children
    2. /parents/1/children.json
    3. /儿童
    4. /children.json

    为什么会这样?

    http://book.cakephp.org/3.0/en/development/routing.html#creating-nested-resource-routes

    告诉我们基本上是从请求参数中检索父 id。

    它没有明确说明的是,路由将重用基本的 5 个功能:索引、添加、查看、删除、编辑,即使您将它们嵌套在父 url 下。

    为什么还要为 Children 设置单独的资源路线?

    这允许 /children 和 /children.json 如果您需要它们也可以工作。

    添加呢?

    我没有尝试过,但我认为使用它不会有任何问题

    1. /parents/1/children/add
    2. /parents/1/children/add.json
    3. /儿童/添加
    4. /children/add.json

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-04
      • 2016-11-07
      • 2016-05-06
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多