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

别来无恙 提交于 2019-11-28 06:06:25

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

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/

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!