Where to put model helper functions in Symfony2

Asked
Active3 hr before
Viewed126 times

5 Answers

90%

Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português, Stack Overflow Public questions & 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;
   }
}
load more v
88%

Provide the following:,Since we've added the #Service type hint to the $service parameter of our getMockService function, we now get completion and navigation when calling it.,To achieve this, in the Settings/Preferences dialog Ctrl+Alt+S, navigate to PHP | Symfony | Type Provider. Then, select the Enable Custom Signature Types checkbox and click to add information about the getMockService function.,Consider the following function, which doesn't use any Symfony-specific functions:

The Symfony Plugin provides parameter types and return types inference based on their usage with Symfony classes and functions. This lets you create your own helper functions that take or return a template, a service, an entity, a translation, a route, a form, or an event.

As an example, let's create a redirectToRoute function that can be used in controllers. This function will combine the Controller class' redirect and generateUrl function, so that you can easily redirect users to a known route within our application. The code will look as follows:

redirectToRoute

The Symfony Plugin provides parameter types and return types inference based on their usage with Symfony classes and functions. This lets you create your own helper functions that take or return a template, a service, an entity, a translation, a route, a form, or an event.

As an example, let's create a redirectToRoute function that can be used in controllers. This function will combine the Controller class' redirect and generateUrl function, so that you can easily redirect users to a known route within our application. The code will look as follows:

Controller

The Symfony Plugin provides parameter types and return types inference based on their usage with Symfony classes and functions. This lets you create your own helper functions that take or return a template, a service, an entity, a translation, a route, a form, or an event.

As an example, let's create a redirectToRoute function that can be used in controllers. This function will combine the Controller class' redirect and generateUrl function, so that you can easily redirect users to a known route within our application. The code will look as follows:

redirect

The Symfony Plugin provides parameter types and return types inference based on their usage with Symfony classes and functions. This lets you create your own helper functions that take or return a template, a service, an entity, a translation, a route, a form, or an event.

As an example, let's create a redirectToRoute function that can be used in controllers. This function will combine the Controller class' redirect and generateUrl function, so that you can easily redirect users to a known route within our application. The code will look as follows:

generateUrl
load more v
72%

# How to register utilities class on Laravel 5.8
# File: composer.json
# ref: https: //stackoverflow.com/questions/28290332/best-practices-for-custom-helpers-in-laravel-5
   "autoload": {
      "classmap": [
         ...
      ],
      "psr-4": {
         "App\\": "app/"
      },
      "files": [
         "app/helpers.php" // <---- ADD THIS
      ]
   },

   # Then run: `composer dump-autoload`
65%

In Symfony 1.4 it's normal to reuse components and partials to include HTML blocks that require some extra logic. Other solution that many developers apply is the creation of classes with static methods, that work in the same way so to speak, however they have a prefix of a predefined class and they're always available. One feature that not every developer that support a legacy Symfony 1.4 project is the existence of Helper functions.,By default, Symfony 1.4 has a lot of helper functions available (almost) on the entire project e.g url_for, link_to etc. If you like this style, you need to know that you can create the same kind of helpers without writing the same function everywhere.,As you can see, with helpers you are writing plain PHP methods so to speak and they can be used everywhere in your project. To learn more about the use_helper method of Symfony 1.4, please read the official legacy documentation.,Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

To expose helpers globally on your project, you need to create the helper directory inside of yourproject/lib. Inside this folder you can create many helper files as you want that contains as many helper method as you want, the only condition is that the name of the file ends with *Helper, for example if you want to create helper functions that are related to manipulation of strings, we would create a file named StringHelpers.php inside the lib directory. There, instead of a class you should create plain PHP functions. For example, we'll include a method that highlights a given word inside a string HTML using spans:

<?php 
// application/lib/StringHelper.php

function highlight_word_inside_html($content, $search){
    if(is_array($search)){
        foreach ( $search as $word )
        {
            $content = str_ireplace($word, '<span class="highlight_word">'.$word.'</span>', $content);
        }
    } else {
        $content = str_ireplace($search, '<span class="highlight_word">'.$search.'</span>', $content);        
    }
    
    return $content;
}
load more v
75%

The array_dot function flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth:,The array_flatten function will flatten a multi-dimensional array into a single level.,The array_set function sets a value within a deeply nested array using "dot" notation:,The array_has function checks that a given item exists in an array using "dot" notation:

The array_add function adds a given key / value pair to the array if the given key doesn't already exist in the array:

$array = array_add(['name' => 'Desk'], 'price', 100);

// ['name' => 'Desk', 'price' => 100]
load more v

Other "undefined-undefined" queries related to "Where to put model helper functions in Symfony2"