0
votes

Hello to all zend coders. I am newbie to zend, doing code for manage pages through DB. I want to use URL for these pages like this:

www.domainname.com/about-us

www.domainname.com/contact-us ...

Table name is 'cms_page' & attributes are:

`id` int(11) NOT NULL AUTO_INCREMENT,

  `title` varchar(255) NOT NULL,

  `page_key` varchar(255) NOT NULL,

  `content` longtext NOT NULL,

  `added_on` datetime NOT NULL,

I have done R&D for this and here are code:

// in module.config.php

'navigation' => array(
        'default' => array(
            'account' => array(
                'label' => 'Account',
                'route' => 'node',
                'params' => array(
                            'id' => '2',
                            ),
                'pages' => array(
                    'home' => array(
                        'label' => 'Dashboard',
                        'route' => 'node',
                        'params' => array(
                                    'id' => '1',
                                    'link' => '/test'
                                    ),

                    ),
                    'login' => array(
                        'label' => 'Sign In',
                        'route' => 'node',
                        'params' => array(
                                    'id' => '5',
                                    'link' => '/about-us'
                                    ),

                    ),
                    '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,
            ),

        ),
    ),

// In Module.php


 public function onBootstrap(MvcEvent $e)
    {
       $app =   $e->getApplication();
       $sm  =   $app->getServiceManager();
       $sm->get('translator');
       $sm->get('viewhelpermanager')->setFactory('controllerName', function($sm) use ($e) {
        $viewHelper = new View\Helper\ControllerName($e->getRouteMatch());
        return $viewHelper;
    });

        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $sm->setFactory('Navigation', 'Zend\Navigation\Service\DefaultNavigationFactory', true);
        $alias = $sm->get('Application\Router\Alias');
        $nav = $sm->get('Navigation');
        $alias->setNavigation($nav);
    }

public function getServiceConfig()
    {
        return array(
    'factories' => array(
            'Application\Router\Alias' => function($sm) {
                $alias = new \Application\Router\Alias('/node[/:id]');
                return $alias;
            },
          )
    );
}   
//  Application\src\Application\Router

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;
use Zend\Mvc\Router\Http\Segment as Segment;

class Alias extends 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 getParams();
                if (isset($params['link']) && $params['link']==$path){
                   echo $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, array $options)
    {

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

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

}

When I run this url www.domainname.com/about-us on browser getting 404 error. Please help me where am I wrong and what code need to add or edit?

1
Heyho, i don't have enough time to digg into this, but there is already a very great module (caching included) that does exactly what you need! It's called PhlySimplePage by Matthew Weier O'Phinney (ZF2 Architect)Sam

1 Answers

0
votes

You can use the a Zend_Controller_Router_Route as an alternative. Here is a quick tutorial on how to do so:

http://www.codexperience.co.za/post/hiding-url-parameters-names-using-zend-routers