aliaszend-framework2zend-navigationzend-router

ZF2: create url aliases in router


I'm new to Zend Framework 2 and i want to learn this framework. I want to create url aliases in router. For example, I have defined something like this in module.config.php

'router' => array(
    'routes' => array(

        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index',
                ),
            ),
        ),
        'node' => array(
            'type'    => 'Application\Controller\AliasSegment',
            'options' => array(
                'route'    => '/node[/:id]',
                'constraints' => array(
                    'id' => '[0-9]+'
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                    'id'            => '0'
                ),
            ),
            'may_terminate' => true,
        ),

    ),
),

When i type www.myapp.local/node/1 it routes to the default action in default controller of my application. What i want is a router extension that can handle aliases for url paths. For example:

www.myapp.local/node/1 = www.myapp.local/aboutus
www.myapp.local/node/2 = www.myapp.local/company/gallery

I know that it was possible in ZF. Here is a link to tutorial how to achieve this in ZF: friendly urls I know that this is in Polish but code is self-explanatory i think :)

The idea is to use url helper to assembly valid url using aliases or normal segments (node/[:id])

I've already created AliasSegment class in my Application\Controller folder but it shows me an error:

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Application\Controller\AliasSegment' in C:\xampp\htdocs\industengine\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:450 Stack trace: #0 

My AliasSegment class (incomplete):

    <?php

    namespace Zend\Mvc\Router\Http;

    use Traversable;
    use Zend\Mvc\Router\Exception;
    use Zend\Stdlib\ArrayUtils;
    use Zend\Stdlib\RequestInterface as Request;

 class AliasSegment extends Segment
 {


    public function match(Request $request, $pathOffset = null)
    {


    }

 }

I was looking for an answer for hours and i couldnt find anything. Please tell me at least what I'm doing wrong, where to insert a code or maybe You know better sollution?

I'm not looking for ready application. I want to learn something but i would appreciate if You can tell me an answer in details :)

Thanks in advance and sorry for my English :)

EDITED:

My custom router is working now. At this moment aliases are hardcoded but it works.

My AliasSegment class looks now:

<?php

namespace Application\Controller;

use Traversable;
use Zend\Mvc\Router\Exception;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Mvc\Router\Http;

class AliasSegment extends \Zend\Mvc\Router\Http\Segment
{


    public function match(Request $request, $pathOffset = null)
    {
        $uri  = $request->getUri();
        $path = $uri->getPath();

        //sample logic here
        //for /about/gallery uri set node id to 1
        //todo: get action, controller and module from navigation        
        if($path == '/about/gallery'){
            $uri->setPath('/node/1');
            $request->setUri($uri);
        }

        return parent::match($request, $pathOffset);

    }

    protected function buildPath(array $parts, array $mergedParams, $isOptional, $hasChild)
    {

        if(isset($mergedParams['link'])){
            return $mergedParams['link'];
        }

        return parent::buildPath($parts, $mergedParams, $isOptional, $hasChild);
    }

}

In this case /about/gallery is an alias to /node/1. Both adresses are correct. The buildPath function returns alias path correctly. Well, I hope this would be usefull for somebody :)

However i want to setup it in Zend_Navigation with additional parameter named 'link'.

I've done 50% of what i want to achieve however now I have problem to get Zend_Navigation from my router. I don't know how to pass it. I guess it should be something like this:

$sm = $this->getServiceLocator();
$auth = $sm->get('Navigation');

It works in my IndexController but doesnt work in my AliasSegment. I need to find in navigation array nodes with 'link' parameter.

EDIT

I've found solution. The answer is below.


Solution

  • OK, I've made it. The important thing for this Thread: ZF2: How to get Zend\Navigation inside custom route?.

    You can use any segment type route. But this may need a little modifications to match function.

    If navigation's single page will have 'link' param, the url will be converted to 'link' string but other params will stay behind it. Just think of it as an overlay for default URI of current route.

    I had to modify my custom route class a little bit. First of all, i had to change its namespace to Application\Router. Here is a full class:

    // EDIT - file within ModuleName/src/Router/Alias.php
    namespace Application\Router;
    
    use Traversable;
    use Zend\Mvc\Router\Exception;
    use Zend\Stdlib\ArrayUtils;
    use Zend\Stdlib\RequestInterface as Request;
    use Zend\Mvc\Router\Http;
    
    class Alias extends Http\Segment
    {
        private static $_navigation = null;
    
        public function match(Request $request, $pathOffset = null)
        {
            $uri  = $request->getUri();
            $path = $uri->getPath();
    
    
            $items = self::$_navigation->findAllBy('route', 'node');
            $params = null;
    
            if($items != null){
                $t = sizeof($items);
                for ($i=0; $i < $t; $i++) { 
                    $item = $items[$i];
                    $params = $item->getParams();
                    if (isset($params['link']) && $params['link']==$path){
                        $uri->setPath('/'.$item->getRoute().'/'.$params['id']);
                        $request->setUri($uri);
                        break;
                    }
                }
            }
    
            return parent::match($request, $pathOffset);
        }
    
        public function setNavigation($navigation){
            self::$_navigation = $navigation;
        }
    
        protected function buildPath(array $parts, 
                                     array $mergedParams, $isOptional, $hasChild)
        {
    
            if(isset($mergedParams['link'])){
                return $mergedParams['link'];
            }
    
            return parent::buildPath($parts, $mergedParams, 
                                                   $isOptional, $hasChild);
        }
    
    }
    

    here is sample part of module.config.php:

    'navigation' => array(
            // The DefaultNavigationFactory we configured in (1) uses 'default' as the sitemap key
            'default' => array(
                // And finally, here is where we define our page hierarchy
                'account' => array(
                    'label' => 'Account',
                    'route' => 'node',
                    'params' => array(
                                'id' => '2',
                                ),
                    'pages' => array(
                        'home' => array(
                            'label' => 'Dashboard',
                            'route' => 'node',
                            'params' => array(
                                        'id' => '8',
                                        'link' => '/about/gallery'
                                        ),
    
                        ),
                        'login' => array(
                            'label' => 'Sign In',
                            'route' => 'node',
                            'params' => array(
                                        'id' => '6',
                                        'link' => '/signin'
                                        ),
    
                        ),
                        'logout' => array(
                            'label' => 'Sign Out',
                            'route' => 'node',
                            'params' => array(
                                        'id' => '3',
                                        ),
                        ),
                    ),
                ),
            ),
        ),
        'router' => array(
            'routes' => array(
    
                'home' => array(
                    'type' => 'Zend\Mvc\Router\Http\Literal',
                    'options' => array(
                        'route'    => '/',
                        'defaults' => array(
                            'controller' => 'Application\Controller\Index',
                            'action'     => 'index',
                        ),
                    ),
                ),
                'node' => array(
                    'type'    => 'Application\Router\Alias',
                    'options' => array(
                        'route'    => '/node[/:id]',
                        'constraints' => array(
                            'id' => '[0-9]+'
                        ),
                        'defaults' => array(
                            '__NAMESPACE__' => 'Application\Controller',
                            'controller'    => 'Index',
                            'action'        => 'index',
                            'id'            => '0'
                        ),
                    ),
                    'may_terminate' => true,
                ),
    
            ),
        ),