Mark Purnell
Mark Purnell

Reputation: 191

Including Custom Classes

I've just installed Symfony 2 and I'm looking at including my own classes into my project, but am having trouble getting the results I am after.

I'd like a controller to be able to call "MyClass.php" and run functions from it without the use of includes. After a bit of searching, it seems that classes need to be placed within a specific folder for them to be picked up automatically and used by the 'app'. I currently believe it to be the "lib" folder within the project bundle root.

At the moment my project sits within src/Bundle/ProjectBundle.

I have tried storing the Test class within Test.php inside ProjectBundle/lib, and have tried naming it 'Test.php' and Test.class.php, but my controller is still unable to find the file/class.

Where do I place my class files?

How should I name my class files? (aside from the standard naming conventions)

Am I barking up the wrong tree completely?

edit: The class to be called.

<?php
class Test
{
    public function callMe()
    {
        return "FUNCTION 1 SUCCESSFULLY CALLED";
    }
}
?>

Upvotes: 0

Views: 92

Answers (1)

insertusernamehere
insertusernamehere

Reputation: 23600

You can do the following. Place the class in a sub-directory of your project and in the controller use use to register the namespace like this:

// the last part is the class name you want to include
use Vendor\BundleNameBundle\Utility\Factory;

The class would be located in: src/Vendor/BundleNameBundle/Utility/Factory.php.

Upvotes: 2

Related Questions