PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] CakePHP 3.x how do I include the params in the mapped resources?

 January 23, 2022     cakephp, cakephp-3.0, rest     No comments   

Issue

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',
            ]
        ]
    ]);  

This is my method get_by_account inside 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)
{

}

Relations:

  1. CustomerAccounts hasMany CustomerCompanies
  2. CustomerCompanies belongsTo CustomerAccounts

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.

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:

what i saw in documents


Solution

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

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

See also

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


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing