简体   繁体   中英

Slim Framework and Eloquent ORM

I'm using Slim Framework together with Laravel's Eloquent ORM and this is my code:

User.php

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

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 . 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. Let's say your composer.json file lives in app . Then you can use the classmap attribute in your composer.json to automatically include all classes in 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). 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.

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.

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)


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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