我正在尝试通过控制台 shell 从 CakePHP 2.1.2 发送电子邮件(最终通过 cron 作业)。我发送的 View 是一个日历,其中包含返回应用程序网页的链接。我发现的问题是网址不包含正确的路径,从我读到的内容来看,这是因为自从我使用控制台以来没有请求对象。例如,如果我在浏览器中创建 View ,我会得到如下链接:
http://localhost/ReportMonitor/scheduledReports/index/show_date:2012-06-10/result:GOOD
但是在使用相同代码的电子邮件中我得到了这个:
http://localhost/scheduledReports/index/show_date:2012-06-10/result:GOOD
很接近,但没有雪茄。
我一直在尝试找到可以在某个地方设置的全局变量,以便对应用程序子目录进行硬编码,但尚未找到任何有效的方法。这些链接是由如下代码创建的:
$newUrl = array();
$newUrl['controller'] = 'scheduledReports';
$newUrl['action'] = 'index';
$newUrl['url'] = array();
foreach ($data as $key => $value) {
$newUrl['show_date'] = "$year-$month-$key";
$newUrl['result'] = 'GOOD';
$data[$key]['num_complete'] = $this->Html->link(__('Complete: ') . $value['num_complete'], Router::reverse($newUrl, true), array('class' => 'green'));
我认为这是一个常见的功能(在控制台生成的电子邮件中发送有效的网址),但我就是无法弄清楚。
谢谢
最佳答案
在链接中使用full_base
选项
echo $this->Html->link(array(
'controller' => 'posts', 'action' => 'index', 'full_base' => true
));
并将 bootstrap.php
中的 FULL_BASE_URL
常量设置为您的基本 URL:
define('FULL_BASE_URL', 'http://www.domain.com/subdir');
如果您的应用程序使用路由,则需要在 shell 中包含路由:
App::uses('Router', 'Routing');
config('routes');
关于url - 从 CakePHP 2.1.2 控制台 Shell 创建完整 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11109785/