Folder Structure of a PHP MVC Framework... am I Doing This Right

Folder structure of a PHP MVC framework... am I doing this right?

One Solution for admin routing is what CakePHP does,
you first define a configuration for the admin string
and then in your controller use actions with a specific naming convertion

//Configuration ============================
Configure::write("admin_routing" , true );
Configure::write("admin_prefix" , "admin" );

//Controller ===============================
class MyController extends AppController{

function index(){
//Will map to /mycontroller/
}

function admin_index(){
//Will map to /admin/mycontroller/
}

}

You can generalize this by using a routing system
just look how your favorite framework does it

On another note

  1. The modules folder seems to be unecesary
  2. I agree with antpaw you should add a globals view and model folder in order to share them across applications
  3. I don't get why autoloader is inside the config directory and not as part of the lib directory, you can also just move the boostrap.php to the config directory

Hope this helps

PHP MVC folder structure / Model-Folder-Structure

I think this is subjective, but I will give you my way of doing it. I keep the models directory and then create different sub-directories for each module of the application. It would look something like the following:

application
- controllers
- models
- authentication
- services
- mappers
- ...
- mail
- services
- mappers
- ...
... (other directories)
- views
- templates

I feel this gives a nice separation of each module while keeping everything inside the models directory which other developers are properly used to.

I dont know if this is the best or most effective solution, but I think with proper use of namespaces it proves quite easy to manage. I have also learned that if you make proper use of the SOLID principle you can (almost) copy/paste the different directories/modules to other projects without much hastle.

A little series created by TutsPlus, which explains the SOLID principle in detail by using theory and a concrete example.

I hope this can help you the right direction.
Best regards.

Directory Structure for MVC

I would suggest to follow the Symfony 1.x directory structure. Clear, logical, secure.

Excerpt from book "The definitive guide to Symfony" by Fabien Potencier & François Zaninotto :

apps/
frontend/
backend/
cache/
config/
data/
sql/
doc/
lib/
model/
log/
plugins/
test/
bootstrap/
unit/
functional/
web/
css/
images/
js/
uploads/
  • apps/ Contains one directory for each application of the project (typically, frontend
    and backend for the front and back office).
  • cache/ Contains the cached version of the configuration, and (if you activate it) the
    cache version of the actions and templates of the project. The cache mechanism
    (detailed in Chapter 12) uses these files to speed up the answer to web requests.
    Each application will have a subdirectory here, containing preprocessed PHP
    and HTML files.
  • config/ Holds the general configuration of the project.
  • data/ Here, you can store the data files of the project, like a database schema, a SQL
    file that creates tables, or even a SQLite database file.
  • doc/ Stores the project documentation, including your own documents and the
    documentation generated by PHPdoc.
  • lib/ Dedicated to foreign classes or libraries. Here, you can add the code that needs
    to be shared among your applications. The model/ subdirectory stores the
    object model of the project (described in Chapter 8).
  • log/ Stores the applicable log files generated directly by symfony. It can also contain
    web server log files, database log files, or log files from any part of the project.
    Symfony creates one log file per application and per environment (log files are
    discussed in Chapter 16).
  • plugins/ Stores the plug-ins installed in the application (plug-ins are discussed in Chapter
    17).
  • test/ Contains unit and functional tests written in PHP and compatible with the
    symfony testing framework (discussed in Chapter 15). During the project setup,
    symfony automatically adds some stubs with a few basic tests.
  • web/ The root for the web server. The only files accessible from the Internet are the
    ones located in this directory.

PHP MVC - Is this the right structure?

It's kind of hard to define what is right or wrong at conceptual discussions such as this one. All I can provide you is my own limited experience and opinion. Absorb it with your own opinion and decides what's best for you in your own opinion. Build your first architecture, implement your first project and then your own project will start to tell you if your architecture is working for you or not. Here is a few personal tips.


  • I never output anything to the browser from any layer except the VIEW.

That is, if you're moving towards MVC model, try to keep the connection between the server and the response to a single layer, which seems to be a perfect job for the VIEW layer, that handles anything from and to what the user is seing (User Interface).


  • Learn addicional design pattern to complete your archtecture

In my opinion, MVC is not a fully working concept/archtecture. That is, it perfectly defines key responsabilities, but doesn't define how to make everything work. For instance, how will you define your model? You can go with Active Record (like your code represents) or you can opt out for DAO (Data Access Object). In a few words, the differences would be like this:

class ActiveRecordModel {
protected function connect();
protected function create();
protected function update();
protected function delete();
}

class Car extends ActiveRecordModel {
public $id;
public $maker;
public $model;
public $year;
}

This way, you can use $myCar->create(); and the model will be created inside the database. As oppose to DAO

class Car {
private $id;
private $maker;
private $model;
private $year;

// Getters, Setters, Constructor
}

class CarDAO {
public function add(Car $obj){
// Establish connection here or at the constructor
// Take data from $obj and arrange it to be stored.
}
}

  • Controllers talk to Controllers, but only one Model

I usually prefer making one controller for each model and that controller will know how to handle that model (regardless of which design pattern the model is using). That being said, I avoid using more than 1 model inside one controller. If you need to access data from a model that is not your own, talk to it's controller instead of bypassing the controller and going direct to the model. Here is what I mean:

class AccountController extends GenericController {
public function add($name, $phone, $birthday, $email, $password) {
$accountModel = new Account();
$accountModel->name = $name;
$accountModel->phone = $phone;
$accountModel->birthday = $birthday;
$accountModel->password = $password;
$id = $accountModel->insert();

$emailController = new EmailController();
$emailController->add($email, $id);
// Here, instead of declaring a model for "Email",
// I'm declaring the controller
// so, in my architecture, whenever I need to change some rule
// for some entity, I know I only have to work at one single controller.
}
}

What folder structure do you use

My Default Folder Structure for web projects is

+projectname/
+htdocs/
| +assets/
| | +js/
| | +css/
| | +img/
| | +swf/
| +content/
+sys/
| +temp/
| +config/
| +libs/
+docs

How much directory separation should my framework have?

I would suggest separating framework code from application code. The framework should be under one top-level directory and the application under a different one.

Actually... I suggest you look at the directory structure used by CakePHP.

Structure for a MVC PHP application

http://httpd.apache.org/docs/2.1/vhosts/examples.html

You can have a look there. Setting up a virtual host is always nice so you don't mix your application. And you just give access to your public folder.

<VirtualHost *:80>
DocumentRoot /var/www/mvc_app/public
ServerName www.example.com

<Directory /var/www/mvc_app/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>

</VirtualHost>

folder structure in php project

Even if you are not using Composer for auto-loading the PSR-4 directory structure is very good and if you use it and want to add in auto-loading later you can so woot. I would imagine most frameworks use this.

The general layout I use is:

app_name -> general dir
|
|____App -> where your app goes
| |___app_name -> name of application
| | |___ models -> these can be whatever you want
| | |___controllers
| |
| |____templates -> where I put my php/html/twig
| |
| |____css -> application specific
| | |
| | |____app.css
| |
| |____js -> application specific
| |
| |____app.js
|
|____Vendor
| |____php -> php libs
| |____js -> frontend css js libs
|
|____Tests
| |____modelTest
|
|____.git
|____composer.json
|____bower.json
|____index.php


Related Topics



Leave a reply



Submit