【问题标题】:CakePHP 3.4 and CakePHP v3.5 Parameterized routingCakePHP 3.4 和 CakePHP v3.5 参数化路由
【发布时间】:2018-01-16 07:06:36
【问题描述】:

我在 cakePHP v3.5 中制作小项目,我无法以适当的方式使用参数化。同样在某些情况下,如果我们想在 url 中传递可选参数,那我该怎么办?

CakePHP v3.5

$routes->get('test/testfn/:param1/:param2', ['controller' => 'Pages', 'action' => 'testfn']);

where 
test: Controller,
testfn: Method of TestController,
param1: Parameter 1,
param2: Parameter 2

我所做的一切都是为了从 url 获取参数到 TestController,

  $this->request->getParam(param1)
  $this->request->getParam(param2)

如何一步一步列出我从路由传递到我的控制器的所有参数,而不是单个参数。

或者任何人都有更好的选择在 cakePHP v3.5 中进行路由

另外我对 cakePHP3.4 的参数化路由原理感到困惑 所以,在那种情况下,如果有人对 cakePHP v3.4 有一些解决方案。

请帮帮我。

谢谢

【问题讨论】:

  • 在 cake 3.4 带参数的路由中:- stackoverflow.com/questions/37448765/…
  • 不清楚你在问什么。您是在问如何定义路由或如何读取路由参数。
  • @cgTag 你能解释一下我们如何在 cakePHP3.5 中进行参数化路由。谢谢

标签: php cakephp routing cakephp-3.0


【解决方案1】:

在 config/routes.php 中

$routes->get(
            '/api/test/*', ['controller' => 'Api', 'action' => 'check']
    );

在控制器中

public function check($first=null, $sec=null) {

   pr($params);
   pr($sec);
   die;
}

【讨论】:

    【解决方案2】:

    在 Routes.php 中

    $routes->connect('/users/:id/edit/:type', ['controller' => 'Users', 'action' => 'edit', ['id' => '\d+', 'pass' => ['id', 'type'], '_name' => 'edit-client']);
    

    在这个路由中,ID是用户ID,类型是用户类型,两个参数传入这个路由

    如果这个路由方法是 POST 那么 ex - $this->request->getData('id'); 如果这个路由方法 GET 那么 ex - $this->request->getParam('id')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 2018-04-22
      • 1970-01-01
      相关资源
      最近更新 更多