cakephp - 如何在 Controller 中创建一个 URL,如 HtmlHelper

标签 cakephp html-helper cakephp-2.x cakephp-3.x

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/

相关文章:

php - Cakephp 3 多模型操作的事务管理

php - 蛋糕 php 包含在包含

cakephp - CakePHP中如何显示登录用户的记录?

c# - 从 HTML 帮助程序中提取显示名称和描述属性

mysql - CakePHP 分页器中的数字排序

php - CakePHP 在登录时保存日期/时间

asp.net-mvc - 为什么许多 MVC html Helpers 使用委托(delegate)作为输入参数

c# - ASP.NET MVC 3 : Override "name" attribute with TextBoxFor

cakephp - 如何使用新生成的 .pot 文件更新现有的 .po 文件?

CakePHP 2.x 翻译后备