简体   繁体   English

如何在外部应用程序中使用Laravel模型?

[英]How to use Laravel models in an external application?

I currently have two applications that run on the same server, under separate virtualhosts. 我目前有两个应用程序在不同的虚拟主机下运行在同一台服务器上。 The first is a Laravel 4 app and the second is a older PHP project that doesn't use a framework, but follows a rough MVC pattern. 第一个是Laravel 4应用程序,第二个是较旧的PHP项目,它不使用框架,但遵循粗略的MVC模式。

I'd like to find a way to interact with and use some of the models from the Laravel app inside of the older PHP project. 我想在旧的PHP项目中找到一种与Laravel应用程序交互并使用其中一些模型的方法。

Obviously it's not going to be as simple as just including the model files, as they will have various dependencies, so are there any general approaches or patterns that I should be looking at when trying to do this? 显然,它不会像仅包含模型文件那样简单,因为它们将具有各种依赖性,因此,在尝试执行此操作时,是否应该考虑任何常规方法或模式?

You can add illuminate/database to your composer.json . 您可以在composer.json添加illuminate/database That way you can use database module of laravel without installing whole framework. 这样你就可以使用laravel的数据库模块而无需安装整个框架。

Read the instructions in README.md for code examples and more details. 阅读README.md的说明以获取代码示例和更多详细信息。

Can Vural is right, you can just use these components, take a look at http://www.slimframework.com/news/slim-and-laravel-eloquent-orm . Vural是否正确,你可以使用这些组件,看看http://www.slimframework.com/news/slim-and-laravel-eloquent-orm

Here are the main extracts: 以下是主要摘录:

INSTALLATION 安装

First, prepare the composer.json file so it will pull down and install the Slim Framework and the Eloquent ORM. 首先,准备composer.json文件,以便下拉并安装Slim Framework和Eloquent ORM。 The composer.json file should look like this: composer.json文件应如下所示:

{
    "require": {
        "slim/slim": "*",
        "illuminate/database": "*"
    }
}

When this is done, run composer install to install the application dependencies. 完成此操作后,运行composer install来安装应用程序依赖项。

BOOTSTRAP THE ELOQUENT ORM BOOTSTRAP ELOQUENT ORM

Next, I tell Composer to autoload the application's dependencies by requiring Composer's autoload.php file. 接下来,我告诉Composer通过要求Composer的autoload.php文件来自动加载应用程序的依赖项。

<?php
// Autoload our dependencies with Composer
require '../vendor/autoload.php';

And now I bootstrap the Eloquent ORM and pass it my database connection information (be sure you add your own username, password, and database name). 现在我引导Eloquent ORM并将其传递给我的数据库连接信息(确保添加自己的用户名,密码和数据库名称)。

<?php
// Database information
$settings = array(
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'database' => '',
    'username' => '',
    'password' => '',
    'collation' => 'utf8_general_ci',
    'prefix' => ''
);

// Bootstrap Eloquent ORM
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory();
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);

Now that the Eloquent ORM is bootstrapped, I can create and use models that extend the Eloquent abstract model. 既然Eloquent ORM已启动,我可以创建和使用扩展Eloquent抽象模型的模型。 This example assumes your database contains a table named books with columns title and author. 此示例假定您的数据库包含一个名为books的表,其中包含列标题和作者。

<?php
class Book extends \Illuminate\Database\Eloquent\Model
{

}

Extract taken from the website mentioned above, this is so if it gets removed the information still remains. 从上面提到的网站中提取的摘录,如果它被删除,那么信息仍然存在。

To load from a different project, you will most likely have to autoload the models with a custom autoloader. 要从其他项目加载,您很可能必须使用自定义自动加载器自动加载模型。 Or include the autoloader from the Laravel framework if you have dependencies within the models. 或者,如果模型中存在依赖关系,请包含Laravel框架中的自动加载器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM