【发布时间】:2014-04-11 00:30:30
【问题描述】:
任何人都知道如何在 Silex 中集成 Red bean ORM。任何可用的示例/文档。请帮助我。
谢谢
【问题讨论】:
任何人都知道如何在 Silex 中集成 Red bean ORM。任何可用的示例/文档。请帮助我。
谢谢
【问题讨论】:
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。
它仍在进行中,非常感谢所有贡献:)
【讨论】:
我为 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 提供了一个静态类外观 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();
【讨论】:
$parameters = Yaml::parse(file_get_contents('parameters.yml'))。当您说每个页面时,您是指呈现页面的每个路由/控制器操作吗?
我今天才做的。 只需由 Composer 加载两个库(Silex 和 RedBean)。确保释放 Fat Silex 以保持安全。 并且.. 以一种优雅的方式,您应该创建一个加载 RedBean 的控制器,而不是将控制器作为服务提供者,然后您的 RedBean 设置将在您的应用程序中的每个位置可用。 我将在下几天制作 RedBeanServiceProvider。
【讨论】: