CakePHP:通过url字符串获取模型实例


CakePHP: Get model instance by url string

我有一个CakePHP网站,navigaton链接存储在数据库中。我想要的是一些导航条目调用自定义函数,该函数将返回关于链接的一些额外的动态数据:我想为链接"空缺"添加文章计数。我可以在一个模型上调用一个函数来返回total count。此链接将呈现在每一页上。

因此,我需要获得合适的模型实例,但不是针对当前请求,而是url指向的请求。

所以基本上我有url"/en/vilements"。我可以通过以下方式获取控制器名称:

 $urlInfo = Router::parse("/en/vacancies");
 $controllerName = $urlInfo['controller'];

要做到这一点,可靠的方法是什么?

欢迎为这个问题提供任何其他解决方案。

假设您有在Model中收集导航链接数据的方法。

App::import('Controller', $controllerName);
$controller = new $controllerName;
$controller->loadModel('YourModel');
$yourModel = $controller->YourModel;
$yourData = $yourModel->your_method();

还有很多其他方法可以做到这一点。但是,如果不知道你将在哪里调用这个函数,我就无法再提供建议了。