search

Home  >  Q&A  >  body text

How to customize public functions in symfony and call them in templates?

How to customize public functions in Symfony and how to call them in templates?

迷茫迷茫2787 days ago486

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-05-16 16:46:59

    You can write your own service, For example, create a folder name in your bundle. Service Then create a php inside MyClass.php namespace .....BundleService; class MyClass

    {
    

    function HelloWord($value)

    {

    return 'hello'.$value;

    }

    }


    Add in config.yml

    parameters:

    myclass.class: ..\..Bundle\Service\MyClass  
    
    myclass.value: value  
    

    services:

    myclass:  
    
        class: "%myclass.class%"  
    
        arguments: [%myclass.value%]  
    

    When using it, write it in the controller

    $myclass= $this->get('myclass');

    echo $myclass->HelloWord("world");


    Detailed explanations are available on the official website http://symfony.com/fr/doc/current/book/service_container.html

    reply
    0
  • Cancelreply