Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put model helper functions in Symfony2

Tags:

php

symfony

I have the following function:

function array_duplicates($array)
{
    $duplicates = array();
    $unique = array_unique($array);
    for ($i = 0; $i < count($array); $i++) {
        if (!array_key_exists($i, $unique)) {
            $duplicates[] = $array[$i];
        }   
    }   
    return $duplicates;
}

This function obviously doesn't apply to any certain model, and it's not a template helper. Where would be the appropriate place to put this function? (Please don't say "anywhere you want.")

like image 456
Jason Swett Avatar asked Oct 04 '12 17:10

Jason Swett


People also ask

Where do I put helper functions in PHP?

php, you would do this: $this->load->helper('url'); A helper can be loaded anywhere within your controller methods (or even within your View files, although that's not a good practice), as long as you load it before you use it.

How do you write a helper function in JavaScript?

To implement the helper, we write a JavaScript function that takes its arguments as an array. This is because helpers can also receive named arguments, which we'll discuss next. import { helper } from '@ember/component/helper'; function substring(args) { let [string, start, end] = args; return string.


2 Answers

This might be the type of thing you'd put into a service. Create a class like this:

class ArrayUtils
{
    function array_duplicates($array)
    {
        ... 
        return $duplicates;
    }
}

And then define it as a service. If you're using YAML, you'd put something like this into your config.yml file:

services:
    arrayUtils:
        class:        Full\Path\To\ArrayUtils

Under this configuration, Symfony will create a single instance of your ArrayUtils, and give all your controllers access to it. Then you can call it like this:

class SomeController
{
    public function someAction()
    {
        ...
        $duplicates = $this->get("arrayUtils")->array_duplicates($array);
        ...
    }
}

This is all part of Symfony's dependency injection framework. It's pretty cool, and I recommend reading up on it here: http://symfony.com/doc/2.0/book/service_container.html

Alternative Option

Now, that might be a little overkill for such a small chunk of code. If you're only going to be using this in a single bundle, then you might want to just put it into a base controller, and have all your other controllers extend that base controller.

class BaseController
{
    function array_duplicates($array)
    {
        ...
    }
}

class SomeController extends BaseController
{
    function someAction()
    {
        $this->array_duplicates($array);
    }
}
like image 130
Thomas Kelley Avatar answered Oct 02 '22 01:10

Thomas Kelley


By convention, utility classes go under the Util namespace.

If you use bundles, a class would go into the YourBundle\Util namespace. If you don't, it would go into the Acme\Util namespace — the src/Acme/Util folder.

like image 35
Elnur Abdurrakhimov Avatar answered Oct 02 '22 02:10

Elnur Abdurrakhimov