将每个请求发送到适当的控制器
我想制作一个小型网络应用程序,但我不知道如何去做。 我想要做的是将每个请求发送到正确的控制器。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要重新路由您的请求。使用 apache,可以使用 mod_rewrite 来完成此操作。
例如,将 .htaccess 文件添加到您的公共基目录并添加以下内容:
这将重定向尝试访问的用户
到
在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:
This will redirect users trying to access
to
In index.php, you can access the $_GET['rt'] to determine which controller and action has been requested.
重写 url 来自
www.example.com/taco 至
www.example.com/index.php?taco/
参数键,使用它来路由到
正确的网址。正如它看起来的那样
像“taco/”
如果不存在则返回。作为
这将使网址标准化并且
让生活更轻松
具有传统的查询字符串。
直接检查 URL 并获取
查询字符串部分。打破那个
上?,这将为您留下
key 0 和其余部分中的路由信息
在键 1 中,将其拆分为 &,然后
将其中的每一个拆分为 = 并设置
第一个到钥匙,第二个到
数组的值。替换 $_GET
与该数组。
例子:
rewrite the url from
www.example.com/taco to
www.example.com/index.php?taco/
parameter key, use that to route to
the correct url. As it will look
like "taco/"
and back if it doesn't exist. As
this will normalize the urls and
make life easier
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:
根据您想要做什么或想要做多少工作,与编写自己的 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