Laminas / Zend framework 3 表单注解

Laminas / Zend framework 3 form annotations

我正在研究 Laminas 或 Zend 框架 3,我尝试使用注释来生成表单

Using Annotations

但是收到这条消息

Message:
Class 'Laminas\Code\Annotation\AnnotationManager' not found

我的控制器:

<?php
declare(strict_types=1);

namespace Application\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Laminas\Form\Annotation\AnnotationBuilder;
 
use Application\Entity\User;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
      

         $builder = new AnnotationBuilder();
         $form    = $builder->createForm(User::class);
         
        return new ViewModel();
    }
}

我的实体:

 <?php 
namespace Application\Entity;
use Laminas\Form\Annotation;


/**
 * @Annotation\Name("user")
 * @Annotation\Hydrator("Laminas\Hydrator\ObjectProperty")
 */
class User
{
    /**
     * @Annotation\Exclude()
     */
    public $id;

    /**
     * @Annotation\Filter({"name":"StringTrim"})
     * @Annotation\Validator({"name":"StringLength", "options":{"min":1, "max":25}})
     * @Annotation\Validator({"name":"Regex","options":{"pattern":"/^[a-zA-Z][a-zA-Z0-9_-]{0,24}$/"}})
     * @Annotation\Attributes({"type":"text"})
     * @Annotation\Options({"label":"Username:"})
     */
    public $username;

    /**
     * @Annotation\Type("Laminas\Form\Element\Email")
     * @Annotation\Options({"label":"Your email address:"})
     */
    public $email;
}

这是我得到的错误:

An error occurred
An error occurred during execution; please try again later.
Additional information:
Error
File:
C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-form\src\Annotation\AnnotationBuilder.php:178
Message:
Class 'Laminas\Code\Annotation\AnnotationManager' not found
Stack trace:
#0 C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-form\src\Annotation\AnnotationBuilder.php(221): Laminas\Form\Annotation\AnnotationBuilder->getAnnotationManager()
#1 C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-form\src\Annotation\AnnotationBuilder.php(257): Laminas\Form\Annotation\AnnotationBuilder->getFormSpecification('Application\Ent...')
#2 C:\dev\htdocs\laminas-mvc-skeleton\module\Application\src\Controller\IndexController.php(21): Laminas\Form\Annotation\AnnotationBuilder->createForm('Application\Ent...')
#3 C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-mvc\src\Controller\AbstractActionController.php(77): Application\Controller\IndexController->indexAction()
#4 C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-eventmanager\src\EventManager.php(331): Laminas\Mvc\Controller\AbstractActionController->onDispatch(Object(Laminas\Mvc\MvcEvent))
#5 C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-eventmanager\src\EventManager.php(188): Laminas\EventManager\EventManager->triggerListeners(Object(Laminas\Mvc\MvcEvent), Object(Closure))
#6 C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-mvc\src\Controller\AbstractController.php(103): Laminas\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Laminas\Mvc\MvcEvent))
#7 C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-mvc\src\DispatchListener.php(139): Laminas\Mvc\Controller\AbstractController->dispatch(Object(Laminas\Http\PhpEnvironment\Request), Object(Laminas\Http\PhpEnvironment\Response))
#8 C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-eventmanager\src\EventManager.php(331): Laminas\Mvc\DispatchListener->onDispatch(Object(Laminas\Mvc\MvcEvent))
#9 C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-eventmanager\src\EventManager.php(188): Laminas\EventManager\EventManager->triggerListeners(Object(Laminas\Mvc\MvcEvent), Object(Closure))
#10 C:\dev\htdocs\laminas-mvc-skeleton\vendor\laminas\laminas-mvc\src\Application.php(331): Laminas\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Laminas\Mvc\MvcEvent))
#11 C:\dev\htdocs\laminas-mvc-skeleton\public\index.php(42): Laminas\Mvc\Application->run()
#12 {main}

感谢您的支持。

您是否检查过供应商中是否存在 AnnotationManager? 应该在vendor/laminas/laminas-code/src/Annotation/AnnotationManager.php

如您所见,它位于一个名为 laminas/laminas-code 的单独包中。 根据您的作曲家 require 语句,默认情况下不会加载它。

你有composer why laminas/laminas-code

的输出吗