将每个请求发送到适当的控制器

发布于 2024-10-07 05:26:14 字数 396 浏览 5 评论 0原文

我想制作一个小型网络应用程序,但我不知道如何去做。 我想要做的是将每个请求发送到正确的控制器。 CodeIgniter 是如何做到这一点的。

因此,如果用户请求domain.com/video/details 我希望我的引导程序(索引?)文件将他发送到“视频”控制器类并调用该类中的“详细信息”方法。

如果用户请求domain.com/profile/edit,我想将他发送到“Profile”控制器类并调用该类中的“edit”方法。

有人可以解释我应该怎么做吗?我有一些使用 MVC 框架的经验,但自己从未用 MVC “编写”过一些东西。

谢谢!

编辑:我现在明白了 url 如何指向正确的控制器,但我没有看到控制器的对象实例是在哪里创建的,以调用正确的方法?

I want to make a small web application, and I'm not sure how to go about this.
What I want to do is send every request to the right controller. Kind of how CodeIgniter does it.

So if user asks for domain.com/video/details
I want my bootstrap (index?) file to send him to the "Video" controller class and call the "details" method in that class.

If the user asks for domain.com/profile/edit I want to send him to the "Profile" controller class and call the "edit" method in that class.

Can someone explain how I should do this? I have some experience using MVC frameworks, but have never "written" something with MVC myself.

Thanks!

Edit: I understand now how the url points to the right Controller, but I don't see where the object instance of the Controller is made, to call the right method?

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

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

发布评论

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

评论(3

分分钟 2024-10-14 05:26:14

您需要重新路由您的请求。使用 apache,可以使用 mod_rewrite 来完成此操作。

例如,将 .htaccess 文件添加到您的公共基目录并添加以下内容:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]

这将重定向尝试访问的用户

/个人资料/编辑

index.php?rt=个人资料/编辑

在index.php 中,您可以访问$_GET['rt'] 来确定请求了哪个控制器和操作。

You need to re-route your requests. Using apache, this can be done using mod_rewrite.

For example, add a .htaccess file to your public base directory and add the following:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]

This will redirect users trying to access

/profile/edit

to

index.php?rt=profile/edit

In index.php, you can access the $_GET['rt'] to determine which controller and action has been requested.

掌心的温暖 2024-10-14 05:26:14
  1. 使用 mod-rewrite (.htaccess) 来
    重写 url 来自
    www.example.com/taco 至
    www.example.com/index.php?taco/
  2. 在index.php中,获取第一个URL
    参数键,使用它来路由到
    正确的网址。正如它看起来的那样
    像“taco/”
  3. 你可能想在前面添加/
    如果不存在则返回。作为
    这将使网址标准化并且
    让生活更轻松
  4. 如果你想保留能力
    具有传统的查询字符串。
    直接检查 URL 并获取
    查询字符串部分。打破那个
    上?,这将为您留下
    key 0 和其余部分中的路由信息
    在键 1 中,将其拆分为 &,然后
    将其中的每一个拆分为 = 并设置
    第一个到钥匙,第二个到
    数组的值。替换 $_GET
    与该数组。

例子:

$path = explode("?", $_SERVER["QUERY_STRING"],2);
$url = $path[0];
if(substr($url,0,1) != "/")
{
    $url = "/".$url;
}
if(substr($url,-1,1) != "/")
{
    $url = $url."/";
}
unset($_GET);
foreach(explode("&", $path[1]) as $pair)
{
    $get = explode("=", $pair, 2);
    $_GET[$get[0]] = $get[1];
}

// Define the Query String Path
define("QS_PATH", $url);
  1. Use mod-rewrite (.htaccess) to
    rewrite the url from
    www.example.com/taco to
    www.example.com/index.php?taco/
  2. in index.php, grab the first URL
    parameter key, use that to route to
    the correct url. As it will look
    like "taco/"
  3. You may want to add / to the front
    and back if it doesn't exist. As
    this will normalize the urls and
    make life easier
  4. If you want to preserve the ability
    to have traditional query strings.
    Inspect the URL directly and take
    the query string portion. break that
    on ?, which will leave you with the
    routing info in key 0 and the rest
    in key 1. split that on &, then
    split each of those on = and set the
    first to the key and the second to
    the value of an array. Replace $_GET
    with that array.

Example:

$path = explode("?", $_SERVER["QUERY_STRING"],2);
$url = $path[0];
if(substr($url,0,1) != "/")
{
    $url = "/".$url;
}
if(substr($url,-1,1) != "/")
{
    $url = $url."/";
}
unset($_GET);
foreach(explode("&", $path[1]) as $pair)
{
    $get = explode("=", $pair, 2);
    $_GET[$get[0]] = $get[1];
}

// Define the Query String Path
define("QS_PATH", $url);
烟织青萝梦 2024-10-14 05:26:14

根据您想要做什么或想要做多少工作,与编写自己的 MVC 相比,另一个选择是使用 Zend Framework。它完全满足您的要求,甚至更多。您仍然需要像其他答案中提到的那样配置 URL 重写,但它快速且简单。

即使您对 Zend Framework 不感兴趣,以下链接也可以帮助您配置重写规则:

Depending on what you want to do or how much work you want to do, another option versus writing your own MVC is to use Zend Framework. It does exactly what you're asking for and more. You still need to configure URL rewriting as mentioned in the other answers, but it quick and easy.

Even if you're not interested in Zend Framework, the following link can help you configure your rewrite rules: http://framework.zend.com/wiki/display/ZFDEV/Configuring%2BYour%2BURL%2BRewriter

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