简体   繁体   中英

Zend 2 framework simple website routing

I'm trying simple routing in zend for educational purposes. Before i say anything i downloaded 3 zend tutorials, 4 pdf's and I'm still stuck on the simplest tasks. 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. 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

This is my 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.

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.

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

Good Luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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