CakePHP 3:Shell中的路由

时间:2017-12-22 23:27:56

标签: php shell cakephp cakephp-3.x

我有Shell来处理不同的Cronjob任务。在这样的任务中,我想生成一个将通过电子邮件发送的链接。为此,使用Cake\Routing\Router应该是一个选项。代码如下所示:

namespace App\Shell\Task;

use Cake\Console\Shell;
use Cake\Routing\Router;

class ActivitiesSendTask extends Shell
{
  public function main()
  {
    $url = Router::url(['controller' => 'Posts', 'action' => 'index'], true);
    // ...

从终端呼叫任务始终会为MissingRouteException电话投出Router::url()

Exception: A route matching "array ('controller' => 'Posts', ...)" could not be found.
App.fullBaseUrl中{p> bootstap_cli.php的设置正确,如https://book.cakephp.org/3.0/en/console-and-shells.html#routing-in-shells-cli

中所述

但似乎在config/routes.php中定义的应用路由在cli环境中不可用。甚至还没有创建到/:controller /:action的简单路径。

如何解决此问题并在shell任务中使应用路由可用?

修改bin/cake routes的输出显示以下内容:

+--------------------------------+-----------------------------------+------------------------------------------------------------------------------------------------+
| Route name                     | URI template                      | Defaults                                                                                       |
+--------------------------------+-----------------------------------+------------------------------------------------------------------------------------------------+
| news:home                      | /                                 | {"controller":"News","action":"home","language":"","plugin":null}                              |
| pages:display                  | /pages/*                          | {"controller":"Pages","action":"display","language":"","plugin":null}                          |
| contacts:index                 | /contact/*                        | {"controller":"Contacts","action":"index","language":"","plugin":null}                         |
| search:index                   | /search*                          | {"controller":"Search","action":"index","language":"","plugin":null}                           |
...
| _controller:index              | /:controller                      | {"action":"index","language":"","plugin":null}                                                 |
| _controller:_action            | /:controller/:action/*            | {"action":"index","language":"","plugin":null}                                                 |

0 个答案:

没有答案