PHP   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – 如何在ZF2中的控制台控制器中创建URL?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个控制台控制器和一个发送电子邮件的动作(在下面的module.config.php中定义)
'console' => array(
    'router' => array(
        'routes' => array(
            'cronroute' => array(
                'options' => array(
                    'route'    => 'sendEmails','defaults' => array(
                        'controller' => 'Application\Controller\Console','action' => 'send-emails'
                    )
                )
            ),)
    )
),

在操作中,我想发送一封电子邮件,其中包含指向该网站上其他操作的链接.这通常使用URL View Helper来完成,但由于request类型是Console而不是http,因此不起作用.我试图创建一个http请求,但我不知道如何给它站点域或Controller / Action链接.

我的控制器代码:

$vhm = $this->getserviceLocator()->get('viewHelpeRMANager');
$url = $vhm->get('url');
$urlString = $url('communication',array('action' => 'respond','id' => $id,array('force_canonical' => truE));

这会引发错误:

======================================================================
   The application has thrown an exception!
======================================================================
Zend\Mvc\Router\Exception\RuntimeException
request URI has not been set

如何在具有站点方案,域和路径/到/ action的控制台控制器中创建http请求?我如何将其传递给URL View Helper?

解决方法

以下是此问题的解决方法:
<?php

// Module.php

use Zend\View\Helper\ServerUrl;
use Zend\View\Helper\Url as UrlHelper;
use Zend\Uri\http as httpUri;
use Zend\Console\Console;
use Zend\ModuleManager\Feature\ViewHelperProviderInterface;

class Module implements ViewHelperProviderInterface
{

    public function getViewHelperConfig()
    {
        return array(
            'factories' => array(
                'url' => function ($HelperPluginManager) {
                    $serviceLocator = $HelperPluginManager->getserviceLocator();
                    $config = $serviceLocator->get('Config');

                    $viewHelper =  new UrlHelper();

                    $routerName = Console::isConsole() ? 'httpRouter' : 'Router';

                    /** @var \Zend\Mvc\Router\http\TreeRouteStack $router */
                    $router = $serviceLocator->get($routerName);

                    if (Console::isConsole()) {
                        $requesturi = new httpUri();
                        $requesturi->setHost($config['website']['host'])
                            ->setscheR_249_11845@e($config['website']['scheR_249_11845@e']);
                        $router->setrequesturi($requesturi);
                    }

                    $viewHelper->setRouter($router);

                    $match = $serviceLocator->get('application')
                        ->getMvcEvent()
                        ->getRouteMatch();

                    if ($match instanceof RouteMatch) {
                        $viewHelper->setRouteMatch($match);
                    }

                    return $viewHelper;
                },'serverUrl' => function ($HelperPluginManager) {
                    $serviceLocator = $HelperPluginManager->getserviceLocator();
                    $config = $serviceLocator->get('Config');

                    $serverUrlHelper = new ServerUrl();
                    if (Console::isConsole()) {
                        $serverUrlHelper->setHost($config['website']['host'])
                            ->setscheR_249_11845@e($config['website']['scheR_249_11845@e']);
                    }

                    return $serverUrlHelper;
                },),);
    }
}

当然,您必须在config中定义默认主机和方案值,因为无法在控制台模式下自动检测它们.

大佬总结

以上是大佬教程为你收集整理的php – 如何在ZF2中的控制台控制器中创建URL?全部内容,希望文章能够帮你解决php – 如何在ZF2中的控制台控制器中创建URL?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。