【问题标题】:how to route pages to the root of a site using zend 2 routing如何使用zend 2路由将页面路由到站点的根目录
【发布时间】:2014-10-08 11:54:20
【问题描述】:

我正在使用 zend 框架 2。我想在站点的根目录上放置几个页面。即

www.exampleSite.com/siteMap

我知道如何获取子目录中的页面。

www.exampleSite.com/support/helppage

'router' => array(
        'routes' => array(
            'supportsec' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/support[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'support\Controller\supportController',
                        'action' => 'index',
                    ),
                   ),
          ), ),  ),

我也知道如何路由到主页,即

 'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Members\Controller\HomeController',
                        'action'     => 'index',
                    ),
                ),
            ),

困惑在于如何将其他页面放置在网站的根目录中。

提前感谢您的帮助

我听从了@exlord 的建议,并将它放在我的模块中。

'my-static-page-1' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/my-static-page-1',
                    'defaults' => array(
                        'controller' => 'Members\Controller\HomeController',
                        'action'     => 'my-static-page-1',
                    ),
                ),
            ),

我现在可以拥有如下页面:

www.exampleSite.com/examplepage

但是路由只是将路由发送到索引

'成员\控制器\家庭控制器'

但是我需要将路由发送到示例页面的操作函数,即

examplepageAction(){

}

我需要做什么?

【问题讨论】:

  • 您能否详细说明您对路由的困惑?您能否提供一些路由示例以更清楚地了解您的问题?

标签: routing zend-framework2


【解决方案1】:

对于动态页面名称:

 'my-static-pages' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route'    => '/[:my-page]',
                    'defaults' => array(
                        'controller' => 'Members\Controller\HomeController',
                        'action'     => 'index',
                    ),
                ),
            ),

请注意,这与主路由相同,它使用索引操作,因此在索引操作中,您应该检查是否设置了 my-page 参数并采取相应措施。

或者您可以为每个页面定义静态路由:

 'my-static-page-1' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/my-static-page-1',
                    'defaults' => array(
                        'controller' => 'Members\Controller\HomeController',
                        'action'     => 'my-static-page-1',
                    ),
                ),
            ),

更新:

public function indexAction()
{
    $page = $this->params()->fromRoute('my-page', false);
    if($page){
        //return a view model according to the $page parameter            
    }

    //return the original index action view model
}

【讨论】:

  • 嗨,Exlord。非常感谢你的帮助。我有点困惑。你是什​​么意思,我应该检查我的页面参数是否设置并采取相应的行动。我通常将页面发送到操作页面。如果页面设置为索引页面,我需要执行什么操作。我以前从未遇到过这种情况
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多