dongyishen5796 2013-02-18 11:21
浏览 20
已采纳

包括自定义类

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";
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dtpn60029 2013-02-18 11:30
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?