dongxu0690 2017-09-16 19:22
浏览 46
已采纳

ZF3:未找到操作和视图

In my application I have created a second action in a controller. When I call the application with the 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 it doesn't work and I've got this error:

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',
        ],           
    ],
];

Directories:

enter image description here

I have checked my cache in "/dir_project/data/cache" and there is nothing.

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dsceme82487 2017-09-16 20:05
    关注

    Have a look at the route option of the home route: it’s set to /. The liga-futbol-1 route is a child route of the home route, so its URL is a “sum” of:

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

    In result: //liga-futbol-1 is the URL of the home/liga-futbol-1 route.

    If you want just something like /liga-futbol-1, there are two solutions:

    1. Make the liga-futbol-1 route independent of the home route (i.e. not its child):

      '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. Remove / from the beginning of liga-futbol-1’s route option:

      'liga-futbol-1' =>  [
          'type' =>  Segment::class,
          'options' => [
              'route' => 'liga-futbol-1',
                  'defaults' => [
                      'controller' => Controller\IndexController::class,
                      'action'     => 'ligaFutbol1'
                  ]
              ]
          ]
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。