在Web MVC框架中,谁有责任显示404页面?

发布于 2024-10-31 13:04:48 字数 412 浏览 1 评论 0原文

在Web MVC模型中,哪个类/对象有责任/功能调用404页面?为什么它有责任?我认为是路由器,但我只是为了以防万一而问。

$router->show_404("Unable to find controller.");

另外,404 页面应该是一个视图还是只是一个模板?这里的标准做法是什么?

-- 更新,让事情变得更清楚 --

例如,如果我们同意 Router 类拥有调用 404 页面的功能/职责,那么我们应该注入 Router对象到我们实例化的控制器中,以便每个控制器都可以使用Router对象来调用404s?或者我应该创建一个自定义类来显示“特殊页面”,例如 Error/404s?

感谢您的回答。

In a web MVC model, which class/object has the responsibility/function to call 404 page? Why does it has the responsibility? I think it's the Router, but I'm asking just in case.

$router->show_404("Unable to find controller.");

Also, should a 404 page be a view or just a template? What's the standard practice here?

-- Update, makes things much clearer --

If for example, we agree that the the Router class holds the function/responsibility to call 404 page, should we then inject Router object to the controller we instantiated so that each controller can use the Router object to call 404s? Or should I create a custom class built to display 'special pages' like Error/404s?

Thanks for the answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风不识月 2024-11-07 13:04:48

视情况而定。

如果是由于无效路由造成的,则可以由路由器本身处理,也可以使用指向 404 处理程序的默认路由。

如果是由于有效路由接收到无效数据(/user/JohnDoe,但系统不知道 John Doe),则必须由该路由的处理程序启动 404。

一旦 404 被触发,我倾向于更喜欢轻量、快速的页面来报告它,但是有很多网站会进行近似匹配搜索并返回一个列表“您是指这些事情之一吗?” ”我不确定在这两种情况下我会称之为“标准实践”,除非你谈论的是 REST API,它通常会发送最小的响应,因为它们的目标是机器可读性,所以没有太多提出一个只有人类才能回答的问题是很重要的。

Depends on the situation.

If it's due to an invalid route, then it can either be handled by the router itself or you can have a default route which points to a 404 handler.

If it's due to a valid route receiving invalid data (/user/JohnDoe, but John Doe isn't known to the system), then the 404 would have to be initiated by the handler for that route.

Once the 404 is triggered, I tend to prefer light, fast pages reporting it, but there are plenty of sites out there which will, e.g., do an approximate-match search and return a list of "did you mean one of these things?" I'm not sure which way I would call "standard practice" in either case, unless you're talking about REST APIs, which would normally send a minimal response, since they're aimed at machine-readability, so there's not a lot of point in asking a question that only a human could answer.

擦肩而过的背影 2024-11-07 13:04:48

我更喜欢“驱动程序”中的类似内容:

try
{
  $router->handleRequest($request);
}
catch (Exception $e)
{
  $view = new View('error');
  echo $view;
}

404 只是由 catch 块处理的另一个异常。如果你想用它做一些特殊的事情,你总是可以有一个特定的 404 异常。或者您可以有一个通用的“http 异常”,其中包含一些指示要使用哪个模板的状态代码。

I prefer something like this in the "driver":

try
{
  $router->handleRequest($request);
}
catch (Exception $e)
{
  $view = new View('error');
  echo $view;
}

A 404 is just another exception handled by the catch block. You could always have a specific 404 exception if you want to do something special with it. Or you could have a generic "http exception" that includes some status code that would indicate which template to use.

疯狂的代价 2024-11-07 13:04:48

报告错误是视图的责任。并且需要相应地设计视图代码以报告有意义的错误消息

<web-app ...> 

  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/pages/404_error.jsp</location>
  </error-page>

</web-app>

It is the responsibility of the View, to report the error. And view code need to be designed accordingly to report the meaningful error messages

<web-app ...> 

  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/pages/404_error.jsp</location>
  </error-page>

</web-app>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文