MVC - URI 中没有控制器?

发布于 2024-10-23 18:55:38 字数 432 浏览 1 评论 0原文

假设我正在尝试学习 CI,作为我的测试项目,我正在构建一个团购网站。

我想要的是每个城市有一个不同的页面,例如:

http://www.groupon.com/las-vegas/
http://www.groupon.com/orlando/

我也想要有不同的页面,例如:

http://www.groupon.com/learn
http://www.groupon.com/contact-us

如果我在 CI 中构建这个并遵循 MVC 意识形态,这将如何工作?我很难理解如何使用以下概念来实现所需的 URL:

http://www.domain.com/controller/view/segment_a/segment_b/etc...

Let's pretend I'm trying to learn CI, and as my test project I am building a group-buying site.

What I'd like is to have a different page for each city, e.g.:

http://www.groupon.com/las-vegas/
http://www.groupon.com/orlando/

I'd also like to have different pages such as:

http://www.groupon.com/learn
http://www.groupon.com/contact-us

If I am building this in CI and following the MVC ideology, how would this work? I'm having difficulty seeing how to accomplish the desired URL's with the concept of:

http://www.domain.com/controller/view/segment_a/segment_b/etc...

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

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

发布评论

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

评论(3

望笑 2024-10-30 18:55:38

我要做的是创建一个自定义 404 控制器,充当不存在的路由的包罗万象的角色。

它将获取 URI,可能会验证它,并将其重新路由到(例如)“城市”控制器。

如果城市控制器找不到城市(无论指定什么字符串),那么它需要发出正确的 404。否则,您可以显示该城市的信息。

此外,创建自定义 404 控制器后,您可以通过指定名为“404_override”的路由向其发送所有 404 错误。

What I would do is create a custom 404 controller that acts as a catch-all for non-existent routes.

It would take the URI, possibly validate it, and re-route it to the (e.g.) "city" controller.

If the city controller can't find the city (whatever string was specified), then it needs to issue a proper 404. Otherwise, you're good to display your information for that city.

Also, once you create your custom 404 controller, you can send all 404 errors to it by specifying a route named '404_override'.

有木有妳兜一样 2024-10-30 18:55:38

这就是 URI 路由 发挥作用的地方。但就您而言,您可能必须仔细定义你的路线,因为你的路线的第一个也是唯一的部分已经是一个可变的部分。

That's where URI Routing comes in. But in your case you'll probably will have to be carefull defining your routes as the first and only part of your route is a variable part already.

怼怹恏 2024-10-30 18:55:38

这实际上与 MVC 无关,而与良好的 URL 有关。

您正在寻找这样的 URL:(a) 从用户的角度来看是清晰的,(b) 可以向您的应用程序提供有关如何处理它的提示。

在这种情况下,我会做的是稍微重新设计您的 URL,而不是:

http://www.groupon.com/las-vegas/
http://www.groupon.com/orlando/

您的 URL 看起来像这样:

http://www.groupon.com/destinations/las-vegas/
http://www.groupon.com/destinations/orlando/

开头的位 - /destinations/ - 可以由你的 URL 路由代码来决定哪个控制器应该处理它。如果您的路由代码是基于 URL 的,则可能有一个如下所示的数组:

$routes = array(
    '/destinations/' => 'on_destination_list',
    '/destinations/(.+)' => 'on_destination',
    '/(.*)' => 'on_page');

// Basic URI routing code based off of REQUEST_URI
foreach ($pattern => $func) {
    if (preg_match("`^$pattern

这实际上与 MVC 无关,而与良好的 URL 有关。

您正在寻找这样的 URL:(a) 从用户的角度来看是清晰的,(b) 可以向您的应用程序提供有关如何处理它的提示。

在这种情况下,我会做的是稍微重新设计您的 URL,而不是:

http://www.groupon.com/las-vegas/
http://www.groupon.com/orlando/

您的 URL 看起来像这样:

http://www.groupon.com/destinations/las-vegas/
http://www.groupon.com/destinations/orlando/

开头的位 - /destinations/ - 可以由你的 URL 路由代码来决定哪个控制器应该处理它。如果您的路由代码是基于 URL 的,则可能有一个如下所示的数组:

", $_SERVER['REQUEST_URI'], $placeholders)) { array_shift($placeholders); call_user_func($func, $placeholders); } }

请记住,该路由代码是我凭空写出来的,它可能并不绝对正确。它应该为您提供需要做什么的要点。

这样做还有一个额外的好处,即如果有人访问 http://www.groupon.com/destinations/,您将有机会显示目的地列表。

This really has nothing to do with MVC, and much more to do with good URL.

You're looking for URLs that are both (a) clear from the user's point of view and (b) that give hints to your application as to how it's meant to be handled.

What I'd do in this case is redesign your URLs slightly so that rather than:

http://www.groupon.com/las-vegas/
http://www.groupon.com/orlando/

You would have URLs that looks like this:

http://www.groupon.com/destinations/las-vegas/
http://www.groupon.com/destinations/orlando/

The bit at the beginning--/destinations/--can be used by your URL routing code to decide what controller should be dealing with it. If your routing code is URL-based, you might have an array like this:

$routes = array(
    '/destinations/' => 'on_destination_list',
    '/destinations/(.+)' => 'on_destination',
    '/(.*)' => 'on_page');

// Basic URI routing code based off of REQUEST_URI
foreach ($pattern => $func) {
    if (preg_match("`^$pattern

This really has nothing to do with MVC, and much more to do with good URL.

You're looking for URLs that are both (a) clear from the user's point of view and (b) that give hints to your application as to how it's meant to be handled.

What I'd do in this case is redesign your URLs slightly so that rather than:

http://www.groupon.com/las-vegas/
http://www.groupon.com/orlando/

You would have URLs that looks like this:

http://www.groupon.com/destinations/las-vegas/
http://www.groupon.com/destinations/orlando/

The bit at the beginning--/destinations/--can be used by your URL routing code to decide what controller should be dealing with it. If your routing code is URL-based, you might have an array like this:

", $_SERVER['REQUEST_URI'], $placeholders)) { array_shift($placeholders); call_user_func($func, $placeholders); } }

Keep in mind that I wrote that routing code off the top of my head and it may not be absolutely correct. It should give you the gist of what you need to do.

Doing things this way has the added benefit that if somebody goes to http://www.groupon.com/destinations/, you'll have the opportunity to show a list of destinations.

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