FewFlyBy FewFlyBy - 1 year ago 96
PHP Question

Slim Framework and Eloquent ORM

I'm using

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


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


require_once 'vendor/autoload.php';

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

$app = new \Slim\Slim();

// Database information
$settings = array(
'driver' => 'mysql',
'host' => '',
'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);

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


As you can see in my code, I have to include the
file in my
. 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.

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" : [

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.