Confused about the flow of MVC, PHP
I have been trying to learn about MVC but i have a few questions about the flow of input to the controller and then to the model.
- Say a user goes to example.com and get to their home page. They then select the search.php link. How does the controller.php know that the user has requested the search.php instead of user.php?
When the controller.php knows the search.php has been selected it will load the model and then the view.php. But when calling these the code will look like this.
class Search extends Core_Search_Controller public function inboxSearch(){ $this->view->navigation = $this->navigation(); $this->load->box = $this->box(); }
There is no folder or class view and no folder for load or class for load. And I can find function navigation in a different file but its folder is different location. How can it access that file without include or require?
- Once of search.php How does the controller.php know that search.php has requested information? Maybe this is redundant from question number one but im quite confused on this.
I know its long, sorry about that.
*Edit:*From what i have learned from the project i am on is that all the functions in the controller have Action on the end of them will direct to a view with the corresponding name. such as index.php/.tpl
class IndexController extends Zend_Controller_Action{
public function indexAction(){
/**
开发者_StackOverflow社区 Somecode
**/
}
}
Cheers
Most MVC frameworks do a lot of behind the scenes magic for you and this is probably what has you confused about how things work.
To answer your first question, most frameworks use a .htaccess file with a rewrite rule that will redirect all traffic to your controller. So, when you request search.php, it will actually call the controller and not search.php. From there, the controller can look at what you originally requested (search.php in this case) to figure out the appropriate model and view.
I believe the answer to your second question is that it is auto-loading the files as needed. This is another bit of magic, where it can look at the class name and figure out the location of the file and load it. You can read more about this in the PHP manual. http://php.net/manual/en/language.oop5.autoload.php
To answer your last question, getting the requested information is often handled by the controller when it looks at the request. For example, if you request "example.com/blog/7263", it will figure out that you want the "blog" model and that the id number is 7263. Depending on what framework you are using, the way that you configure this will be different.
I hope that helps a little.
精彩评论