zf2 URL Helper customization to add achnor for ang

2019-09-03 12:13发布

问题:

I have a following Link in zf2

<a href="<?php echo $this->url('application/default',array('controller'=> 'enquiries', 'action'=>'index')) ?>"><?php echo $this->translate('Enquiries') ?></a>

and this router in zf2

'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),

which convert above link into

<a href="/application/enquiries/index">Enquiries</a>

which is fine.

I am using angularjs, which uses anchor fragments a lot. How can I add achor "#/active" on above zf2 link url, so target url look something like this.

<a href="/application/bookingenquiries/index#/active">Active Enquiries</a>

回答1:

do you have a reason why you are not just adding the anchor manually after the url definition url(...).'#/active' ?

if you have, try this:

'route' => '/[:controller[/:action[#/:anchor]]]',

UPDATE:

I checked the source for router and uri and url , it seems that url view helper accepts a 'fragment' key for the third option so try this :

url('[route]',array([params]),array('fragment'=>'/active'));

Zend/View/Helper/Url

Zend/Mvc/Router/Http/TreeRouteStack

Zend/Uri/Uri



回答2:

Create following view Helper by extending Url

namespace Application\View\Helper;

use Zend\View\Helper\Url;

class MyUrl extends Url {

    public function __invoke($name = null, $params = array(), $options = array(), $reuseMatchedParams = false) {

        if (isset($params['anchor']) && !empty($params['anchor'])) {
            $anchor = $params['anchor'];
            unset($params['anchor']);
        } else {
            $anchor = '';
        }

        $link = parent::__invoke($name, $params, $options, $reuseMatchedParams);

        return $link . $anchor;
    }

}

In Module file add following in factory to add newly created helper

public function getViewHelperConfig() {

    return array(
        'factories' => array(
            'myUrl' => function($sm) {
                $locator = $sm->getServiceLocator(); 

                $helper = new \Application\View\Helper\MyUrl;
                $router = \Zend\Console\Console::isConsole() ? 'HttpRouter' : 'Router';
                $helper->setRouter($locator->get($router));

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

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

                return $helper;
            },
        ),
    );
}

Add anchor in URL as parameter

<a href="<?php echo $this->myUrl('application/default',array('controller'=> 'enquiries', 'action'=>'index', 'anchor'=>'#/active')) ?>"><?php echo $this->translate('Enquiries') ?></a>