Skip to content

Instantly share code, notes, and snippets.

@reinink
Created February 20, 2014 12:16
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save reinink/9112262 to your computer and use it in GitHub Desktop.
Save reinink/9112262 to your computer and use it in GitHub Desktop.
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