【问题标题】:Integrate silex with Redbean orm将 silex 与 Redbean orm 集成
【发布时间】:2014-04-11 00:30:30
【问题描述】:

任何人都知道如何在 Silex 中集成 Red bean ORM。任何可用的示例/文档。请帮助我。

谢谢

【问题讨论】:

    标签: orm silex redbean


    【解决方案1】:

    FWIW,我还写了一个RedBean service provider for Silex。它允许您使用 RedBean 作为实例而不是静态外观(我个人不喜欢):

    $book = $app['redbean']->dispense('book');
    $book->title = 'PHP for dummies';
    $app['redbean']->store($book);
    

    因此,RedBean 在您的 Silex 应用程序中随处可用,而无需在每个文件中使用 use Redbean_Facade

    它仍在进行中,非常感谢所有贡献:)

    【讨论】:

    • 您的提供程序是否使用 4.0 版的 redbean?
    【解决方案2】:

    我为 Silex 中的 RedBean 创建了一个Service Provider
    只需将其放入您的 composer.json 并更新:

    "ivoba/redbean-service-provider": "dev-master"
    

    然后注册提供者:

    $app->register(new Ivoba\Silex\RedBeanServiceProvider(), array('db.options' => array( 'dsn' => 'sqlite:/tmp/db.sqlite' )));
    

    像这样使用它:

    use RedBean_Facade as R;
    ...
    $app['db']; //call once to init RedBean
    ...
    $e = R::findAll('table',' ORDER BY date DESC LIMIT 2');
    

    【讨论】:

    • 当我尝试添加最新版本的 redbean 时,您的服务提供商无法使用它。您会添加对 4.0+ 版本的支持吗?红豆的?
    • 我现在在 ~2.0 版本中添加了 4.0 支持
    【解决方案3】:

    这是一个非常基本的设置,如下所示。

    由于 Redbean 提供了一个静态类外观 RedBean_Facade,在文档中称为 R,因此几乎不需要将其注册为 Silex 应用程序的服务。

    理想情况下,您应该使用 Composer 同时安装 Silex 和 Redbean。在 Redbean Github 页面上,它告诉您如何将它与 Composer 一起使用。

    <?php
    
    require __DIR__.'/vendor/autoload.php';
    
    use Silex\Application;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use RedBean_Facade as R;
    
    $app = new Application();
    
    $app['db.connection.default.driver'] = "mysql";
    $app['db.connection.default.host'] = "localhost";
    $app['db.connection.default.name'] = "mydatabase";
    $app['db.connection.default.user'] = "user";
    $app['db.connection.default.password'] = "password";
    
    $app['db.connection.default.rsn'] = $app->share(function () use ($app) {
        return sprintf('%s:host=%s;dbname=%s',
            $app['db.connection.default.driver'],
            $app['db.connection.default.host'],
            $app['db.connection.default.name']
        );
    });
    
    R::setup(
        $app['db.connection.default.rsn'],
        $app['db.connection.default.user'],
        $app['db.connection.default.password']
    );
    
    $app->get('/article/{id}/show', function ($id) {
        $article = R::load('article', $id );
    
        // do something with $article, then
        // return an HTML page of the article.
    });
    
    $app->get('/article/new', function () {
        // return an HTML page with a form that contains
        // input fields with HTML name attributes set to
        // 'title' and 'body' for example.
    });
    
    $app->post('/article/new', function (Request $request) use ($app) {
        $article = R::dispense('article');
    
        $article->title = $request->request->get('title');
        $article->body = $request->request->get('body');
    
        $id = R::store($article);
    
        return $app->redirect(sprintf('/article/%d/show', $id));
    });
    
    $app->run();
    

    【讨论】:

    • 我应该在每个页面中给出这个连接字符串吗?有没有类似 parmeters.yml 的东西?
    • 在 Silex 中,并不是所有内容都可以在一个 PHP 文件中,例如我的答案中的代码。但是,您可以安装 Symfony Yaml 组件并解析您自己的文件,例如 $parameters = Yaml::parse(file_get_contents('parameters.yml'))。当您说每个页面时,您是指呈现页面的每个路由/控制器操作吗?
    • 我刚刚意识到你刚刚发表了评论。抱歉回复晚了。
    【解决方案4】:

    我今天才做的。 只需由 Composer 加载两个库(Silex 和 RedBean)。确保释放 Fat Silex 以保持安全。 并且.. 以一种优雅的方式,您应该创建一个加载 RedBean 的控制器,而不是将控制器作为服务提供者,然后您的 RedBean 设置将在您的应用程序中的每个位置可用。 我将在下几天制作 RedBeanServiceProvider。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 2020-08-03
      • 2011-12-07
      • 2023-03-06
      • 2011-06-07
      • 2013-02-26
      相关资源
      最近更新 更多