我想了解前端 Controller 应该的样子。来自维基百科,
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."
那么,下面这段在 Slim 中处理路由的代码是前端 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();
最佳答案
provides a centralized entry point for handling requests.
是的,Slim 可以是某种前端 Controller 。它处理所有传入的请求并将它们带到正确的位置/ Controller 。
不要将前端 Controller 与 MVC 模式的 Controller 混淆。
在您的示例中,路由是前端 Controller 的一部分,应该调用您的 MVC 模式的 Controller 。这个 MVC Controller (在你的例子 $bookController 中)负责评估信息,将信息提交给 View 并显示 View 。因此,您的示例应如下所示:
//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");
}
关于php - 前端 Controller 模式 - 路由器是前端 Controller 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32389438/