0

私のクラスの 1 つは現在、FOSUserBundle で BaseController を拡張し、親アクションを返します。ただし、プロジェクトの仕様により、親クラスを編集する必要はありません。小枝がレンダリングするために、子応答を介して追加の変数を送信する方法はありますか?

子クラス:

class ChangePasswordController extends BaseController
{
    public function changePasswordAction(Request $request)
    {
        $response = parent::changePasswordAction($request);

        return $response; // and 'myVariable' => $myVariable
    }
}

親クラス:

class ChangePasswordController extends ContainerAware
{
    /**
     * Change user password
     */
    public function changePasswordAction(Request $request)
    {
        //lots of code.....

        return $this->container->get('templating')
                    ->renderResponse(
                    'FOSUserBundle:ChangePassword:changePassword.html.'
                        .$this->container->getParameter('fos_user.template.engine'),
                         array(
                         'form' => $form->createView()

                          //and 'myVariable' => $myVariable     

                          )
               );

    }
}

要約すると、親クラスを変更せずに、親クラスに何かを渡す方法はありますか...小枝ビューを追加の変数でレンダリングします。

- アップデート -

基本的に、FOSUserBundle changePassword アクションを使用してフォームをレンダリングしたいので、これはうまくいきます:

return $this->container
            ->get('templating')
            ->renderResponse(
            'FOSUserBundle:ChangePassword:changePassword.html.'.$this->container->getParameter('fos_user.template.engine'),
            array('form' => $form->createView())
        );

ただし、 FosUserBundle ChangePassword Controllerを変更せずに、上記のように「フォーム」が渡されるように、ビューにさらに変数を渡したいと思います。したがって、そのコントローラーを継承し、いくつかの追加機能を追加し、親のパスワード変更アクションを返すクラスがあります。

class ChangePassController extends ChangePasswordController
{
    public function changePasswordAction(Request $request)
    {
        // more code......

        $response = parent::changePasswordAction($request);
        return $response;
    }
}

しかし、ほとんどのアプリケーションと同様に、ビュー テンプレートに追加したいのはフォーム変数だけではありません。親コントローラー/アクションを変更せずに、追加の変数をビューに渡す方法はありますか? 'myVariable' => $myVariable を親の changePasswordAction return ステートメントにプッシュするのは好き (好きではない) ですか?

4

3 に答える 3

0

ボトムアップで考えてください。

Twig 拡張機能http://symfony.com/doc/current/cookbook/templating/twig_extension.htmlを使用して、アクションを介さずにデータにアクセスできます。

    twig.extension.user_profile:
    class: 'MyBundle\UserProfileExtension'
    arguments:
        - '@doctrine.orm.entity_manager'
    tags:
        - { name: twig.extension }

拡張クラス

class UserProfileExtension extends \Twig_Extension
{
/**
 * @var EntityManager
 */
private $entityManager;

/**
 * @param UserProfileDataService $userProfileDataService
 */
public function __construct(EntityManager $entityManager)
{
    $this->entityManager = $entityManager;
}

/**
 * @return array
 */
public function getFunctions()
{
    return array(
        new \Twig_SimpleFunction('get_my_custom_var', array($this, 'getMyCustomVar')),
    );
}

/**
 * @return array
 */
public function getMyCustomVar()
{
    $var = $this->entityManager->getRepository('MyCustomRepository')->findOneBy(['id' => 1]);

    return $var;
}

/**
 * Returns the name of the extension.
 *
 * @return string The extension name
 */
public function getName()
{
    return 'user_profile_extension';
}

テンプレートの使用

{dump(get_my_custom_var())}
于 2016-06-24T13:35:19.557 に答える