Autoloading a class in Symfony 2.1

前端 未结 2 975
囚心锁ツ
囚心锁ツ 2020-12-11 17:37

I\'m porting a Symfony 1.2 project to Symfony 2.x. I\'m currently running the latest 2.1.0-dev release.

From my old project I have a class called Tools which has som

2条回答
  •  时光取名叫无心
    2020-12-11 18:28

    For a simple case like this the quickest solution is creating a folder (for example Common) directly under src and put your class in it.

    src
      -- Common
        -- Tools.php
    

    Tools.php contains your class with proper namespace, for example

    Before calling your function do not forget the use statement

    use Common\Tools;
    
    // ...
    Tools::slugify('my test string');
    

    If you put your code under src following the proper folder structure and namespace as above, it will work without touching app/autoload.php.

提交回复
热议问题