开发者

Controller Problem when using MVC in Object Orientated PHP

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开发者_StackOverflow中文版 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.


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.

Controller Problem when using MVC in Object Orientated PHP


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜