问题描述
我应该如何将自定义样式应用到供应商模块的表单或将其嵌入到我自己的视图脚本中?更具体地说,我想使用 EdpUser 模块 (https://github.com/EvanDotPro/EdpUser) 提供的表单.
How should I apply custom styles to Vendor Module's Form or embed it on my own View script?More specifically I want to work with forms provided by EdpUser Module (https://github.com/EvanDotPro/EdpUser).
最简单的方法似乎是直接修改供应商的脚本.但显然这会与供应商未来的更新发生冲突.
The easiest way seems to be modifying the vendor's script directly. But obviously this will conflict with vendor's future updates.
另一种方法似乎是将供应商的控制器复制到我自己的模块中,然后提供我自己的视图.然而,这会遇到与供应商未来更新不同步的类似问题.我将不得不小心命名空间.
Another way seems to be copying Vendor's Controller into my own Module then provide my own views. However this suffers a similar issue of getting out of sync with Vendor's future updates. And I will have to be careful with namespace.
也许我应该扩展供应商的控制器而不是复制它.这应该适用于命名空间,我应该能够很容易地访问我的控制器中的表单.使用我自己的视图脚本时.这是正确的方法还是有更好的方法?
Maybe I should extend Vendor's Controller instead of copying it. This should work well with namespaces and I should be able to access the Forms within my Controller quite easily. While using my own view scripts. Is this the right way or is there a better one?
谢谢
推荐答案
不推荐修改模块目录中的任何内容,尤其是那些放在/vendor/代码>.这也是必须将配置模板复制到您自己的
/config/autoload/
目录的原因.
It is not the recommended approach to modify anything in the module's directory, especially those modules put in /vendor/
. This is also the reason configuration templates must be copied to your own /config/autoload/
directory.
模块必须提供足够的扩展点以使其足够灵活以供您使用.这些可能性包括以下选项:
A module must provide sufficient extension points to make it flexible enough for your usage. Among those possibilities are the following options:
ZfcUser 模块在/user url 下注册自己.例如,如果您想将其更改为/account,您只需将其添加到您的配置中即可:
The ZfcUser module registers itself under the /user url. If you want to change that into /account for example, you can simply add this to your configuration:
<?php
return array(
'di' => array(
'instance' => array(
'Zend\Mvc\Router\RouteStack' => array(
'parameters' => array(
'routes' => array(
'zfcuser' => array(
'options' => array(
'route' => '/account',
),
),
),
),
),
),
),
);
更换控制器
如果您对使用 ZfcUser\Controller\UserController
不满意,并且您想覆盖此控制器中的某些操作,您可以创建一个自定义控制器,例如 MyUser\Controller\用户控制器
.如果您扩展 ZfcUser\Controller\UserController
并提供此配置,您就可以开始了:
Change a controller
If you are not happy the ZfcUser\Controller\UserController
is used and you want to override some action in this controller, you can create a custom controller, for example MyUser\Controller\UserController
. If you extend the ZfcUser\Controller\UserController
and provide this configuration, you are ready to go:
<?php
return array(
'di' => array(
'instance' => array(
'Zend\Mvc\Router\RouteStack' => array(
'parameters' => array(
'routes' => array(
'zfcuser' => array(
'options' => array(
'defaults' => array(
'controller' => 'MyUser\Controller\UserController'
),
),
),
),
),
),
),
),
);
您还可以使用 DI 别名覆盖 DI 配置中的 zfcuser
别名:
You can also use DI aliasing to override the zfcuser
alias in your DI configuration:
<?php
return array(
'di' => array(
'instance' => array(
'alias' => array(
'zfcuser' => 'MyUser\Controller\UserController'
),
),
),
);
修改表单实例
例如,ZfcUser 模块触发多个事件以帮助其他模块连接到表单创建过程.ZfcUser\Form\Login
和 ZfcUser\Form\Register
在设置所有表单元素后都会触发 init
事件.这使您有机会添加或删除元素.
Modify the form instance
For example the ZfcUser module triggers several events to help other modules hook into the form creation process. Both the ZfcUser\Form\Login
as ZfcUser\Form\Register
trigger an init
event after they set up all form elements. This gives you the chance to add or remove elements.
use Zend\EventManager\StaticEventManager;
$events = StaticEventManager::getInstance();
$events->attach('ZfcUser\Form\Login', 'init', function ($e) {
$form = $e->getTarget();
$form->addElement('text', 'something-new');
});
我需要在这里说两句:
- 每个模块都必须提供自己的触发器.这个 ZfcUser 在两种形式上都有
init
,但这不是每个模块都给定的.您必须查看文档或源代码才能找到相关信息. Zend\EventManager
目前正在重构中,以通过SharedEventManager
实例替换单例StaticEventManager
,该实例可以通过实例化>Zend\Di
定位器.您可以在此拉取请求中查看此次重构的进度.
- Every module must provide its own triggers. This ZfcUser has the
init
on both forms, but this is not a given for every module. You must look into the documentation or source code to find out about this. - The
Zend\EventManager
is currently under a refactoring to replace the singletonStaticEventManager
by aSharedEventManager
instance which can be instantiated through theZend\Di
locator. You can watch the progress of this refactoring in this Pull Request.
更改视图或表单呈现
根据 这个 RFC Zend\Form
组件可能会更改.特别是对于渲染,装饰器将被移除,而只是"普通视图助手将改为渲染表单.有了这个,再加上覆盖视图的可能性,只需使用另一个模块和一些视图脚本就可以很容易地改变动作的呈现.
Change a view or form rendering
As per this RFC the Zend\Form
component is likely to change. Especially for the rendering, the decorators will be removed and "just" normal view helpers will render the form instead. With this given, plus the possibility to override views, it is very easy to change the rendering of an action just by using another module with only some view scripts.
如果您创建自己的模块并提供此配置,则会向模板路径堆栈添加一个新位置:
If you create your own module and provide this configuration, you add a new location to the template path stack:
<?php
return array(
'di' => array(
'instance' => array(
'Zend\View\Resolver\TemplatePathStack' => array(
'parameters' => array(
'paths' => array(
'myuser' => __DIR__ . '/../view',
),
),
),
),
),
);
现在您可以在模块的 view
目录中创建视图脚本.如果您的模块名为MyUser"并位于 /modules/MyUser
下,并且您想覆盖来自 ZfcUser 的登录视图脚本(位于 /vendor/ZfcUser/view/zfcuser/login.phtml
,在 /modules/MyUser/view/zfcuser/login.phtml
中创建您的视图脚本.
Now you can create view scripts in your module's view
directory. If you have your module called "MyUser" and located under /modules/MyUser
and you want to override the login view script from ZfcUser (located in /vendor/ZfcUser/view/zfcuser/login.phtml
, create your view script in /modules/MyUser/view/zfcuser/login.phtml
.
这篇关于ZF2,使用供应商模块的表单类的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!