-1

Hello I am following a tutorial on sitepoint about MVC (Model View Controller). However the instructions are not 100% clear in that tutorial, and the comment section has been closed.

On this tutorial https://www.sitepoint.com/the-mvc-pattern-and-php-1/ it says to setup relationships betwee model, view and contoller. It provides the code but does not state where this code should go or in what file it should go. First they tell us make model using code on tutorial, than make view file, than make controller file all codes for files they provided in tutorial. Then they say use following code to make relationships, but they do not state where to place this code.

<?php
$model = new Model();
$controller = new Controller($model);
$view = new View($controller, $model);
echo $view->output();

I would appreciate any assistance you can provide or advise me on as to where this particular code goes for setting up the relationships please.

I sincerely apologise if this is not the right place to ask. I seem to get banned from posting questions for a period of 3 days when I ask questions, and I am not sure as to why that reason could be.

So accept my apologise if this is another one of those posts.

Thanks in advance.

  • It seems like what you need to do is learning how to use an MVC framework (Laravel, CodeIgniter, etc.) vs just learning the principals of the MVC pattern. This tutorial is simply theoretical to explain the concept. Try Laravel it is a great PHP framework with great tutorials and video tutorials (https://laracasts.com/series/laravel-5-fundamentals/) – dev7 Jun 03 '17 at 23:04
  • Usually you would put the different code in different files and use `include()` to link them together. For this example, you can put all the code in one file. – RST Jun 03 '17 at 23:07
  • Maybe it isn't clear because there are different ways in which you can put MVC in your code. I would say no frame looks the same. Im a student myself, currently building a very basic framework containing MVC, and it is far from the quality of open scource frameworks. no wonder having a whole team working on it for years.. I think you need to figure out what the point and advantages of using MVC are. If you can see the big picture, you will be more likely to understand how to put this into your project, but you will need control over your code first, separating it in different steps/objects. – Maarten Kuijper Jun 03 '17 at 23:15
  • Hi thanks for response, I have been trying to learn how to do simple MVC tutorial using php, as I am doing a web course and part of our assignment is to turn our static website into a MVC, but the only tutorials and resources the tutor has provided is link to that tutorial on sitepoint and a simple books MVC example, which I think he found online as I came across it last night. I have spent the past 4 days trying to learn how to implement a basic 3 page website into the mvc but most of the tutorials are quite different or usually based off frameworks. – JediMaster Orion Jun 03 '17 at 23:16
  • A good start would be to separate 3 elements in your code, your 'views or html', your logic, and your database communications. Later on you will find more efficient ways to do this.. For example you have different pages, which all should get their own controller and model. Maybe you can create a router which reads your URL, now you know wich controller and model to use. Each have code to get the data to be shown on that specific page, and send it to the view. – Maarten Kuijper Jun 03 '17 at 23:21
  • 12
    @Yani Laravel is implementing MVC in the same way as *Democratic People's Republic of Korea* is implementing democracy. – tereško Jun 07 '17 at 08:44

1 Answers1

4

In general, that code would go in your bootstrap file. Right after the setup of DIC and the execution or the router.

As for real-world code, the structure you see in that tutorial, it a very naive example. There wouldn't be a magical "model", that your views and controllers depend upon, but various services. And the choice of commands, that are executed on the chosen controller, would be governed by the routing result.

In practice, that code would look closer to:

// routing is used to get $command and $resource
// where $command = $request->getMethod() . $parameters['action'];

$controller = $container->get("controllers.$resource");
if (method_exists($controller, $command)) {
    $controller->{$command}($request);
}

$view = $container->get("views.$resource");
if (method_exists($view, $command)) {
    $response = $view->{$command}($request);
    $response->send();
}
tereško
  • 58,060
  • 25
  • 98
  • 150
  • I don't have a bootstrap file. I just have Model, View and Controller and the index file. – JediMaster Orion Jun 08 '17 at 08:32
  • @JediMasterOrion that's probably something you should fix. You should not keep your app's logic and config in `DOCUMENT_ROOT`, because the php interpreter can fail to load. In such case, all of your code becomes readable to the visitor. The recommended practice is for `index.php` file to only contain something like `require __DIR__ . '/../app/bootstrap.php';`, which passes the execution to a file, that is outside of webserver's direct reach. – tereško Jun 08 '17 at 08:42
  • As for the "MVC" part, you might find [this post](https://stackoverflow.com/a/5864000/727208) useful (even thought it's a bit outdated ... I should clean it up this month). – tereško Jun 08 '17 at 08:44
  • I have this code only in index.php invoke(); ?> – JediMaster Orion Jun 08 '17 at 08:44
  • only index.php I have in document root. The model, view and controller files are in their own folders located inside the root directory – JediMaster Orion Jun 08 '17 at 08:46
  • And if `mod_php` fails to load, every visitor will be able to read the code in `controller/controller.php` file, since it is inside `DOCUMENT_ROOT` – tereško Jun 08 '17 at 08:52
  • Thanks a lot for your information. Really appreciate it. Are you able to help answer this new question I have regarding MVC thanks. https://stackoverflow.com/questions/44430455/how-to-change-view-for-certain-page-in-php-mvc Thanks again for assistance. – JediMaster Orion Jun 08 '17 at 08:57