Zend 2框架简单的网站路由

时间:2016-02-07 13:35:12

标签: php twitter-bootstrap zend-framework

我在zend尝试简单路由以用于教育目的。在我说任何事情之前,我下载了3个zend教程,4个pdf&s,而且我仍然坚持最简单的任务。我甚至阅读了官方文档,但我仍然无法理解它是如何工作的。

我正在尝试创建简单的引导程序Home About和Contact页面,但发现路由的工作方式没有运气。我尝试配置module / ModuleName / module.config.php 添加自定义路线,但没有运气。我是否需要为每个静态页面提供诸如关于和联系单独的模块或一个模块以及每个页面的单独控制器?如何处理路由www.example.com/about我得到的就是这个并且它无法正常工作

这是我的module.config.php

namespace Application;

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),

            'about' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/about',
                    'defaults' => array(
                        'controller' => 'Application\Controller\About',
                        'action'     => 'index',
                    ),
                ),
            ),

在src / Application / Controler中我有IndexControler.php和AboutController.php 并在视图/ application / index index.phtml和关于phtml。

AboutController

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http:/o/github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AboutController extends AbstractActionController
{
    public function indexAction()
    {
        return new ViewModel();
    }

}

1 个答案:

答案 0 :(得分:0)

我认为你缺少invokables。在您上面的module.config.php文件中,您还应该添加以下行,以便您的代码开始工作。

在module.config.php中添加以下代码

'controllers' => array(
        'invokables' => array(
            'Applcation\Controller\Index' => 'Application\Controller\IndexController',
            'Applcation\Controller\About' => 'Application\Controller\AboutController',
            'Applcation\Controller\Contact' => 'Application\Controller\ContactController',
             ),
    ),

此链接可以更好地了解缺少的内容。 http://framework.zend.com/manual/current/en/user-guide/routing-and-controllers.html

祝你好运