TLDR:如何在 Controller 中创建 URL,类似于如何使用 HtmlHelper 在 View 中创建 URL?
问题:
我想在我的 Controller 中打印 Controller 操作的 url(因为我在 Controller 中创建了我的 JSON 字符串,而不是在 View 中)
在 View
, 我可以用 $this->Html->url()
,但是在 Controller 中呢?
我应该使用定义的常量,如 APP_DIR
+ Controller 名称 + Controller Action ?)
最佳答案
使用路由器类。
$url = Router::url([
'controller' => 'Articles',
'action' => 'index',
'?' => ['page' => 1],
'#' => 'top'
]);
或同样的事情,但在更常见/更简单的情况下:
$url = Router::url(['controller' => 'Articles', 'action' => 'index']);
注意:在 Cake2.x 中,“文章”将是小写的。
CakePHP 2.x Router documentation
CakePHP 3.x 'Generating URLs' documentation
关于cakephp - 如何在 Controller 中创建一个 URL,如 HtmlHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11915441/