将Silex与Redbean Orm集成

苯胺

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

谢谢

亚当·埃尔索达尼

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

由于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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章