简体   繁体   English

超薄框架和雄辩的ORM

[英]Slim Framework and Eloquent ORM

I'm using Slim Framework together with Laravel's Eloquent ORM and this is my code: 我正在使用Slim FrameworkLaravel's Eloquent ORM ,这是我的代码:

User.php user.php的

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

index.php 的index.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.php file in my index.php . 正如您在我的代码中看到的,我必须在index.php包含User.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. 我可以只包含一个文件夹,所有模型也将被包含在内,这样它就不会看起来很杂乱,包括索引中的每个模型文件。

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? 有一个更清洁的方式吗?

You can use Composer to automatically include classes from your project. 您可以使用Composer自动包含项目中的类。 Let's say your composer.json file lives in app . 假设您的composer.json文件存在于app Then you can use the classmap attribute in your composer.json to automatically include all classes in models : 然后,你可以使用classmap在你的属性composer.json自动包含在所有的类models

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

The classmap tells Composer to map all classes in the specified directory(ies). classmap告诉Composer映射指定目录中的所有类。 Then, all you need to do is run composer update to update Composer's list of includes whenever you add a new file to this directory. 然后,您需要做的就是运行composer update以在每次向此目录添加新文件时更新Composer的包含列表。

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. 这将自动包含位于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) 如果您的命名空间是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. 注册自动装带器可以防止这种情况,只需加载必要的代码。

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

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