ZF3:未找到操作和视图

ZF3: Action and view doesn't found

在我的应用程序中,我在控制器中创建了第二个动作。当我使用 url http://local.domain I've got the correct page so it's called the correct controller. But If I want to make this call http://local.domain/liga-futbol-1 调用应用程序时,它不起作用并且出现此错误:

A 404 error occurred Page not found. The requested URL could not be matched by routing.

No Exception available

IndexController.php

namespace Stats\Controller;

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController {
    public function indexAction()
    {
        //This action serves the page
        return [];
    }

    public function ligaFubtol1Action(){

        return [];
    } }

module.config.php

namespace Stats;

use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
        ],
    ],
    'router' => [
        'routes' => [
            'home' => [
                'type'    => 'Literal',
                'options' => [
                    // This works!!! => http://local.domain
                    'route'    => '/',
                    'defaults' => [
                        'controller'    => Controller\IndexController::class,
                        'action'        => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    // This doesn't work!!! http://local.domain/liga-futbol-1
                    'liga-futbol-1' =>  [
                        'type'  =>  Segment::class,
                        'options'   =>  [
                            'route' =>  '/liga-futbol-1',
                            'defaults'  =>  [
                                'controller'    => Controller\IndexController::class,
                                'action'        =>  'ligaFutbol1'
                            ],
                        ],
                        'may_terminate' =>  true,
                        'child_routes'  =>  [
                        ],
                    ],                    
                ],
            ],       
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'stats/index/index'       => __DIR__ . '/../view/stats/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
        /*
         * Con este array de parámetros permitimos enviar datos y no mostrar vista
         */
        'strategies' => [
            'ViewJsonStrategy',
        ],           
    ],
];

目录:

我检查了“/dir_project/data/cache”中的缓存,没有任何内容。

我做错了什么?

查看 home 路由的 route 选项:它设置为 /liga-futbol-1 路由是 home 路由的子路由,因此它的 URL 是以下的“总和”:

  • home URL: /
  • liga-futbol-1 URL: /liga-futbol-1

结果://liga-futbol-1home/liga-futbol-1 路线的 URL。

如果您只想要 /liga-futbol-1 之类的东西,有两种解决方案:

  1. 使 liga-futbol-1 路由独立于 home 路由(即不是它的子路由):

    'routes' => [
        'home' => [
            'type' => Literal::class,
            'options' => [
                'route' => '/',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'index'
                ]
            ]
        ],
        'liga-futbol-1' => [
            'type' =>  Segment::class,
            'options' =>  [
                'route' => '/liga-futbol-1',
                'defaults' =>  [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'ligaFutbol1'
                ]
            ]
        ]
    ]
    
  2. liga-futbol-1route 选项的开头删除 /:

    'liga-futbol-1' =>  [
        'type' =>  Segment::class,
        'options' => [
            'route' => 'liga-futbol-1',
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action'     => 'ligaFutbol1'
                ]
            ]
        ]