You can use yes, as the illuminate / database repository itself, it is actually possible to use it independently of anything, or you can probably use it in any framework, the only dependencies is to install via composer
and have php5.6 +, ie this will work for both Silex and anything equivalent.
Type in the terminal within your project composer require "illuminate/database"
, if you use the events you also need the command composer require "illuminate/events"
Or set up composer.json (5.4 is the most current version before the development version, you can see other versions):
"illuminate/database": "~5.4",
"illuminate/events": "~5.4"
And then run composer update
.
Add autoload.php and call PHP like this:
<?php
use Illuminate\Database\Capsule\Manager as Capsule;
require __DIR__ . '/vendor/autoload.php';
$capsule = new Capsule;
//Exemplo mysql
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
// Define o dispatcher usado pelos models do Eloquent (opcional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));
// Faz essa instancia de Capsule ficar disponível globalmente usando metodos estaticos (opcional)
$capsule->setAsGlobal();
// Configura o Eloquent ORM... (opcional e desnecessário se você já usou setEventDispatcher())
$capsule->bootEloquent();
This part above you could put in a global file which would be accessible to everyone, or when you call a specific namespace, for example \Model\foo\bar
(I'll talk about it later).
After adding the above file, you can use:
-
QueryBuilder
$users = Capsule::table('users')->where('votes', '>', 100)->get();
-
Schema Builder
Capsule::schema()->create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->timestamps();
});
-
Eloquent ORM (I think this is what interests you):
class User extends Illuminate\Database\Eloquent\Model {}
$users = User::where('votes', '>', 1)->get();
Creating a namespace to use the Models
Now let's say you will not use ORM on every page, there's really no reason to load everything if you will not use it, so you may want to organize your project based on a namespace, I suggest doing something like:
Create in the composer.json a namespace to point to a folder where the models will be:
...
"require": {
"php": ">=5.6.4",
"illuminate/database": "~5.4",
"illuminate/events": "~5.4"
},
"autoload": {
"psr-4": {
"FooBarModel\": "app/Models"
}
},
...
This case is an example, assuming you have a folder named ./app/Models
within the project.
You can create an abstract Model that will be the basis of everything in ./app/Models/Model.php
, so you will avoid including Eloquent in calls that will not use the bank (of course this is just a way to organize)
In this case you should remove from the global file everything I mentioned before leaving only require
:
<?php
namespace FooBarModel;
use \Illuminate\Database\Capsule\Manager as Capsule;
use \Illuminate\Events\Dispatcher;
use \Illuminate\Container\Container;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
//Cria a classe
abstract class Model extends \Illuminate\Database\Eloquent\Model {}
So now in the same folder you can create a Model named User
( ./app/Models/User.php
):
<?php
namespace FooBarModel;
class User extends Model {}
If it is in a subfolder ( ./app/Models/Admin/FooBar.php
):
<?php
namespace FooBarModel\Admin;
class FooBar extends \FooBarModel\Model {}
Create such a global.php (or anything like that) that has require __DIR__ . '/vendor/autoload.php';
, or if you have a boot.php or your life index.php play there, assuming:
<?php
require __DIR__ . '/vendor/autoload.php';
Then to call a specific Model, to do so:
<?php
require __DIR__ . '/vendor/autoload.php';
$user = new FooBarModel\User\User;
An example with Silex might look like this:
<?php
use FooBarModel\User;
use FooBarModel\Admin\FooBar;
require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/', function ()
{
return 'Home';
});
$app->get('/user', function ()
{
$user = new User;
....
});
$app->get('/user', function ()
{
$user = new FooBar;
....
});
$app->run();