如何从 zend 2 设置我的 module.config.php 文件以便从控制器访问函数


How do i set my module.config.php file from zend 2 in order to access functions from a controller?

所以,我想从像这样的控制器访问一个函数 myproject/mycontroller/myfunction

这是路由文件的内容:

<?php

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',
                    ),
                ),
            ),
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application'Controller',
                        'controller'    => 'Index',
                        'action'        => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/application[/][:action][/:id]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'abstract_factories' => array(
            'Zend'Cache'Service'StorageCacheAbstractServiceFactory',
            'Zend'Log'LoggerAbstractServiceFactory',
        ),
        'aliases' => array(
            'translator' => 'MvcTranslator',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application'Controller'Index'  => 'Application'Controller'IndexController',
            'Application'Controller'Create' => 'Application'Controller'CreateController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
    // Placeholder for console routes
    'console' => array(
        'router' => array(
            'routes' => array(
            ),
        ),
    ),
);

这是控制器内容:

namespace Application'Controller;
use Zend'Mvc'Controller'AbstractActionController;
use Zend'View'Model'ViewModel;
class IndexController extends AbstractActionController{
    public function indexAction(){
        return new ViewModel();
    }
    public function addAction(){
        echo 1;
    }
    public function editAction(){
    }
    public function deleteAction(){
    }
}

实际上我想设置我的路线或更改它,但我不知道该怎么做才能访问链接myproject/index/add。现在我得到了404.

如果myproject是你的虚拟主机名,那么你就不能像这样访问你的函数,你必须遵循这个: [virtualhost]/[moduleName]/[controller]/[action]/[parmas]

在您的情况下,根据您的配置,您的模块似乎Application并且控制器Index,因此您可以访问add如下功能:http://myproject/application/index/add