0

I read much about PHP MVC pattern yesterday and I wanna get in touch with it. I never did that I always had one class for a whole app, but I want a better structure and get better with OOP.

I want to use twig as template engine, because its easy to use and no other installation required, I already started by creating my folder structure in netbeans, it looks currently like this:

 ## Source Tree 
 .
 |── controllers
 |   |── feedreader.php
 |
 |── CSS
 |── images
 |── js
 |
 |── models
 |   |── database.php
 |
 |── vendor
 |   |── twig
 |   |── twig
 |   |── ...
 |   |── autoload.php
 |── views
 |   |── index.html
 |
 |── composer.json
 |── composer.lock
 |── index.php

Currently this is my structure where I want to start from.

I am going to create my model ( which only does mysql queries ) db.php. Then I will start to work in the controller.php and programm the logic of my APP for example a function that checks if a user already exists in the database. But then there is the template engine.

How do I use it? Will I check on the index.php what site is requested by the visitor and then load the template? And how dos this work with the controller to compute the request?

You see there are many questions.

Can you help me? I want to learn to code with MVC and a template engine. Do you know any good examples or posts where I can learn?

regards & thanks

raviolican
  • 362
  • 4
  • 14
  • Have a look at how to use a router or "front controller": http://stackoverflow.com/questions/32389438/front-controller-pattern-is-router-a-front-controller – 1sloc Jul 10 '16 at 11:33
  • May I suggest the [Laravel framework](https://laravel.com) - it'll better serve those needs. You'll get **blade** instead of **twig** which IMHO works quite well. They've also got a great, and quick - [getting started tutorial](https://laravel.com/docs/5.2/quickstart). –  Jul 10 '16 at 11:36
  • @JethroVanThuyne thanks, oh did know that. So basically a router routes the request to the right controller i see, thats a point what makes it easier. So I can have an userControler.php which handels registrations, logins and settings; and a controller which handles the functionallity of the app, eg. if I have an app that fetches feeds a feedController.php right? Omg that looks so complicated for me right now :/ – raviolican Jul 10 '16 at 11:48
  • @iamjoshuamabina thanks,i heard much about laravel, the quick start looks really great, but i think i want to learn the MVC concept without a framework at first, im not sure. – raviolican Jul 10 '16 at 11:50
  • @raviolican correct! And you don't need a framework for that. Just make sure that your request gets analyzed by a function that parses the `$_SERVER['REQUEST_URI']` and decides which controller to call. – 1sloc Jul 10 '16 at 11:55
  • 1
    @JethroVanThuyne thanks i understand better now, that sounds like that this is the way for real OOP. Because you can create an Object for things you can do on a page. Something i don't understand is, when I have to fetch feed by the feedsControler.php i will also need informations from the userController.php, like the username, feeds to load and some settings. How do I design this? – raviolican Jul 10 '16 at 12:37
  • Try to make you controllers quite dumb, and put your logic in "models". That way, in your FeedController you can address FeedModel for the feed logic, and if necessary address a UserModel to fetch some data about a user. You can either `require` these models in your controller, or use a PSR-4 autoloader. – 1sloc Jul 10 '16 at 12:39
  • Hey there. That's a nice concern you have about learning better practices, but your question as it stands is *really* hard to answer, and will probably be closed by the users of the site. That being said, maybe you should start your mvc quest by furthering your understanding one letter at a time, eg what is the Model. How should it be structured? Before even learning a specific framework. – Félix Adriyel Gagnon-Grenier Jul 15 '16 at 22:12
  • @FélixGagnon-Grenier Hey Thanks for your answer, I have already started my project and its finished soon. I did another post in code review when you mind – raviolican Jul 16 '16 at 08:15

0 Answers0