4

I'm trying to understand how a Front Controller should look like. From Wikipedia,

The Front Controller pattern is a software design pattern listed in several pattern catalogs. The pattern relates to the design of web applications. It "provides a centralized entry point for handling requests."

So, is the code below that handles routes in Slim a front controller?

$app = new \Slim\Slim();
$app->get('/books/:id', function ($id) use ($app) {

    // Get all books or one book.
    $bookModel = new ...
    $bookController = new ...

    $app->render('myTemplate.php', array('id' => $id, ...));
});

$app->run();
NikiC
  • 100,734
  • 37
  • 191
  • 225
Run
  • 54,938
  • 169
  • 450
  • 748

1 Answers1

4

provides a centralized entry point for handling requests.

Yes, Slim can be some kind of a front-controller. It handles all incoming requests and brings them to the right place/controller.

Do not confound the front-controller with the controller of a MVC pattern.

In your example the route is part of the front-controller and should call the controller of your MVC pattern. This MVC-controller (in your exmaple $bookController) is responsible for evaluating information, submitinig information to the view and display the view. So your example should look like the following:

//Inside of your frontcontroller, defining the route:
$app->get("/books/:id", "bookController:displayBook");

//Inside of your MVC bookController class:
public function displayBook($id)
{
    $book = Book::find($id);
    $app->view->set("book", $book);
    $app->view->render("form_book.php");
}
  • Thanks but why is there View in your MVC bookController class? I believe that the Controller shouldn't contain the View in it - that's what the Front Controller for - to have View, Model and Controller in it. – Run Sep 07 '15 at 01:44
  • 1
    Well, the MVC-controller at least needs to know the view so it can modify it and submit the information to the view. The view itself is stored within the $app-object, so the front-controller also knows it. And then you can render the view wherever you want. –  Sep 07 '15 at 08:33