2

I am following the Destructy tutorial and, after setting up and testing out the database connection I get the following error:

So far, this are my settings:

return [
    'settings' => [
        'displayErrorDetails' => true,
    ],
    'url'   =>  'http://localhost',
    'db'    =>  [
        'mysql' =>  [
            'host'      =>  'localhost',
            'dbname'    =>  'destructy',
            'username'  =>  'username',
            'password'  =>  'password',
            'charset'   =>  'utf8',
            'collation' =>  'utf8_unicode_ci',
            'prefix'    =>  '',
        ]
    ],
];

This is how the container is defined:

/*Set Slim up*/

$container = new \Slim\Container();//Ver si le afecta o no los paréntesis ()

$container['settings'] = function($c){
  return $c['config']->get('settings');
};

$container['config'] = function($c){ //c= current state of our container
    return new \Noodlehaus\Config('../config/app.php');//We could pass more parameters ... as an array ['param1','param2','etc']
};
$container['view'] = function($c){//c= current state of our container
    $view = new Slim\Views\Twig('../resources/views');//Ubicación de las vistas

    //Ability to generate URLs to routes from within our views
    $view->addExtension(new \Slim\Views\TwigExtension(
        $c['router'],
        $c['config']->get('url')
    ));

    return $view;
};
//base de datos:
$container['db'] = function($c){
    //return new PDO('mysql:host=127.0.0.1;dbname=destructy','root','root');
    return new PDO(
        'mysql:host='.$c['config']->get('db.mysql.host').';dbname='.$c['config']->get('db.mysql.dbname'),
        $c['config']->get('db.mysql.username'),
        $c['config']->get('db.mysql.password'));
};

$app = new Slim\App($container);

require_once 'routes.php';

The route:

$app->get('/',function($request,$response,$args){
    echo 'Destructy - Home.';
    echo '<p style="color: darkblue">Welcome to the admin area</p>';
    echo '<p>Testing access to config parameters ...</p>';
    echo $this->config->get('db.mysql.host').'<br>';
    echo $this->config->get('url');

    /*Testing out DB connection*/
    var_dump($this->db);

    $this->view->render($response,'home.twig');
});

$app->run();

Does anybody know what the problem is in order to fix it?

alexw
  • 8,468
  • 6
  • 54
  • 86
Pathros
  • 10,042
  • 20
  • 90
  • 156

1 Answers1

1

You're not able to use the dot syntax to access the config values, should be something like:

$container['db'] = function($c){
    $conf = $c->db;
    return new PDO(
        'mysql:host='.$conf['mysql']['host'].';dbname='.$conf['mysql']['dbname'],
        $conf['mysql']['username'],
        $conf['mysql']['password'];
};
the-noob
  • 1,322
  • 2
  • 14
  • 19
  • 1
    I'm hoping silentworks will put out an updated [tutorial](http://thoughts.silentworks.co.uk/slim-php-101-using-laravel-config-package/) for integrating Laravel's Config package into Slim 3. – alexw Mar 29 '16 at 04:40