How to access a global service function within a twig extension using Symfony?

MRE

I'd like to have a global function which is available in all controllers as well as in a twig extension. The function currently looks like this:

namespace Symfony\Bundle\FrameworkBundle\Controller;

class BaseController extends Controller {

/**
 * Check if the user is assigned to one of the given airlines
 *
 * @param Array $airlines
 *
 * @return boolean
 */
public function HAS_AIRLINE(Array $airlines) {
    if ($airlines == null)
        return false;

    // Get current user
    $user = $this->get('security.context')->getToken()->getUser();

    foreach ($airlines as $airline) {
        if($user->hasAirline($airline))
        return true;
    }

    return false;
}

I implemented this function into the BaseController.php, so I can use it in all other controllers. This works perfectly fine so far. Now I want to use this function in a twig extension as well, but I don't know how. I do have the following function in my twig extension:

/* 
 * Check if the current user has one of the given roles by using the function provided by the base controller
 */
public function hasAirline($airlines= Array())
{
    if($airlines == null)
        return false;

    // How can I now use the function from my BaseController?
    return HAS_AIRLINE($airlines);
}

I already tried to define the BaseController as a service and use the service in the twig extension, but I couldn't manage that.

Could someone please help me? I feel like there is something I did not understand about Symfony yet, but the docs do not really help me.

Thank you, MRE

A.L

You can move the logic in your User entity:

class User
{
    public function hasAirline($airlines)
    {
        foreach ($airlines as $airline)
        {
            if ($this->getAirlines()->contains($airline))
            {
                return true;
            }
        }

        return false;
    }
}

This will work if getAirlines() return an ArrayCollection from Doctrine2, this allow the use of the contains() method.

Then you can use this in the controller:

public function HAS_AIRLINE(Array $airlines) {
    return $this->get('security.context')->getToken()->getUser()
        ->hasAirline($airlines);
}

Or in Twig with app.user:

{{ set hasAirline = app.user.hasAirline(airlines) }}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set global variables in Twig using symfony2

From Dev

How to call Twig extension filter or function from PHP (controller / service / other twig extension etc )?

From Dev

Search function using twig in symfony

From Dev

Search function using twig in symfony

From Dev

How to use global variable in symfony twig file?

From Dev

How can I access to parameters.yml content within a custom twig extension

From Dev

How to expose a Twig Extension as service of a bundle?

From Dev

Symfony Twig Extension breaks other service - Is templating done before security?

From Dev

Register a custom twig extension with injected service in Symfony 3 causes an error

From Dev

Symfony Twig global variables

From Dev

Symfony2 - Twig Extension error cannot access empty property

From Dev

Register Twig Extension in Symfony

From Dev

How to use Twig global parameter inside Twig function

From Dev

How to render content within a custom twig function?

From Dev

Symfony - Using twig extension with form prevents errors from showing up

From Dev

How can I render a controller action within a twig extension?

From Dev

How can I render a controller action within a twig extension?

From Dev

Symfony2: How to replace a certain string with a global Twig variable?

From Dev

Symfony2: how to make a twig extension return an image

From Dev

Symfony2: how to make a twig extension return an image

From Dev

Symfony2: How to access a service inside an extension's load() method?

From Dev

How to enable autoescaping in Twig using Symfony

From Dev

Symfony dependency injection in twig extension

From Dev

Activate StringLoader Twig Extension in Symfony

From Dev

Symfony Twig extension render view

From Dev

Access a variable within a sub function within a service function

From Dev

How to access a global variable within a local scope?

From Dev

How to access XMLHttpRequest within a Paw extension?

From Dev

How to access the asp control using $(this) within the setTimeout() function

Related Related

  1. 1

    How to set global variables in Twig using symfony2

  2. 2

    How to call Twig extension filter or function from PHP (controller / service / other twig extension etc )?

  3. 3

    Search function using twig in symfony

  4. 4

    Search function using twig in symfony

  5. 5

    How to use global variable in symfony twig file?

  6. 6

    How can I access to parameters.yml content within a custom twig extension

  7. 7

    How to expose a Twig Extension as service of a bundle?

  8. 8

    Symfony Twig Extension breaks other service - Is templating done before security?

  9. 9

    Register a custom twig extension with injected service in Symfony 3 causes an error

  10. 10

    Symfony Twig global variables

  11. 11

    Symfony2 - Twig Extension error cannot access empty property

  12. 12

    Register Twig Extension in Symfony

  13. 13

    How to use Twig global parameter inside Twig function

  14. 14

    How to render content within a custom twig function?

  15. 15

    Symfony - Using twig extension with form prevents errors from showing up

  16. 16

    How can I render a controller action within a twig extension?

  17. 17

    How can I render a controller action within a twig extension?

  18. 18

    Symfony2: How to replace a certain string with a global Twig variable?

  19. 19

    Symfony2: how to make a twig extension return an image

  20. 20

    Symfony2: how to make a twig extension return an image

  21. 21

    Symfony2: How to access a service inside an extension's load() method?

  22. 22

    How to enable autoescaping in Twig using Symfony

  23. 23

    Symfony dependency injection in twig extension

  24. 24

    Activate StringLoader Twig Extension in Symfony

  25. 25

    Symfony Twig extension render view

  26. 26

    Access a variable within a sub function within a service function

  27. 27

    How to access a global variable within a local scope?

  28. 28

    How to access XMLHttpRequest within a Paw extension?

  29. 29

    How to access the asp control using $(this) within the setTimeout() function

HotTag

Archive