如何在 php 7 下扩展 femanager 控制器

How to extend femanager controller under php 7

自从使用 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);
     }
}

Wolfgang Klinger 制定的一个可能的解决方案是 XClass \TYPO3\CMS\Extbase\Mvc\Controller\Argument class。

这个class有一个受保护的属性"dataType",通常没有setter。

使用 TYPO3 的 XClass 机制,可以添加一个 setDataType 方法来启用手动覆盖此 属性。

有了这个,现在可以在扩展 edit/invitation/new-controller.

的(神奇)初始化操作中覆盖通常自动检测到的数据类型

重要的是,不是更改"normal"操作(newAction、createAction ...)的类型提示和注释,而是添加类似这样的内容对应的初始化动作:

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);
    }
}

完整 answer/tutorial ...

您可以使用以下 tutorial 扩展 TYPO3 femanager。旧的扩展 femanagerextended 不是必需的。这适用于 TYPO3PHP 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 也有帮助。