php - MVC: Nested Views, and Controllers (for a website) -


I'm doing a PHP website using the MVC pattern. I am not using a framework because the site is quite simple and I think that this will give me a good opportunity to know about the pattern directly. I have some questions.

Question 1: How should I organize my thoughts? I'm thinking of keeping a page view that will have the header and footer, and allow content visualization to be nested between the two. Question 2: If I have 5 content pages, can I create 5 different views which can be used as nested material in the page view? Or, should I extend an abstract essence called Abstract content to them?

Question 3: What about the controllers? I think there should be at least one main controller. But then where does the request go from there? For the second controller? Or should I just call the page view and drop it on it? I thought the controllers wanted to handle input, possibly modify a model, and used to select a scene. But what if a nested call is required within a scene, then controller calls need to parse additional input?

Question 4: Is the controller allowed to pass parameters in this scene? Or should the controller only modify the model, which will then affect the scene? Or just model for DB access and other such things?

1 - It's a matter of preference. The easiest way would be to have a separate header and footer file. You can do something like this in your Page Controller

  $ title = "Page Title"; $ Var1 = 'var1'; $ Var2 = 'var2'; $ Var3 = array ("asdf", "adsfasdf", "234"); (Headers); // $ headers include header (DIR_VIEWS. 'Page.php'); // $ var1 / 2/3 in the page. Php included (footer); // Variable was created before the pages were included so that they were set in the template  

If you want to go nested root, then you have to start negligible with the Strelevest and That template engine is out of the scope for this answer.

2 - Creating objects of ideas is not needed A "view" can be a file on your filesystem that contains html for that scene. Like my example above, these pages can also have basic PHP / loop / echo variables.

3 - You are describing the front controller (sometimes called a dispatcher or router). This is actually the way to go, there are some ways to make front controller.

You have an array of URLs that indicate controllers.

  $ routes = array ('~ ^ / home / $ ~' = & gt; home.php ',' ~ ^ / contact / $ ~ '' = & gt; Php ',' ~ ^ / blog /.*?$ ~ '= & gt;' blog.php ');  

Or you can use the first "directory" in the URL as the controller name and you can load the file that creates your controller directory.

4 - The administrator has to get information from the model and give the data in view.


Edit for Comment


If you want a bunch of ideas for a sidebar, you can see that scene in another view For example, include:

  & lt; Div id = "content" & gt; & Lt; P & gt; Lareem Isthm Staff & lt; / P & gt; & Lt; / Div & gt; & Lt ;? Php included (DIR_VIEWS. 'Sidebar.php'); In the controller, just make sure you include some code for sidebar functions with "sidebar" pages with sidebar:  
  if ($ _GET ['keywords']) {$ Sidebar_search_results = get_search_results ($ _GET ['keyword']); } // This code should be in the file that contains you  

$ sidebar_search_results can be an array of results that parse and display your sidebar .


Comments