laravel Slim 框架和 Eloquent ORM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33512676/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:38:06  来源:igfitidea点击:

Slim Framework and Eloquent ORM

phplaraveleloquentcomposer-phpslim

提问by wobsoriano

I'm using Slim Frameworktogether with Laravel's Eloquent ORMand this is my code:

我使用的Slim Framework一起Laravel's Eloquent ORM,这是我的代码:

User.php

用户名.php

class User extends \Illuminate\Database\Eloquent\Model
{
    protected $table = 'accounts';
}

index.php

索引.php

require_once 'vendor/autoload.php';

// Models
include 'app/models/User.php';

$app = new \Slim\Slim();

// Database information
$settings = array(
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'database' => 'photo_mgmt',
    'username' => 'root',
    'password' => '',
    'collation' => 'utf8_general_ci',
    'prefix' => '',
    'charset'   => 'utf8',
);

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

$app->get('/', function () use ($app) {
    $users = \User::all();
    echo $users->toJson();
});

$app->run();

As you can see in my code, I have to include the User.phpfile in my index.php. But what if I have multiple models? Can I just include a folder and all models will also be included so that it won't look messy including every model file in my index.

正如您在我的代码中看到的,我必须将该User.php文件包含在我的index.php. 但是如果我有多个模型呢?我可以只包含一个文件夹,所有模型也将包含在内,这样它就不会看起来很混乱,包括我的 index.html 中的每个模型文件吗?

Thank you in advance.

先感谢您。

UPDATE:I'm using this piece of code I saw

更新:我正在使用我看到的这段代码

foreach (glob("app/models/*.php") as $filename)
{
    include $filename;
}

Is there a cleaner looking way?

有没有更干净的方法?

回答by alexw

You can use Composer to automatically include classes from your project. Let's say your composer.jsonfile lives in app. Then you can use the classmapattribute in your composer.jsonto automatically include all classes in models:

您可以使用 Composer 自动包含项目中的类。假设您的composer.json文件位于app. 然后您可以使用您的classmap属性composer.json自动包含所有类models

...
"require": {
    "php" : ">=5.4.0",
    "slim/slim" : "2.*",
    "illuminate/database" : "5.0.33",
    ...
},
"autoload": {
    "classmap" : [
        "models"
    ]
}

The classmaptells Composer to map all classes in the specified directory(ies). Then, all you need to do is run composer updateto update Composer's list of includes whenever you add a new file to this directory.

classmap告诉作曲家的所有类在指定的目录(IES)映射。然后,您需要做的就是composer update在您向该目录添加新文件时运行以更新 Composer 的包含列表。

回答by Félix Gagnon-Grenier

Yes, there is a much cleaner way to do this, namely autoloading.

是的,有一种更简洁的方法可以做到这一点,即自动加载。

It boils down to the use of spl_autoload_register()and of a custom class loader.

它归结为spl_autoload_register()和自定义类加载器的使用。

The principle is to mimic the namespace with the file hierarchy and load these accordingly to the namespace:

其原理是用文件层次结构模拟命名空间,并将它们相应地加载到命名空间中:

$loader = function load($class)
{
    include __DIR__."/app/$class.php";
}
spl_autoload_register($loader);
$user = new models\User();

This will automatically include the file located at app/models/User.php. It is a good practice to respect uppercases in your namespace; if you namespace is Model\User, the directory should respect the casing (app/Model/User.php)

这将自动包含位于 app/models/User.php 的文件。尊重命名空间中的大写是一个好习惯;如果你的命名空间是 Model\User,目录应该尊重大小写 (app/Model/User.php)



The problem with your current solution:

您当前解决方案的问题:

foreach (glob("app/models/*.php") as $filename)
{
    include $filename;
}

is that it will load all classes, even if the script will not use them. Registering an autoloader will prevent that, only loading the necessary code.

是它将加载所有类,即使脚本不会使用它们。注册自动加载器将阻止这种情况,只加载必要的代码。