由于使用PHP 7.0及更高版本,php的严格模式会生成如下警告:
PHP Warning: Declaration of In2code\Femanagerextended\Controller\EditController::updateAction(In2code\Femanagerextended\Domain\Model\User $user) should be compatible with In2code\Femanager\Controller\EditController::updateAction(In2code\Femanager\Domain\Model\User $user) in ($PATH)\typo3conf\ext\femanagerextended\Classes\Controller\EditController.php line 17
尝试使用本手册最佳实践部分中描述的方式扩展TYPO3 Extension femanager的现有控制器时:
<?php
namespace In2code\Femanagerextended\Controller;
use In2code\Femanager\Controller\EditController as EditControllerFemanager;
use In2code\Femanagerextended\Domain\Model\User;
/**
* Class EditController
*
* @package In2code\Femanagerextended\Controller
*/
class EditController extends EditControllerFemanager
{
/**
* action update
*
* @param User $user
* @validate $user In2code\Femanager\Domain\Validator\ServersideValidator
* @validate $user In2code\Femanager\Domain\Validator\PasswordValidator
* @return void
*/
public function updateAction(User $user)
{
parent::updateAction($user);
}
}
答案 0 :(得分:6)
沃尔夫冈·克林格(Wolfgang Klinger)设计的一个可能的解决方案是XClass \ TYPO3 \ CMS \ Extbase \ Mvc \ Controller \ Argument class。
此类具有受保护的属性“dataType”,通常没有setter。
使用TYPO3的XClass机制,可以添加一个setDataType方法来启用手动覆盖此属性。
有了这个,现在可以在扩展编辑/邀请/新控制器的(魔术)初始化操作中覆盖通常自动检测到的数据类型。
重要的是,不更改“普通”动作(newAction,createAction ...)的类型提示和注释,但是将相应的内容添加到相应的初始化动作中:< / p>
public function initializeNewAction()
{
if ($this->arguments->hasArgument('user')) {
// Workaround to avoid php7 warnings of wrong type hint.
/** @var \Mediagear\Jdcompetition\Xclass\Extbase\Mvc\Controller\Argument $user */
$user = $this->arguments['user'];
$user->setDataType(\Vendor\Extension\Domain\Model\User::class);
}
}
答案 1 :(得分:2)
完整答案/教程...
您可以使用以下tutorial扩展TYPO3 femanager
。不需要旧的扩展名femanagerextended
。 TYPO3
和PHP 5.x
可以很好地工作。这是PHP 7.x
的解决方法:
添加一个新的XCLASS文件f.e. typo3conf/ext/yourext/Classes/Xclass/Extbase/Mvc/Controller/Argument.php
:
namespace Yournamespace\Yourext\Xclass\Extbase\Mvc\Controller;
class Argument extends \TYPO3\CMS\Extbase\Mvc\Controller\Argument
{
/**
* Set data type for femanager workaround.
* Workaround to avoid php7 warnings of wrong type hint.
*
* @param $dataType
* @return $this
*/
public function setDataType($dataType) {
$this->dataType = $dataType;
return $this;
}
}
更改文件typo3conf/ext/yourext/Classes/Controller/EditController.php
:
namespace Yournamespace\Yourext\Controller;
use \Yournamespace\Yourext\Domain\Model\User;
class EditController extends \In2code\Femanager\Controller\EditController {
/**
* @return void
*/
public function initializeUpdateAction()
{
if ($this->arguments->hasArgument('user')) {
// Workaround to avoid php7 warnings of wrong type hint.
/** @var \Yournamespace\Yourext\Xclass\Extbase\Mvc\Controller\Argument $user */
$user = $this->arguments['user'];
$user->setDataType(\Yournamespace\yourext\Domain\Model\User::class);
}
}
/**
* action update
*
* @param User $user
* @validate $user In2code\Femanager\Domain\Validator\ServersideValidator
* @validate $user In2code\Femanager\Domain\Validator\PasswordValidator
* @validate $user In2code\Femanager\Domain\Validator\CaptchaValidator
* @return void
*/
public function updateAction(\In2code\Femanager\Domain\Model\User $user) {
parent::updateAction($user);
}
}
更改文件typo3conf/ext/yourext/Classes/Controller/NewController.php
:
namespace Yournamespace\Yourext\Controller;
use Yournamespace\Yourext\Domain\Model\User;
class NewController extends \In2code\Femanager\Controller\NewController {
/**
* Initialize create action for setting the right custom data type for the user.
*/
public function initializeCreateAction() {
if ($this->arguments->hasArgument('user')) {
// Workaround to avoid php7 warnings of wrong type hint.
/** @var \Yournamespace\Yourext\Xclass\Extbase\Mvc\Controller\Argument $user */
$user = $this->arguments['user'];
$user->setDataType(\Yournamespace\Yourext\Domain\Model\User::class);
}
}
/**
* action create
*
* @param User $user
* @validate $user In2code\Femanager\Domain\Validator\ServersideValidator
* @validate $user In2code\Femanager\Domain\Validator\PasswordValidator
* @return void
*/
public function createAction(User $user)
{
parent::createAction($user);
}
}
将您的XCLASS注册到ext_localconf.php
中:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument'] = array('className' => 'Yournamespace\\Yourext\\Xclass\\Extbase\\Mvc\\Controller\\Argument');
通过安装工具清除缓存并清除自动加载器!
此issue也有帮助。