使用Laminas Framework时出现404错误有人可以协助吗?

问题描述

错误是:

发生404错误 找不到页面

请求的URL无法与路由匹配。 没有可用的例外

我已经在Composer中注册了我的模块和名称空间,然后运行了Composer dump-autoload。我的代码如下:

module \ Album \ config \ module.config.PHP

name="Kenza"
height_m=float(input("enter the height:")
weight_kg=input("enter the weight :")
bmi=weight_kg/(height**2)
print("bmi is:")
print(bmi)
if  bmi>=25 :
    print(name)
    print("is overweight")
else:
if bmi <25 and bmi > 18.5:
    print(name)
    print("is normal weight")
else:
    if bmi<18.5:
        print(name)
        print("she is underweight")

module \ Album \ src \ Module.PHP

<?PHP

declare(strict_types=1);

namespace Album;

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

return [
    'router' => [
        'routes' => [
            'album' => [
                'type' => Segment::class,'options' => [
                    'route' => '/album[/:action[/:id]]','constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*','id' => '[0-9]+',],'defaults' => [
                        'controller' => Controller\AlbumController::class,'action' => 'index','controllers' => [
        'factories' => [
            Controller\AlbumController::class => InvokableFactory::class,'view_manager' => [
        'template_map' => [
            'album/index' => __DIR__ . '/../view/album/album/index.phtml','template_path_stack' => [
            'album' => __DIR__ . '/../view',];

module \ Album \ src \ Controller \ AlbumController.PHP

<?PHP

declare(strict_types=1);

namespace Album;

class Module
{
    public function getConfig() : array
    {
        return include __DIR__ . '/../config/module.config.PHP';
    }
}

module \ Album \ view \ album \ album \ index.phtml

<?PHP

declare(strict_types=1);

namespace Album\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\viewmodel;

class AlbumController extends AbstractActionController
{
    public function indexAction()
    {
        return new viewmodel();
    }
}

解决方法

当您指定根为

'route' => '/album[/:action[/:id]]',

URL 应该看起来像 http://laminas.com/album,它将路由到 indexAction,因为您已将该操作定义为默认值,但 http://laminas.com/album/index 将具有相同的效果。