简体   繁体   English

Zend 2框架简单的网站路由

[英]Zend 2 framework simple website routing

I'm trying simple routing in zend for educational purposes. 我正在出于教育目的尝试在zend中进行简单路由。 Before i say anything i downloaded 3 zend tutorials, 4 pdf's and I'm still stuck on the simplest tasks. 在我发表任何言论之前,我下载了3个zend教程,4个pdf,但我仍然停留在最简单的任务上。 I even read the official documentation but still i can't understand how it works. 我什至阅读了官方文档,但仍然不明白它是如何工作的。

I'm trying to create simple bootstrap Home About and Contact page and found no luck in how routing works. 我正在尝试创建一个简单的引导程序“首页关于和联系”页面,但发现路由工作方式没有任何运气。 I tried configuring module/ModuleName/module.config.php adding custom routes but with no luck. 我尝试配置module / ModuleName / module.config.php添加自定义路由,但没有运气。 Do i need to have for every static page like About and Contact seperate modules or one module and seperate controllers for each page? 我是否需要每个静态页面(例如“关于”和“联系”)单独的模块,或者每个页面都有一个模块和单独的控制器? And how to handle routing www.example.com/about all i got is this and it's not working 以及如何处理路由www.example.com/有关我的全部信息,这是行不通的

This is my module.config.php 这是我的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',
                    ),
                ),
            ),

In src/Application/Controler i have IndexControler.php and AboutController.php and in view/application/index index.phtml and about phtml. 在src / Application / Controler中,我有IndexControler.php和AboutController.php,在view / 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();
    }

}

I think you are missing invokables. 我认为您缺少发票。 In you above module.config.php file you should also add these below lines so that your code will start working. 在您上面的module.config.php文件中,您还应该在下面的行中添加这些内容,以便您的代码开始工作。

Add this below code in 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',
             ),
    ),

This link will give a better idea what is missing. 该链接将使您更好地了解缺少的内容。 http://framework.zend.com/manual/current/en/user-guide/routing-and-controllers.html http://framework.zend.com/manual/current/zh-CN/user-guide/routing-and-controllers.html

Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM