在面向对象的 PHP 中使用 MVC 时的控制器问题
我目前正在使用面向对象 PHP 中的 MVC 对书店进行编程,并且我很难确定控制器在处理请求时应如何工作,尤其是在处理表单时。
例如,我有一个搜索表单,当用户访问“index.php?action=search”时显示,但是我不确定应该如何处理表单中的搜索字符串,因为我无法发送“$_GET['action” '] = search”再次将“index.php?action?search=searchstring”发送到浏览器,以便在不使用隐藏字段发送搜索操作的情况下显示搜索结果,这当然是非常不安全的!
我觉得这对于它的价值来说是太多的努力,到目前为止,程序似乎是尝试这个的更好方法!除非你能说服我!
谢谢丹
。
I am currently programming a book store using MVC in Object Orientated PHP and I am having a hard time working out how the controller should work when dealing with requests, especially when dealing with forms.
For example, I have a search form that is shown when the user visits "index.php?action=search" however I am unsure how i should deal with the search string from the form as i cannot send the "$_GET['action'] = search" again so that "index.php?action?search=searchstring" is sent to the browser so that the search results are shown without using a hidden field to send the search action which is of course very unsecure!!
I feel like this is all too much effort for what its worth and proceedural seems a better way of attempting this so far!! Unless you can convince me otherwise!!
Thanks
Dan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你应该明白 PHP 中的 MVC 模式通常是这样设置的:
你有 2 个关键变量:
他们总是默认为:
所以当参数
class
传递到 URI 然后它会覆盖默认值,因此使用index.php?class=view
上面的内容将变为:您可以运行类 view.php 并初始化视图类到一个对象中,并像这样执行方法:
传递的任何其他变量都可以通过 GET 请求或 POST 获得。
您还应该注意,这通常是通过 URL 语法中的 URI 处理的,因此以下
index.php?action=view&method=download
变为/view/download/
URI 内的任何内容(在类/方法之后)都将发送到该方法,示例如下。
这将执行以下方法并传入上下文中变量的值。
现在我知道这并不是针对您的问题,但我认为您处理问题的方式是错误的,这应该可以帮助您开始将框架布局为可管理的结构。
更新
使用路由器通过 GET/POST 初始化的控制器,路由器是一个类,它从 URI 中检测类/方法,然后找到正确的控制器并与其方法一起执行。
路由器的例子如下:
用法如下:
看一下下图,可能会帮助你理解。
You should understand that The MVC Patter in PHP Is usually set out like so:
You have 2 key variables that are:
And they always default to:
So when the parameter
class
is passed into the URI then it overrides the default so usingindex.php?class=view
the above becomes:There for you run the class view.php and initialize the view class into an object, and execute the method like so:
Any other variables passed would be available via the GET Request, or POST.
You should also note that this is usually handled via the URI within the URL Syntax, so the following
index.php?action=view&method=download
becomes/view/download/
Anything within the URI, after the class/method would be sent to the method, example below.
This will execute the following method and pass in the values for the variables in context.
Now I know that this is not specific to your question but i think your going the wrong way about things and this should help you start laying your framework out into a manageable structure.
Update
The Controller initialized by the GET/POST using a router, a router is a class that detects the class / method from the URI and then finds the correct controller and executes it along with its method.
An example of a router is like so:
Usage like so:
Take a look at the following image, may help you understand.
好吧,在你的情况下,你实际上应该使用额外的参数进行重定向。例如,使用
index.php?action=search&q=searchstring
。这将保留仅根据操作参数调用正确“控制器”的能力。至于“MVC”部分,你是对的。它不太适用网络应用程序。但是您可以阅读 Model-View-Presenter,它更类似于每个人都在做的事情。
Well, in your case you should actually redirect with an extra parameter. Use
index.php?action=search&q=searchstring
for example. That would retain the ability to invoke the right "controller" solely based on the action param.As for the "MVC" part you are right. It's not overly applicable to web apps. But you could read up on Model-View-Presenter, which better resembles what everyone is doing anyway.
使用此 HTML 完成的
index.php?action=search&q=WHATEVER
没有任何问题:这大约是搜索引擎的做法,只不过它们使用 URL 重写来使 URL 更漂亮。例如,Bing 搜索“test”为 http://www.bing.com/search? q=测试。
当您第一次学习 MVC 时,它可能看起来很麻烦,但它确实是一种高效且方便的 Web 开发模式,远远优于过程式“一切皆有可能”的 Web 开发。
There is nothing wrong with
index.php?action=search&q=WHATEVER
, accomplished with this HTML:This is approximately how search engines do it, except that they use URL rewriting to make the URLs prettier. For example, a Bing search for "test" is http://www.bing.com/search?q=test.
MVC may seem like a hassle when you are first learning it, but it really is a productive and convenient pattern for web development that is far superior to procedural-style "anything goes" web development.