php - Laminas / Zend framework 3 表单注解
问题描述
我正在研究 Laminas 或 Zend 框架 3,我尝试使用注释来生成表单
但是得到这个消息
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 的单独包中。根据您的作曲家需要语句,默认情况下不会加载它。
你有任何输出吗composer why laminas/laminas-code
推荐阅读
- java - 短信通知默认声音的uri
- react-native - Android:react-native-image-picker manifest.xml 文件路径无效
- file - 如何为应用程序工作会话设置一次文件中的常量
- regex - 通过正则表达式识别句子中的单词
- c++ - 三元运算符为什么以及何时返回左值?
- python - 使用python计算肽单同位素
- c# - 尝试不每秒创建新文件,但如果文件限制超过则使用 log4net 创建新文件
- windows - 使用不带方括号的 dir /D 命令作为输出
- haskell - Haskell 中使用 List Monad 的下推自动机
- java - 如何分配多维输出缓冲区来提供 Android Tflite 的interpreter.run()?