zend2 向应用程序添加另一个控制器

zend2 adding another controller to application

我正在处理 ZendFramework2.4 项目。 我刚开始用一些代码填充标准框架应用程序的 IndexController,并想到了同一应用程序中的另一个控制器。

没有新模块,只有第二个控制器来保持代码分离。

当然我阅读了文档 Zend2: Routing and Controllers

但是无论我在做什么,我总是会收到类似 404 not found 的消息。 我找不到通往第二个控制器的路线。

当我阅读 modul.config.php 时,我想,这里没什么可做的....我是对还是错?

// The following is a route to simplify getting started creating
        // new controllers and actions without needing to create a new
        // module. Simply drop new controllers in, and you can access them
        // using the path /application/:controller/:action

但是除了实现控制器什么都不做也行不通...

文档有问题吗?

也许你有一个提示...

好的: 我已经尝试了你给出的两个例子。

我的 module.config.php 现在看起来像这样:

 'controllers' => array(
    'invokables' => array(
        'Index' => 'Application\Controller\IndexController',
        'Am'    => 'Application\Controller\AmController'
    ),
),

'router' => array(
    'routes' => array(                  

        'home' => array(
            'type' => 'Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Index',
                    'action' => 'index'
                ),
            ),
        ),

        'arznei' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/[:controller][/:action][/:id]',
                'constraints' => array(
                    'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Am',
                    'action'     => 'index',
                ),
            ),
        ),

    )
),

恕我直言,我认为这应该适用于 //myServer/(第一个规则 'home')和任何其他 uri,例如 //myServer/myController/myAction/25 .

但事实并非如此。

对于第二条路线,我总是得到“404 找不到对象”。 也许在apache2的配置中还有另一个选项出错了。 似乎重写在某些方面不正确。

据我目前所知,重写模块已加载并启用。我要检查那个问题。

你们都是对的。

我发现apache2-configuration中隐藏得很深的一个错误阻止了对上层配置的精确重写。

主要是确保选项 "AllowOverride" 设置为 "All" 并且 "FollowSymLinks" 也必须允许应用程序的 "public" 目录。

在我使用 openSUSE-Server 的特殊情况下,我在 /etc/apache2/default-server.conf:

中写了一个目录定义
<Directory "/srv/www/htdocs/myDirectory/public">
Options Indexes Multiviews FollowSymLinks
AllowOverride All
order allow,deny
Allow from all

</Directory>

您需要使用这些步骤在您的应用程序中添加控制器

  1. /module/Yourmodule/src/Yourmodule/Controller 中创建一个控制器 class,类似于

    <?php
    
    namespace Yourmodule\Controller;
    
    use Zend\Mvc\Controller\AbstractActionController;
    
    /**
     *
     * @author user
     *        
     */
    class YourController extends AbstractActionController {
    
        public function indexAction() {}
    
        // .... other codes
    
    }
    

    并将其命名为 YourController.php.

  2. 为操作创建视图 /module/Yourmodule/view/yourmodule/yourontroller/index.phtml

Remember the view file name should be same as the function name except Action here indexAction => index.phtml

  1. 添加路线,编辑module.config.php

    return array(
        'router' => array(
            'routes' => array(                  
    
                'your-test-route' => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route'    => '/your-test-uri',
                        'defaults' => array(
                            'controller' => 'Yourmodule\Controller\YourController',
                            'action' => 'index'
                        ),
                    ),
                ),
    
                // ... other routes 
            )
        ),
    
        'controllers' => array(
            'invokables' => array(              
                'Yourmodule\Controller\YourController' => 'Yourmodule\Controller\YourController'
            ),
        ),
        'view_manager' => array(
            'template_path_stack' => array(
                __DIR__ . '/../view',
            ),
        ),
    
        // ... other codes
    );
    
  2. 现在 运行 在您的网络浏览器中 http://yoursite.com/your-test-uri

您可以在一个模块中有多个控制器,但您需要设置路由以确定何时使用哪个控制器。由于你在问题中引用了 Akrabat 的相册模块,我将用它来说明:

相册模块教程展示了如何创建四个动作:indexActionaddActiondeleteActioneditAction;都在同一个控制器中。但是,假设 index 处理专辑集合,而其他操作每个操作一个专辑;我们决定有两个控制器:AlbumsController(其中包括 indexAction)和 AlbumController(其中包括 addActiondeleteActioneditAction).

在您的 module.config.php 文件中,您需要调用第二个控制器并创建一个指向它的备用路由:

 return array(
     'controllers' => array(
         'invokables' => array(
             'Album\Controller\Album' => 'Album\Controller\AlbumController',
             'Album\Controller\Albums' => 'Album\Controller\AlbumsController', // added second controller
         ),
     ),

     // The following section is new and should be added to your file
     'router' => array(
         'routes' => array(
             'album' => array(
                 'type'    => 'segment',
                 'options' => array(
                     'route'    => '/album[/:action][/:id]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                         'id'     => '[0-9]+',
                     ),
                     'defaults' => array(
                         'controller' => 'Album\Controller\Album',
                         'action'     => 'add',
                     ),
                 ),
             ),

             // add route to new controller
             'albums' => array(
                 'type'    => 'segment',
                 'options' => array(
                     'route'    => '/albums[/:action]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                     ),
                     'defaults' => array(
                         'controller' => 'Album\Controller\Albums',
                         'action'     => 'index',
                     ),
                 ),
             ),
         ),
     ),

 // ...

你们都说对了。

我发现apache2-configuration中隐藏得很深的错误阻止了对上层配置的精确重写。

主要是确保选项 "AllowOverride" 设置为 "All" 并且 "FollowSymLinks" 也必须允许应用程序的 "public" 目录。

在我使用 openSUSE-Server 的特殊情况下,我在 /etc/apache2/default-server.conf:

中写了一个目录定义

<Directory "/srv/www/htdocs/myDirectory/public">
Options Indexes Multiviews FollowSymLinks
AllowOverride All
order allow,deny
Allow from all

</Directory>

在 linux-distributions 中执行此操作的位置不同,因此您必须看一下,是否必须放置此...在大多数情况下,类似于 "httpd.conf".

这解决了我的问题。

感谢您提供有关路由器配置的重要提示!