我可以独立使用Laravel的数据库层吗? [英] Can I use Laravel's database layer standalone?

查看:261
本文介绍了我可以独立使用Laravel的数据库层吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在考虑使用php框架进行工作.直到最近,我一直在编写过程样式,并且仍在尝试尝试oop世界/样式. 我认为php框架将帮助我编写更好的代码,并且我敢肯定,不久的将来我会倾向于Laravel项目.

For some time now I have been looking towards using a php framework for my work. I've been writing procedural style until recently and is still trying to find my way around the oop world/style. I figured that a php framework would help me write better code and I'm pretty sure I'll lean towards the Laravel project in a near future.

现在,我需要可以在现有代码中使用的数据库层.我现在将mysqli与已准备好的语句一起使用,因为它很容易实现(以前使用MySQL).

Right now I'm in need of a database layer that I can use in my existing code. I use mysqli with prepared statements right now, as it was easy for me to implement (using MySQL before).

我一直在查看 http://medoo.in 作为使用pdo包装器的简便"方法/class,但是支持页面上没有活动,并且我将来还会使用Laravel,这使我想知道是否可以将Laravel数据库层用于现有代码.

I've been looking at http://medoo.in as an "easy" way to use a pdo wrapper/class but the lack of activity on the support page, and the fact that I'm working on using Laravel in the future, made me wonder if I could use the Laravel database layer now for my existing code.

这可以做到吗?是否有意义?或者我是否误解并混淆了代码样式的概念?

Could this be done and would it make sense or am I misunderstanding and mixing concepts of code styling?

推荐答案

IMO,逐步过渡到OOP方法是绝对有效的.

IMO it's absolutely valid to transition to an OOP approach step by step.

对您的问题:

是的,您可以使用Eloquent独立版本.

Yes, you can use Eloquent standalone.

这是包装专家网站: https://packagist.org/packages/illuminate/database"illuminate/database": "5.0.*@dev"添加到您的composer.json并运行composer update. 现在,您需要引导Eloquent. ( https://github.com/illuminate/database )

Here is the packagist site: https://packagist.org/packages/illuminate/database Add "illuminate/database": "5.0.*@dev" to your composer.json and run composer update. Now you'll need to bootstrap Eloquent. (https://github.com/illuminate/database)

以下内容是从存储库的自述文件中复制的:

The following is copied from the repo's readme:

使用说明

首先,创建一个新的胶囊"管理器实例. Capsule旨在使配置该库以在Laravel框架之外使用变得尽可能容易.

First, create a new "Capsule" manager instance. Capsule aims to make configuring the library for usage outside of the Laravel framework as easy as possible.

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => 'password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
]);

// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));

// Set the cache manager instance used by connections... (optional)
$capsule->setCacheManager(...);

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();

一旦注册了Capsule实例.您可以这样使用它:

Once the Capsule instance has been registered. You may use it like so:

使用查询生成器

$users = Capsule::table('users')->where('votes', '>', 100)->get();

可以直接从Capsule访问其他核心方法,方法与从DB Facade相同:

Other core methods may be accessed directly from the Capsule in the same manner as from the DB facade:

$results = Capsule::select('select * from users where id = ?', array(1));

使用架构生成器

Capsule::schema()->create('users', function($table)
{
    $table->increments('id');
    $table->string('email')->unique();
    $table->timestamps();
});

使用雄辩的ORM

class User extends Illuminate\Database\Eloquent\Model {}

$users = User::where('votes', '>', 1)->get();

有关使用该库提供的各种数据库功能的更多文档,请查阅Laravel框架文档.

For further documentation on using the various database facilities this library provides, consult the Laravel framework documentation.

这篇关于我可以独立使用Laravel的数据库层吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆