【问题标题】:Creating full url from CakePHP 2.1.2 Console Shell从 CakePHP 2.1.2 Console Shell 创建完整的 url
【发布时间】:2012-06-19 21:33:37
【问题描述】:

我正在尝试通过控制台 shell(最终通过 cron 作业)从 CakePHP 2.1.2 发送电子邮件。我发送的视图是一个带有返回应用程序网页链接的日历。我发现的问题是 url 不包含正确的路径,从我读到的内容来看,这是因为我使用控制台后没有请求对象。例如,如果我在浏览器中创建视图,我会得到如下链接:

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'));

我认为这是一个常见的功能(在控制台生成的电子邮件中发送有效的 url),但我就是想不通。

谢谢

【问题讨论】:

    标签: url cakephp console


    【解决方案1】:

    在链接中使用full_base 选项

    echo $this->Html->link(array(
        'controller' => 'posts', 'action' => 'index', 'full_base' => true
    ));
    

    并将bootstrap.php 中的FULL_BASE_URL 常量设置为您的基本网址:

    define('FULL_BASE_URL', 'http://www.domain.com/subdir');
    

    如果您在应用中使用路由,则需要在 shell 中包含路由:

    App::uses('Router', 'Routing');
    config('routes');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 2013-04-06
      • 2015-03-17
      相关资源
      最近更新 更多