在面向对象的 PHP 中使用 MVC 时的控制器问题

发布于 2024-10-10 16:12:19 字数 347 浏览 2 评论 0原文

我目前正在使用面向对象 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 技术交流群。

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

发布评论

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

评论(3

夜空下最亮的亮点 2024-10-17 16:12:19

你应该明白 PHP 中的 MVC 模式通常是这样设置的:

你有 2 个关键变量:

  • class
  • method

他们总是默认为:

  • class = index
  • method = index

所以当参数 class传递到 URI 然后它会覆盖默认值,因此使用 index.php?class=view 上面的内容将变为:

  • class = view
  • method = index

您可以运行类 view.php 并初始化视图类到一个对象中,并像这样执行方法:

class View/*_Controller extends Controller*/
{
     public function index(){}
}

传递的任何其他变量都可以通过 GET 请求或 POST 获得。

您还应该注意,这通常是通过 URL 语法中的 URI 处理的,因此以下 index.php?action=view&method=download 变为 /view/download/

URI 内的任何内容(在类/方法之后)都将发送到该方法,示例如下。

/view/download/id/browser

这将执行以下方法并传入上下文中变量的值。

class View/*_Controller extends Controller*/
{
     public function download($id,$direction)
     {
     }
}

现在我知道这并不是针对您的问题,但我认为您处理问题的方式是错误的,这应该可以帮助您开始将框架布局为可管理的结构。


更新

使用路由器通过 GET/POST 初始化的控制器,路由器是一个类,它从 URI 中检测类/方法,然后找到正确的控制器并与其方法一起执行。

路由器的例子如下:

class Router
{
    //Params
    public $route;

    public static function CreateRoute($route = false)
    {
        return new Router($route);
    }

    public function __construct($route = false)
    {
         if($route !== false)
         {
              $this->route = $route;
         }else
         {
             $this->route = $_SERVER['REQUEST_URI'];
         }

         /*
             * The route would be parsed so that the following are matched
             * {/class}{/method}{/param1}{/param1}
             * /view/download/12/direct | as example
         */
    }


    public function run()
    {
        /*
            * Here you would do the following:
            * Validate the class to make sure no one is triggering an LFI
            * Make sure the class file exists within the directory
            * Include the class file and create an instance of it
            * Execute the method  sending in the params.
        */
    }
}

用法如下:

$Route = Router::CreateRoute(); //AutoDetect
$Route->run();

看一下下图,可能会帮助你理解。
Codeigniter 应用流程图

You should understand that The MVC Patter in PHP Is usually set out like so:

You have 2 key variables that are:

  • class
  • method

And they always default to:

  • class = index
  • method = index

So when the parameter class is passed into the URI then it overrides the default so using index.php?class=view the above becomes:

  • class = view
  • method = index

There for you run the class view.php and initialize the view class into an object, and execute the method like so:

class View/*_Controller extends Controller*/
{
     public function index(){}
}

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.

/view/download/id/browser

This will execute the following method and pass in the values for the variables in context.

class View/*_Controller extends Controller*/
{
     public function download($id,$direction)
     {
     }
}

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:

class Router
{
    //Params
    public $route;

    public static function CreateRoute($route = false)
    {
        return new Router($route);
    }

    public function __construct($route = false)
    {
         if($route !== false)
         {
              $this->route = $route;
         }else
         {
             $this->route = $_SERVER['REQUEST_URI'];
         }

         /*
             * The route would be parsed so that the following are matched
             * {/class}{/method}{/param1}{/param1}
             * /view/download/12/direct | as example
         */
    }


    public function run()
    {
        /*
            * Here you would do the following:
            * Validate the class to make sure no one is triggering an LFI
            * Make sure the class file exists within the directory
            * Include the class file and create an instance of it
            * Execute the method  sending in the params.
        */
    }
}

Usage like so:

$Route = Router::CreateRoute(); //AutoDetect
$Route->run();

Take a look at the following image, may help you understand.
Codeigniter Application Flow Chart

海夕 2024-10-17 16:12:19

好吧,在你的情况下,你实际上应该使用额外的参数进行重定向。例如,使用 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.

念﹏祤嫣 2024-10-17 16:12:19

使用此 HTML 完成的 index.php?action=search&q=WHATEVER 没有任何问题:

<form method="GET" action="index.php">
<input type="hidden" name="action" value="search" />
Search: <input type="text" name="q" />
<!-- ... -->

这大约是搜索引擎的做法,只不过它们使用 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:

<form method="GET" action="index.php">
<input type="hidden" name="action" value="search" />
Search: <input type="text" name="q" />
<!-- ... -->

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.

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