php - 前端 Controller 模式 - 路由器是前端 Controller 吗?

标签 php design-patterns slim front-controller

我想了解前端 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/

相关文章:

javascript - 为什么我的 angularjs 多选界面不起作用?

javascript - 当使用ajax在单行中切换按钮时,laravel将状态更新为0或1

php - if else if 语句不适用于 get

design-patterns - 策略设计模式与状态设计模式

php - 如何在类定义之外为魔术属性创建 PHPDoc?

php - 在 MySQL 之后保留 PHP 排序

iOS 设计模式将项目上传到网络,同时在 TableView 中显示进度

javascript - 强制子类不可变

php - 如何重新定义Slim v3的错误处理程序?

mysql - slim 的应用程序错误 : SQLSTATE[HY000] [2002] Connection refused in XAMPP