Created
February 20, 2014 12:16
-
-
Save reinink/9112262 to your computer and use it in GitHub Desktop.
Using illuminate/container outside of Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": | |
{ | |
"illuminate/container": "4.0.*@dev", | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment