0

I'd like to save a route to an external URL, and so, doing

echo $this->url('externalSite');

would return

http://some-other-site.domain/somePage.php?list=modules
3
  • 1
    Why? :) You can just write our the URL. If the domain is variable, use a config-variable for that. The Router is a ZF2 internal thingy. Not to be used for external stuff. There's just no use-case for that. Commented Dec 17, 2013 at 19:21
  • This url is used site-wide, multiple times. Commented Dec 18, 2013 at 7:06
  • Post your Solution as such and re-edit your question to question only please. You can then accept your solution so this question will be flagged as completed ;) And as a side-note, the way you did it is exactly how it's supposed to be done. Good thinking with the exception, too! Commented Dec 18, 2013 at 11:25

1 Answer 1

2

In case anyone wondered how I resolved this issue, I put the url inside the application config and wrote a view&controller helper to retrieve it

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/Helper/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];
    }
}

in a view:

<a href="<?php echo $this->externalUrl('home'); ?>">Main portal</a>

in a controller:

$this->redirect()->toUrl($this->externalUrl('home'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.