PHP MVC structure folder structure ... Am I doing this right?

I am currently working on my own PHP Framework and I need help figuring out if I will go in the right direction or not ...

A framework for both my own use and for further promoting my PHP skills. I encountered many problems that, overcoming them, I learned a lot and love to create something out of nothing, so I would rather not see answers like "Just use Zend"!;)

I read a bunch of articles on both Stack Overflow and a bunch of other sites, but I canโ€™t get the right answer that I need, so I hope someone can give me some good advice!

I tried several different solutions, but I just embarrassed myself, and I'm not sure which direction to go now! I can't completely get around all this ...

"Theoretical" structure structure

- .htaccess - index.php - private/ - app/ - bootstrap.php - modules/ - default/ - controllers/ - pages.php - index.php - models/ - views/ - admin/ - controllers/ - models/ - views/ - config/ - config.php - autoloader.php - lib/ - Some_Library - Class1 - class1.php - Class2 - class2.php - public/ - css - images - scripts 

More details

  • index.php is the main file where each request is redirected using .htaccess.
  • private / unavailable in public, obviously.
  • public / contains all public files.
  • application / contains all application code.
  • lib / may contain Zend or another library (I also work independently), for calling with autoloaders
  • bootstrap.php is the application code ... Do I need it? is the main "index.php"? .
  • modules / will contain each module ... Do I need modules? .
  • default / is the default module that will contain MVC for most requests (used when "admin" is not the first part of the URL).
  • admin / is the module that will contain the MVC for the admin section.

Anyway, to my question ...

I thought it was better to separate the admin section from the rest of the website, but where I am stuck. I made the above structure to work with it, but I'm not sure if this is the most efficient way.

If site.com/videos/view/1/ .. hits the site.

Module : default Controller : video Action : view Params : array ('1')

and if the request site.com/admin/pages/view/1/ comes to my site ..

Module : Admin Controller : Pages Action : Browse Parameters : Array ('1')

Is this the right way? Or am I complicating this too much and doing something that is not worth doing?

Should I have a completely separate application structure for my admin section ...? Do I even need to separate the MVC partition from the rest of everything?

Sorry for the massive question, I just wanted to give you as much information as possible! Feel free to answer any part you can = P

+7
php module model-view-controller directory-structure
source share
3 answers

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

 //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 summarize this using a routing system; just see how your favorite framework works.

In another note

  • The modules folder seems unregistered
  • I agree with antpaw, you must add a global view and model folder in order to exchange them between applications.
  • I donโ€™t understand why the autoloader is inside the config directory, and not as part of the lib directory, you can also just move the boostrap.php file to the configuration directory

Hope this helps

+2
source share

I know this was asked a long time ago, but I was in the same situation a few days ago.

The solution proposed by the researcher is mainly related to what I went with, oddly enough. Basically, I borrowed a concept from ASP.NET MVC2 called the "realm". Areas are areas of a site that have their own controllers and browsing (also models, but I donโ€™t know why ... models usually have to be universal). So this is very similar to your original idea.

In any case, following their folder + routing structure, there was quite a lot of sense for my application (administrator type area, user area and another level between them). Take a look at it, and you can find some success there.

My routing just takes into account areas. Routes are hardcoded, so if I need another area, I just set up my routing file. Oh, also, my autoloaders are configured to look in the area folder if $ area is specified.

/admin/team/add/ reads as, Area: Admin, Controller: team, Action: add

then

/team/add/ will read as, Area: [none], Controller: team, Action: add

The folder structure is as follows:

 app/ areas/ admin/ controllers/ views/ staff/ controllers/ views/ controllers/ models/ views/ 
+2
source share

I suggest that you use bootstrap.php, which manages all the routes, so you never encounter problems such as "I'm sorry that I could no longer put one folder in my administration module."

i will also not use modules and keep the default controllers directly in the / dir controller and administrator controllers inside the controller / admin directory. for models and views.

btw is really not smart not to share models between different parts of your application, they will be the same in 99% of all cases. that is why mvc is so powerful. sometimes you can even share some parts of the view inside your application between the front and backend.

+1
source share

All Articles