Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using illuminate/container outside of Laravel
<?php
// Vendor includes
include $config->base_path . '/libraries/Vendor/autoload.php';
// Include configuration file
$config = include('../config.php');
// Create IoC container
$ioc = new \Illuminate\Container\Container;
// Create request object
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$ioc->instance('Symfony\Component\HttpFoundation\Request', $request);
// Setup database connection
$ioc->singleton(
'PDO',
function () use ($config) {
$db = new \PDO('mysql:dbname=' . $config->db->name . ';host=' . $config->db->host, $config->db->username, $config->db->password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'UTF8'");
return $db;
}
);
// Template template engine
$ioc->singleton(
'League\Plates\Engine',
function () use ($config) {
return new \League\Plates\Engine($config->base_path . '/views', 'tpl');
}
);
// Setup router
// Note the router callback, which allows me to tie in
// the IoC container to help instantiate controllers
$router = new \Reinink\Roundabout\Router(
$request,
function ($class, $method, $parameters) use ($ioc) {
return call_user_func_array(array($ioc->make($class), $method), $parameters);
}
);
{
"require":
{
"illuminate/container": "4.0.*@dev",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment