douguio0185 2012-11-20 12:42
浏览 18

Silex自定义类结构和加载

I'm building an app using Silex (the micro-framework).

As my app are growing in size and the need of using the same code in several routes rises, I want to organise everything a little more..

My idea is to create some custom classes, and then share them with my app:

$app['test'] = $app->share(function () {
    require_once('../vendor/acme/src/test.php');
    $testClass = new Test();
    return new $testClass;
});

This actually works, but I need help with the following:

  1. Autoload the class with composer (the way its supposed to be done in Silex).
  2. Be able to use the existing Doctrine DBAL connection and methods within my class.

I hope someone can give me some tips how to get on, because I'm not finding the Silex docs very useful and I'm a beginner with both Silex and Composer.

  • 写回答

1条回答 默认 最新

  • douya5194 2012-11-20 13:15
    关注

    Check the composer docs about autoloading, and when you added your config you should run composer dump-autoload to regenerate the composer autoloader. Then your require_once should not be necessary anymore.

    Most likely this will work (assuming class Test is in src/Test.php):

    {
        "autoload": {
            "psr-0": {
                "": "src/"
            }
        }
    }
    

    This will make any PSR-0 compliant class inside src/ autoloadable.

    Regarding your second point (using DBAL in your class), you should configure your class as a silex service that accesses the db service. Read up on services at http://silex.sensiolabs.org/doc/services.html

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计