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

后端 未结 3 1785
耶瑟儿~
耶瑟儿~ 2020-12-08 12:21

I\'m currently working on my own PHP Framework, and I need some help figuring out if I\'m going in the right direction or not...

The framework is both for my own use

相关标签:
3条回答
  • 2020-12-08 12:59

    i would suggest you to use a bootstrap.php that manages all the routings so you never run into issues like "i wish i could nest one folder more into my admin module".

    i also wouldnt use modules and keep the default controllers right inside the controller/ dir and the admin controllers inside the controller/admin dir. same for models and views.

    btw its really not clever not to share the models between different parts of your application, they are going to be the same in 99% of all cases. thats why mvc is so powerful. sometimes you even can share some of the view parts inside your app between the front- and backend.

    0 讨论(0)
  • 2020-12-08 13:01

    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

    0 讨论(0)
  • 2020-12-08 13:04

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

    The asker's proposed solution is basically what I went with, oddly enough. Basically, I borrowed a concept from ASP.NET MVC2 called "areas". Areas are sections of the site that have their own controllers and view (also models, but I don't know why ... models should generally be universal). So this is very similar to your initial idea.

    In any case following their folder+routing structure made quite a lot of sense for my application (admin type area, a users area, and another level in between). Take a look at it and you might find some success there.

    My routing just takes into account areas. The routes are hard coded, so if I need another area I just adjust my routing file. Oh also, my autoloaders are set to look in the area folder if $area is specified.

    /admin/team/add/ is read as, Area: Admin, Controller: team, Action: add

    whereas

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

    Folder structure kinda like this:

    app/
       areas/
          admin/
              controllers/
              views/
          staff/
              controllers/
              views/
       controllers/
       models/
       views/
    
    0 讨论(0)
提交回复
热议问题