问题描述
我想了解Front Controller 应该如何的样子。从维基百科,
I'm trying to understand how a Front Controller should look like. From Wikipedia,
所以,下面的代码是处理Slim中的路由前控制器?
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();
推荐答案
是的,Slim可以是某种前端控制器。它处理所有传入的请求并将它们带到正确的位置/控制器。
Yes, Slim can be some kind of a front-controller. It handles all incoming requests and brings them to the right place/controller.
不要将前端控制器与MVC模式的控制器混淆。 strong>
Do not confound the front-controller with the controller of a MVC pattern.
在您的示例中,路由是前端控制器的一部分,应该调用MVC模式的控制器。这个MVC控制器(在你的例子$ bookController中)负责评估信息,提交信息到视图并显示视图。所以你的例子应该如下所示:
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");
}
这篇关于前控制器模式 - 路由器是前端控制器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!