将参数传递到自定义 url 路由

Pass params into custom url route

我创建了这样的自定义 url 路线:

Router::connect('/subjects.details', array(
    'plugin' => 'subjects',
    'controller' => 'subjects',
    'action' => 'details'
));

但是 action/view 需要一个参数。

所以当我像 localhost/foo/subjects.details/12 那样去 link 时,它给了我缺少控制器的错误。

Missing Controller

Error: Subjects.detailsController could not be found.

Error: Create the class Subjects.detailsController below in file: app/Controller/Subjects.detailsController.php

如何为此 url 添加 id 参数?

您必须在 url 和您的操作中定义参数:

  Router::connect('/subjects.details/:id', array(
        'plugin' => 'subjects',
        'controller' => 'subjects',
        'action' => 'details'
    ));

激光回答返回另一个错误。它基本上将新键 'id' => '1' 添加到 $this->params 数组中,而不是添加到 $this->params'passed' 键数组中。通过将其更改为它可以工作:

Router::connect('/subjects.details/*', array(
  'plugin' => 'subjects',
  'controller' => 'subjects',
  'action' => 'details'
));

这是一个可行的解决方案。您必须传递参数,然后它才会开始工作。方法在这里-

  Router::connect('/subjects.details/:id', [
    'plugin' => 'subjects',
    'controller' => 'subjects',
    'action' => 'details'
   ],
   [
    'pass' => ['id']
   ]
  );

你的方法应该是这样的-

public function details($id){
  //....
}

希望对您有所帮助。谢谢