Cakephp 如何创建一条路由来接受 id 和 cat id 并维护路径?

发布于 2025-01-13 09:34:31 字数 1841 浏览 5 评论 0原文

我有一个 API 前缀路由,我有一个控制器,我试图在其中获取产品,然后按 id 获取产品,然后按类别 id 获取产品。

所以,我创建了一个前缀路由

$routes->scope('/api', function (RouteBuilder $routes) {
        $routes->setExtensions(['json']);
        $routes->resources('Shops',[
            'prefix' => 'Api',
            'path' => 'shops',
            'map' => [
                'getProducts' => [
                    'action' => 'getProducts',
                    'method' => 'GET',
                    'path' => '/products'
                ],
                'getProductById' => [
                    'action' => 'getProducts',
                    'method' => 'GET',
                    'path' => '/products/{id}',
                ],
                'getProductBySubcatId' => [
                    'action' => 'getProducts',
                    'method' => 'GET',
                    'path' => '/products/cat/{cat_id}',
                    'subcat_id' => [0-10]
                ],
            ]
        ]);
 });

实际上我正在尝试做

public function getProducts($id = null, $cat_id = null)
{   
        if($id)
          -----
        else if($cat_id)
          ----
}

的数据

我需要获取像http://localhost:8000/api/shops/products/cat/1.json

这样 对于以下 URL

http://localhost:8000/api/shops/products/1.json

我得到了 params pass

protected params => [
'id' => '1',
'pass' => [
   (int) 0 => '1',
],

但对于 catId 我没有得到 pass http://localhost:8000/api/shops/products/cat/1.json

protected params => [
'catId' => '1',
'pass' => [ ],

另外,我可以发送字符串作为 cat id 的参数,

protected params => [
'catId' => 'A',
'pass' => [ ],

如何在 pass 中添加 catId ?另外,如何添加 catId 始终为 int 的验证?

I have an API prefix route, I have a controller where I'm trying to fetch product, then products by id , then products by category id.

So, I have created a prefix route

$routes->scope('/api', function (RouteBuilder $routes) {
        $routes->setExtensions(['json']);
        $routes->resources('Shops',[
            'prefix' => 'Api',
            'path' => 'shops',
            'map' => [
                'getProducts' => [
                    'action' => 'getProducts',
                    'method' => 'GET',
                    'path' => '/products'
                ],
                'getProductById' => [
                    'action' => 'getProducts',
                    'method' => 'GET',
                    'path' => '/products/{id}',
                ],
                'getProductBySubcatId' => [
                    'action' => 'getProducts',
                    'method' => 'GET',
                    'path' => '/products/cat/{cat_id}',
                    'subcat_id' => [0-10]
                ],
            ]
        ]);
 });

In action actually I'm trying to do

public function getProducts($id = null, $cat_id = null)
{   
        if($id)
          -----
        else if($cat_id)
          ----
}

I need to fetch the data like

http://localhost:8000/api/shops/products/cat/1.json

Present
For below URL

http://localhost:8000/api/shops/products/1.json

I'm getting params pass

protected params => [
'id' => '1',
'pass' => [
   (int) 0 => '1',
],

But for catId I'm not getting pass
http://localhost:8000/api/shops/products/cat/1.json

protected params => [
'catId' => '1',
'pass' => [ ],

Also I'm able to send string as a params for cat id

protected params => [
'catId' => 'A',
'pass' => [ ],

How can I add catId in pass ? also how can I add a validation that catId always int ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

棒棒糖 2025-01-20 09:34:31

您可以使用 connectOptions 解决此问题。

map [
  // your map 
],
'connectOptions' => [
    'catId' => '[0-9]+',
    'pass' => ['id','catId'],
],

有关详细信息,请检查 RouteBuilder.php 类

You can solve it using connectOptions

map [
  // your map 
],
'connectOptions' => [
    'catId' => '[0-9]+',
    'pass' => ['id','catId'],
],

For details check RouteBuilder.php class

天冷不及心凉 2025-01-20 09:34:31

会尝试在您的方法中使用扩展运算符:

public function getProducts(...$params)
{   
   //try to understand spread operator
   debug($params);

   if (count($params) == 2 && !is_number($params[0]) && is_number($params[1]) {
     $id = strval($params[0]);
     $cat_id = intval($params[1]);
   }

  if (count($params) == 1 && is_number($params[0]) {
     $id = null;
     $cat_id = intval($params[0]);
   }
   // ... implement your code     
}

Would try to use the spread operator in you method:

public function getProducts(...$params)
{   
   //try to understand spread operator
   debug($params);

   if (count($params) == 2 && !is_number($params[0]) && is_number($params[1]) {
     $id = strval($params[0]);
     $cat_id = intval($params[1]);
   }

  if (count($params) == 1 && is_number($params[0]) {
     $id = null;
     $cat_id = intval($params[0]);
   }
   // ... implement your code     
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文