我可以独立使用 Laravel 的数据库层吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26083175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I use Laravel's database layer standalone?
提问by sjosen
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.
一段时间以来,我一直在考虑在我的工作中使用 php 框架。直到最近,我一直在编写程序风格,并且仍在努力寻找解决 oop 世界/风格的方法。我认为 php 框架可以帮助我编写更好的代码,而且我很确定在不久的将来我会倾向于 Laravel 项目。
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).
现在我需要一个可以在现有代码中使用的数据库层。我现在将 mysqli 与准备好的语句一起使用,因为它很容易实现(以前使用 MySQL)。
I've been looking at http://medoo.inas 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.
我一直在将http://medoo.in视为使用 pdo 包装器/类的“简单”方法,但支持页面上缺乏活动,而且我正在努力在未来使用 Laravel ,让我想知道我现在是否可以将 Laravel 数据库层用于我现有的代码。
Could this be done and would it make sense or am I misunderstanding and mixing concepts of code styling?
这可以完成吗,它是否有意义,还是我误解和混合了代码样式的概念?
回答by Bastian Hofmann
IMO it's absolutely valid to transition to an OOP approach step by step.
IMO 逐步过渡到 OOP 方法是绝对有效的。
To your question:
对于你的问题:
Yes, you can use Eloquent standalone.
是的,您可以独立使用 Eloquent。
Here is the packagist site: https://packagist.org/packages/illuminate/databaseAdd "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)
这是 packagist 站点:https://packagist.org/packages/illuminate/database添加"illuminate/database": "5.0.*@dev"
到您的composer.json
并运行composer update
. 现在你需要引导 Eloquent。( https://github.com/illuminate/database)
The following is copied from the repo's readme:
以下是从 repo 的自述文件中复制的:
Usage Instructions
使用说明
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.
首先,创建一个新的“Capsule”管理器实例。Capsule 旨在尽可能简单地配置库以在 Laravel 框架之外使用。
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();
Once the Capsule instance has been registered. You may use it like so:
一旦 Capsule 实例被注册。你可以像这样使用它:
Using The Query Builder
使用查询生成器
$users = Capsule::table('users')->where('votes', '>', 100)->get();
Other core methods may be accessed directly from the Capsule in the same manner as from the DB facade:
其他核心方法可以直接从 Capsule 访问,方式与从 DB Facade 相同:
$results = Capsule::select('select * from users where id = ?', array(1));
Using The Schema Builder
使用模式构建器
Capsule::schema()->create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->timestamps();
});
Using The Eloquent ORM
使用 Eloquent ORM
class User extends Illuminate\Database\Eloquent\Model {}
$users = User::where('votes', '>', 1)->get();
For further documentation on using the various database facilities this library provides, consult the Laravel framework documentation.
有关使用该库提供的各种数据库工具的更多文档,请参阅 Laravel 框架文档。
回答by merkdev
Bastian's answer is pretty well. But there is one more thing. It can't work without Event Lib. To install it;
巴斯蒂安的回答很好。但还有一件事。如果没有 Event Lib,它就无法工作。安装它;
composer require illuminate/events
You are good to go
你已准备好出发