我想将路线保存到外部网址,所以,执行
echo $this->url('externalSite');
将返回
http://some-other-site.domain/somePage.php?list=modules
答案 0 :(得分:2)
如果有人想知道我是如何解决这个问题的,我把url放在应用程序配置中并写了一个视图和控制器助手来检索它
<强> module.config.php 强>:
// ...
'view_helpers' => array(
'invokables' => array(
'externalUrl' => 'MyModule\Helper\ExternalUrlHelper',
),
),
'controller_plugins' => array(
'invokables' => array(
'externalUrl' => 'MyModule\Helper\ExternalUrlHelper',
)
),
// ...
'external_urls' => array(
'home' => 'http://some-other-site.domain/somePage.php?list=modules',
),
<强> MyModule的/助手/ ExternalUrlHelper.php 强>:
<?php
namespace MyModule\Helper;
use Zend\Stdlib\DispatchableInterface as Dispatchable;
use Zend\View\Helper\AbstractHelper;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Mvc\Controller\Plugin\PluginInterface;
use InvalidArgumentException;
class ExternalUrlHelper extends AbstractHelper implements
ServiceLocatorAwareInterface, PluginInterface
{
/** @var \Zend\View\HelperPluginManager */
protected $sm;
/** @var Dispatchable */
protected $controller;
/**
* Set service locator
* @param ServiceLocatorInterface $serviceLocator
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->sm=$serviceLocator;
}
/**
* Get service locator
* @return ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->sm;
}
/**
* Set the current controller instance
*
* @param Dispatchable $controller
* @return void
*/
public function setController(Dispatchable $controller)
{
$this->controller=$controller;
}
/**
* Get the current controller instance
*
* @return null|Dispatchable
*/
public function getController()
{
return $this->controller;
}
/**
* @param string $name
* @return string
*/
public function __invoke($name)
{
return $this->getRouteByName($name);
}
/**
* @param string $name
* @return string
* @throws \InvalidArgumentException
*/
protected function getRouteByName($name)
{
$config=$this->sm->getServiceLocator()->get('config');
$routes=$config['external_urls'];
if (! isset($routes[$name])) {
throw new InvalidArgumentException(
sprintf('Route name %s not found', htmlspecialchars($name, ENT_QUOTES))
);
}
return $routes[$name];
}
}
视图中的:
<a href="<?php echo $this->externalUrl('home'); ?>">Main portal</a>
控制器中的:
$this->redirect()->toUrl($this->externalUrl('home'));