Joshinki Posted November 25, 2016 Share Posted November 25, 2016 Hi, I am still struggling to get my head around MVC. I have drawn something up in powerpoint just to see if my thinking is correct? If you could let me know if this is correct or am I on the right track? Thanks Josh Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/ Share on other sites More sharing options...
Diversity Posted November 25, 2016 Share Posted November 25, 2016 Which MVC you're using ? this looks like something custom and it's bit complicated if i can say Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1539678 Share on other sites More sharing options...
ignace Posted November 25, 2016 Share Posted November 25, 2016 (edited) It's kinda correct. Your main controller actually should call the gallery controller, not some view. Inside the controller you would load the view. Something like: <?php $page = !empty($_GET['page']) ? $_GET['page'] : null; switch ($page) { case 'gallery': $controller = new GalleryController(); break; default: $controller = new HomeController(); break; } // do something with $controller Edited November 25, 2016 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1539682 Share on other sites More sharing options...
Joshinki Posted November 25, 2016 Author Share Posted November 25, 2016 Thankyou for all your replies. @Diversity Yes this is something custom, I wanted to get my head around this before moving onto a popular framework. @ignace Thanks for that, So what your saying is load the Gallery Controller, and in the gallery controller -> gallery model -> this would then output the gallery views? How is my template incorporated into this? ie - I want my header/footer to be loaded, and then gallery images to be loaded in the body. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1539684 Share on other sites More sharing options...
ignace Posted November 28, 2016 Share Posted November 28, 2016 (edited) Look, in short, this is how it works: MVC is a concept divided in three keywords, but each keyword covers a LOT in code. Your Model does all the work. Now the Model is not ONE thing, it's everything that is associated with your business logic. Queries, DAO's, Services, Repositories, Factories, Entities, ... Your View only handles presentation. The View is ALSO not ONE thing, it's everything that handles presentation. By presentation I mean in the case of a web application is HTML. Your Controller has actually no real task, but is required to handle a request, since neither the Model nor View are able (nor should they) to do this. Your header and footer are part of the View and this should be handled in the View. A popular approach is the '2-step View' to this problem: http://martinfowler.com/eaaCatalog/twoStepView.html You can of course use a simpler method if needed, for example: views/gallery/index.php <?php require APP_VIEW_DIR . '/header.php'; ?> <!-- Your content here --> <?php require APP_VIEW_DIR . 'footer.php'; ?> When people first encounter MVC they imagine something very complicated while it's actually really simple: models/gallery/getAll.php <?php $db = require APP_MODEL_DIR . '/db.php'; $stmt = $db->prepare('SELECT * FROM gallery'); return $stmt->fetchAll(); controllers/gallery/index.php <?php $images = require APP_MODEL_DIR . '/gallery/getAll.php'; require APP_VIEW_DIR . '/gallery/index.php'; To make this work you need a "Front Controller", what this means is a Controller that is called first. In Java this is the project class with the main() method. For web applications this is whatever file you tell nginx/apache to load as default, usually index.php <?php define('APP_CONTROLLER_DIR', __DIR__ . '/controllers'); define('APP_MODEL_DIR', __DIR__ . '/models'); define('APP_VIEW_DIR', __DIR__ . '/views'); define('APP_CONFIG_DIR', __DIR__ . '/config'); require APP_MODEL_DIR . '/functions/request.php'; $page = get('page', 'home'); switch ($page) { case 'gallery': require APP_CONTROLLER_DIR . '/gallery/index.php'; break; default: case 'home': require APP_CONTROLLER_DIR . '/home/index.php'; break; } Try to wrap your head around this. You'll see my model is only concerned with model stuff, the view only with view stuff, and the controller just puts the pieces together. This is also true in every framework though it may seem a lot more complicated there. Edited November 28, 2016 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1539793 Share on other sites More sharing options...
hansford Posted November 29, 2016 Share Posted November 29, 2016 I wrote a custom one when I was trying to wrap my head around it. This is what my index.php page looks like. <?php require($_SERVER['DOCUMENT_ROOT'] . '/config.php'); require_once(ROOT . '/common.php'); // set the path separator define('PS','/'); require_once(APP . '/common.php'); // load system files require_once(SYS . 'Model.php'); require_once(SYS . 'Load.php'); require_once(SYS . 'View.php'); require_once(SYS . 'Controller.php'); require_once(SYS . 'Router.php'); // load the database require(DB); // load routes configuration require(APP . '/config' . PS . 'routes.php'); // launch application $router = new Router($routes); $router->init(); 1 Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1539821 Share on other sites More sharing options...
Stefany93 Posted November 29, 2016 Share Posted November 29, 2016 I wrote a custom one when I was trying to wrap my head around it. This is what my index.php page looks like. <?php require($_SERVER['DOCUMENT_ROOT'] . '/config.php'); require_once(ROOT . '/common.php'); // set the path separator define('PS','/'); require_once(APP . '/common.php'); // load system files require_once(SYS . 'Model.php'); require_once(SYS . 'Load.php'); require_once(SYS . 'View.php'); require_once(SYS . 'Controller.php'); require_once(SYS . 'Router.php'); // load the database require(DB); // load routes configuration require(APP . '/config' . PS . 'routes.php'); // launch application $router = new Router($routes); $router->init(); Kuddos for writing a custom one to understand it. 99% of the programmers would have used a shitty ready made system to understand MVC and would have been more confused at the end. You can think of a controller as a way to have all your pages generated from index.php file while having them in a physical state in the /views folder. It is a confusing concept and everyone have a different opinion about MVC. You dont need MVC to write good code. Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1539848 Share on other sites More sharing options...
Joshinki Posted November 30, 2016 Author Share Posted November 30, 2016 Thank you so much for that response, very helpful! Just so I understand it visually tho, is this correct the way I have drawn it up? Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1539878 Share on other sites More sharing options...
Joshinki Posted November 30, 2016 Author Share Posted November 30, 2016 Also, where you have: $images = require APP_MODEL_DIR . '/gallery/getAll.php'; Could I put this into a model file? and use like: controller/gallery/index.php <?php require_once 'models/gallery/model.php'; $model = new Model; $images = $model->fetchImages(); require_once 'views/gallery/index.php'; ?> models/gallery/model.php <?php class Model { private $pdo; function __construct() { $this->pdo = Database::getInstance(); } function fetchImages() { $st = $this->pdo->prepare("SELECT * FROM GALLERY"); $st->execute(); return $st->fetchAll(); } } ?> And then my view/gallery/index.php will be as above on image? Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1539882 Share on other sites More sharing options...
ignace Posted November 30, 2016 Share Posted November 30, 2016 (edited) I do not encourage building a full production site from my example code merely use it to understand MVC. Once you do, it is recommended to use a framework to learn the finer points of programming and modern programming techniques since MVC is only a small part of building a web application. Other terms you will cross are Object-Oriented Programming, Dependency Injection, Object-Relational Mapping, Design Patterns (Factory, Repository, ..), .. To come back at your example: class Model { private $pdo; function __construct() { $this->pdo = Database::getInstance(); } function fetchImages() { $st = $this->pdo->prepare("SELECT * FROM GALLERY"); $st->execute(); return $st->fetchAll(); } } You will NEVER have a class called Model. Programming is all about assigning responsibility and proper naming. Naming is actually the hardest part of the job. So the responsibility of your "Model" is to: - return a list of gallery images - add / delete a gallery image So let's settle for now on GalleryManager: interface GalleryManager { public function addImage(GalleryImage $image); public function removeImage(GalleryImage $image); public function getAll(); } At some point we may want to use something different than PDO (Doctrine, Eloquent, ..) to query for our images so we define our manager as an interface. Now for our PDO implementation: class PdoGalleryManager implements GalleryManager { private $pdo; public function __construct(PDO $pdo) { $this->pdo = $pdo; } public function getAll() { // your code here } public function addImage(GalleryImage $image) { // your code here } public function removeImage(GalleryImage $image) { // your code here } } We also pass PDO through the constructor and not use a Singleton to fetch it to make it easier to test. Edited November 30, 2016 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1539885 Share on other sites More sharing options...
Joshinki Posted November 30, 2016 Author Share Posted November 30, 2016 I do not encourage building a full production site from my example code merely use it to understand MVC. Once you do, it is recommended to use a framework to learn the finer points of programming and modern programming techniques since MVC is only a small part of building a web application. Other terms you will cross are Dependency Injection, Object-Relational Mapping, Design Patterns (Factory, Repository, ..), .. Hi Ignace, Yes I understand that, but it my logic in thinking correct with the image above? I was looking into this design pattern just to help update my future projects easier. Why use a framework over just coding it from scratch as above? Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1539886 Share on other sites More sharing options...
hansford Posted December 6, 2016 Share Posted December 6, 2016 Why use a framework over just coding it from scratch as above? For one thing...modern frameworks are constantly being updated and improved. Another thing is that is what companies want when looking to hire a developer. You should understand the MVC design pattern and how each part works, but then go learn a modern framework. Quote Link to comment https://forums.phpfreaks.com/topic/302615-mvc/#findComment-1540078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.