Wednesday, February 9, 2022

[FIXED] How to include a custom php file?

Issue

I have added a new directory and a new file into src/AppBundle. So the path is: src/AppBundle/Helper/Helper.php

The path of my controller is as usual src/AppBundle/Controller/MyController.php.

I would now like to include the file Helper.php to my Controller. How can I do this?


Solution

One possibility, assuming your AppBundle/Controller/MyController in Symfony 2.8 it looks something like this:

namespace AppBundle\Controller\MyController

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller
{
...
}

and your Helper class includes public function getSomeHelp()

you can then define a service in services.yml:

services
    app.helper:
        class: 'AppBundle\Helper\Helper'

and in your MyController:

public function someFunctionAction() {
    ...
    $helper = $this->get('app.helper');
    $something = $helper->getSomeHelp();


Answered By - geoB

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.