Business Logic Controller for Validating Address in Commerce I am putting together my Business Logic plugin, as I want to validate the address fields throughout the checkout process of my Cart. I understand in order to do this I need to create a plugin and us the Craft Commerce commerce_addresses.onBeforeSaveAddress event.

I decided to try and use the https://github.com/lindseydiloreto/craft-businesslogic as a starting place.

My code is as follows;

public function init()
{
    craft()-&gt;on('commerce_addresses.onBeforeSaveAddress', function(Event $event){
        $address = $event-&gt;params['address'];

        if (empty($address-&gt;firstName))
        {
            $address-&gt;addError('firstName', Craft::t('Please enter your Name'));
             $event-&gt;performAction = false;
        }

        if (empty($address-&gt;address1))
        {
            $address-&gt;addError('address1', Craft::t('Please enter an Address'));
            $event-&gt;performAction = false;
        }

        if (empty($address-&gt;city))
        {
            $address-&gt;addError('city', Craft::t('Please enter your Town/City'));
            $event-&gt;performAction = false;
        }

        if (empty($address-&gt;zipCode))
        {
            $address-&gt;addError('zipCode', Craft::t('Please enter your Post Code'));
            $event-&gt;performAction = false;
        }

        if (empty($address-&gt;countryId))
        {
            $address-&gt;addError('countryId', Craft::t('Please select your Country'));
            $event-&gt;performAction = false;
        }
    });
}


If I have this code inside my to level plugin file named BusinessLogicPlugin.php it seems to work and it validates some of the fields as specified. 

However, if I move this code into the controllers folder inside /controllers/BusinessLogicController.php it doesn't.

I am unsure what I need to do to move this out of the main/top level file, as I see this file getting very logon/messy if I need to add several 'business logics' to the site.

Can anyone shed light on this as to why it might not work within /controllers/

Thanks.
You can move it all into a Service and then call it from your main plugin file if you find it is getting unwieldy.

file: craft/plugins/businesslogic/BusinessLogicPlugin.php:
    

class BusinessLogicPlugin extends BasePlugin
{
    public function init()
    {
        craft()-&gt;on('commerce_addresses.onBeforeSaveAddress', function(Event $event)
        {
            craft()-&gt;businessLogic_events-&gt;onBeforeSaveAddress($event);
        });
    }
}


file: craft/plugins/businesslogic/services/BusinessLogic_EventsService.php:
    

class BusinessLogic_EventsService extends BaseApplicationComponent
{
    public function onBeforeSaveAddress(Event $event)
    {
        $address = $event-&gt;params['address'];

        if (empty($address-&gt;firstName))
        {
            $address-&gt;addError('firstName', Craft::t('Please enter your Name'));
            $event-&gt;performAction = false;

            // ...
        }
    }
}


Because $event is an object it will be passed by reference, and you don't need to return anything.