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

时间:2015-06-01 20:23:41

标签: controller routing zend-framework2

我正在研究ZendFramework2.4项目。 我刚刚开始填充标准骨架应用程序的IndexController,其中包含一些代码并考虑了同一应用程序中的另一个控制器。

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

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

但无论我在做什么,我总会得到像404这样的东西。 我没有到第二个控制器的路线。

当我读到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',
                ),
            ),
        ),

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

但事实并非如此。

对于第二条路线,我总是得到'404找不到对象'。 也许在apache2的配置中还有另一个选项出错。 似乎重写在某种程度上是不正确的。

目前我已经看到了重写模块的加载和启用。我要查看那个问题。

你们两个都是对的。

我发现在apache2配置中隐藏了一个错误,阻止了对上层配置的精确重写。

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

在我使用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>

3 个答案:

答案 0 :(得分:0)

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

  1. /module/Yourmodule/src/Yourmodule/Controller中创建一个类似

    的控制器类
    <?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

  3. 创建视图
      

    请记住,视图文件名应与函数名相同,但操作此处indexAction =&gt; 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

    3. 中运行

答案 1 :(得分:0)

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

相册模块教程展示了如何创建四个操作:indexActionaddActiondeleteActioneditAction;都在同一个控制器中。但是,让我们说,因为index处理专辑集合,而其他动作各自操纵一张专辑;我们决定有两个控制器:AlbumsController(包括indexAction)和AlbumController(包括addActiondeleteAction和{{1} })。

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

editAction

答案 2 :(得分:0)

你们两个都是对的。

我发现在apache2配置中隐藏了一个错误,阻止了对上层配置的精确重写。

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

在我使用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-distribution中是不同的,所以你必须看看,你必须放置它...在大多数情况下像“httpd.conf”。

这解决了我的问题。

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