How to add an Action to Account Controller in Shopware

蓝咒 提交于 2021-02-19 07:38:46

问题


How to add a custom action to an existing Controller in Shopware?

Examples (url structure):

/account/bonus
/account/custom
/account/...

Usually it's easier and cleaner to create a new controller for that purpose, but in some cases it's necessary.


回答1:


There is a cleaner way than replacing the whole Controller. It is also not recommended to replace a whole controller due to the lack of update compatibility. In the worst case something like that could kill the whole website. A while ago I created a thread in the shopware forum (german) discussing the exact same issue. I wanted to extend an existing finishAction() in the checkout Controller.

public function onPostDispatchCheckout(\Enlight_Controller_ActionEventArgs $args)
{
/** @var \Enlight_Controller_Action $controller */
$controller = $args->getSubject();

/** @var \Enlight_Controller_Request_Request $request */
$request = $controller->Request();

if ($request->getActionName() !== 'finish') {
    return;
}

// do your stuff here
}

So even though it is not the exact same issue you have, the procedure is quite the same.

First off you subscribe to the controller (in my case the PostDispatchCheckout Controller) afterwards you edit the controller in your Bootstrap.php

To make sure, that it just alters a specific action you have to use the if-construction so your code gets just triggered on the wished action [in my case the finishAction()].

I hope this helps. What wonders me though is why you have to add a new action to an already existing controller. I can think of no situation where something like that is more practicable than creating a complete new custom controller.

Kind regards,

Max




回答2:


Spoiler: Replace the controller

There is no cleaner way than to replace the whole controller and extend it's functionality, so it's nearly as clean as Shopware's hooks.

Guide

Add a new Subscriber to your Plugin

class AccountSubscriber implements SubscriberInterface
{
    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return array(
            'Enlight_Controller_Dispatcher_ControllerPath_Frontend_Account' => 'getAccountController'
        );
    }

    /**
     * @return string
     */
    public function getAccountController()
    {
        return $this->getPath() . '/Controllers/Frontend/AccountExtended.php';
    }

    /**
     * @return string
     */
    public function getPath()
    {
        $plugin = Shopware()->Container()->get('kernel')->getPlugins()['AcmeYourPlugin'];

        return $plugin->getPath();

    }
}

Downsides

Unfortunately some controller have private methods which impact the logic. Like the Account Controller. So it's not always possible to simply extend the controller.

In the end, try to add a new controller with a new route. It's easier, and cleaner.




回答3:


You should not replace the "account" controller. You can define you own action for existing controller with following:

public static function getSubscribedEvents()
    {
        return [
            'Enlight_Controller_Action_Frontend_Account_MyBonus'        => 'onAccountMyBonus',        
      ];
    }

and then

public function onAccountMyBonus(\Enlight_Event_EventArgs $args)
    {
        $args->setProcessed(true);
        .....
         your code here
}


来源:https://stackoverflow.com/questions/46340089/how-to-add-an-action-to-account-controller-in-shopware

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!