zf2中内容页面的自定义路由

时间:2013-07-31 09:55:29

标签: zend-framework2 zend-route zend-router

你好所有的zend编码员。我是zend的新手,通过DB为管理页面做代码。 我想对这些页面使用URL,如下所示:

www.domainname.com/about-us

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

表名是'cms_page'&属性是:

`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,

我为此做过R& D,这里是代码:

// 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());
    }

}

当我在浏览器上运行此网址www.domainname.com/about-us时出现404错误。 请帮助我哪里错了,需要添加或编辑哪些代码?

1 个答案:

答案 0 :(得分:0)

您可以使用a Zend_Controller_Router_Route作为替代方案。这是一个如何操作的快速教程:

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