Directory structure in MVC PHP Frameworks
directory-structure, frameworks, model-view-controller, php
Solution
I'll try to cover all your questions.
The vendors folder that you see in many apps is from composer. In that folder are all the dependencies of your application/framework (often libraries from other people).
Composer allows you to pull in some good components for your framework so that you don't have to write everything from scratch. Say for example I want to use FastRoute because it is much better than any router I am capable of writing myself. So I just add the following to my `composer.json`, run `composer update` and I can use it in my framework.
{
"require": {
"nikic/fast-route": "dev-master"
}
}
You can also use composer to autoload your classes so that you don't have to write your own autoloader (and cache that for production).
I really dislike your current file names. adding .class to a filename makes absolutely no sense to me. The dots also just complicate Autoloading. I recommend you have a look at PSR-0 for some inspiration on how to handle file names/namespaces (This is supported by the composer autoloader).
For the tests you could create a `Tests` folder in the root folder of your project and in there recreate the directory structure of your project and add the test classes in the matching folders. You can also put your mock objects in there. This makes it easy to just run tests for a certain part of your application.
I also recommend that you move your application out of your public folder and instead have a `public` folder with only an `index.php` and your assets. That index.php does nothing other than requiring your front controller that bootstraps your app. Doing it that way adds another layer of security if for some reason your webserver stops processing PHP. Otherwise a visitor would be able to see your whole source code.
To get some inspiration for your folder structure questions, I recommend that you have a look at how other people solved this issue. Arya and PitchBlade would be a good starting point because they are way smaller than a framework like symfony.
I hope this helps. In the future I recommend that you split up your questions into different SO questions, that makes answering easier and you will get better answers because people who know the answer to one of your questions don't also have to answer all of them.
Problem
At this moment, my framework's directory structure looks like this: ``` framework/ libraries/ autoload/ autoload.class.php resource.namespaces.php router/ tests/ router.test.php router.class.php resource.routes.php configuration/ framework.configuration.php router.configuration.php controllers/ index.controller.php models/ index.model.php views/ default/ index/ index.view.php header.view.php footer.view.php assets/ css/ javascript/ images/ index.php ``` When my framework was smaller, it was a lot cleaner. I have give a look at other popular frameworks. They have two main folders: ``` framework/ app/ web/ ``` Actually, this structure is very clean and nice because we separate the front-end and the back-end. But I wonder what should I put in each of these. Logically, `libraries` folder should be inside the `app` folder, but it is not really a part of the application. For me, an application has `models`, `controller` and `views`. I think `libraries` should be put outside. Where should I put my libraries? I has a lot of `PHPUnit` tests in my `libraries`. Should I add a folder named `tests` inside each library, or should I put it inside/outside the `app` folder? If I want to implement a template feature, where I could choose which template I want to use for my website, how could I organise it? Every template has differents elements, so `header.view.php` will not be the same, etc. Now, I'm creating a folder in views which is the template folder. But I think it is a bad idea, 'cause now, for every template I must recreate all the views. In a lot of applications, there is a `vendor` folder that contains all the main classes. Is this the same as my `libraries` folder? Does it have the same role? I also have some 'resources' files (eg.: `resources.routes.php`). They are used to add some routes/namespaces. It's a bit like a configuration file. Should I create another direcotry for these files, or put them inside the class which they refers to?