0

I'm new to the Slim Framework and I'm trying to build a simple webapp connected with PDO to a MySQL database (and with a Twig/Bootstrap UI).

I try to access database from a controller named "PagesController" :

class PagesController extends Controller {
    public function getLieu(RequestInterface $request, ResponseInterface $response) {
        // this two lines crash :
        $lieux = $this->$database->query('SELECT * FROM Lieu');
        var_dump($lieux);

        $this->render($response, 'pages/lieu.twig');
    }
}

But I'm only able to call my PDO object from index.php or the from the parent abstrat class named "Controller" :

class Controller {
    private $container;

    public function __construct($container) {
        $this->container = $container;
        // those 3 lines works well :
        $database = $this->container->get('db');
        $lieux = $database->query('SELECT * FROM Lieu');
        var_dump($lieux);
    }

    public function render(ResponseInterface $response, $file) {
        $this->container->view->render($response, $file);
    }
}

Is somebody here can help me ?

Thank you !

joedu12
  • 33
  • 1
  • 6

1 Answers1

0

Make the database property protected.

Also: $this->$database is wrong. Correct is $this->database.

Be aware that your class design is based on the service locator antipattern. E.g. you are passing the whole container to your controllers in order to get the database object from it. Don't do this! Inject only the resources that a class needs. E.g. your controller should receive only the database instance as dependency. Take a look at this, this, this and, especially, this.

You don't actually want to inject the database instance either. You should inject it into proper data mappers. And the data mappers should be injected into the controllers. In principle, the process of accessing data from a persistence layer is the responsibility of more MVC components. Read this and this. And this serie for code examples involving domain objects, database adapters, data mappers, repositories and services: Part 1, Part 2, Part 3 and Part 4.

A note: in Slim you can use the Eloquent ORM. See this.

Suggestion: You should learn OOP before starting (using) an MVC and familiarize yourself with the SOLID principles too.

class Controller {

    private $container;
    protected $database;

    public function __construct($container) {
        $this->container = $container;
        $this->database = $this->container->get('db');

        $lieux = $this->database->query('SELECT * FROM Lieu');
        var_dump($lieux);
    }

    public function render(ResponseInterface $response, $file) {
        $this->container->view->render($response, $file);
    }

}

class PagesController extends Controller {

    public function getLieu(RequestInterface $request, ResponseInterface $response) {
        $lieux = $this->database->query('SELECT * FROM Lieu');
        var_dump($lieux);

        $this->render($response, 'pages/lieu.twig');
    }

}
PajuranCodes
  • 303
  • 3
  • 12
  • 43
  • Thank you very munch for your response! You made my evening. The main problem was the "$" behind database. Yes I felt that give the database object to every controller may not be a great idea. I'll watch and read your links to understand more how to build a great MVC application. Thank you! – joedu12 Mar 15 '18 at 22:54
  • You are very welcome. I reedited with more links (a serie with code examples) and a suggestion. – PajuranCodes Mar 15 '18 at 23:07