Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

model view controller - Folder structure of a PHP MVC framework... am I doing this right?

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 and to generally advance my PHP skills further. I've encountered numerous problems that by overcoming them I have learned a great deal, and love being able to create something from nothing, so I'd rather not see answers like "Just use Zend"! ;)

I have read a bunch of articles both on Stack Overflow and a bunch of other sites, but can't quite get the right answer I need, so hopefully someone can give me some helpful advice!

I've tried a few different solutions, but I've just ended up confusing myself and I'm not sure which direction to go now! Can't quite get my head around it all...

'Theoretical' framework 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

Details

  • index.php is the main file, where every request is routed to with .htaccess.
  • private/ can't be accessed publicly, obviously.
  • public/ contains all the public files.
  • app/ contains all app-specific code.
  • lib/ could contain Zend or another library (I'm also working on my own), to be called with autoloaders
  • bootstrap.php is the app-specific code... Do I need this? is the main 'index.php' enough?.
  • modules/ would contain each module... Do I need modules at all?.
  • default/ is the default module that will contain the MVC's for most requests (used when 'admin' isn't the first part of the URL).
  • admin/ is the module that will contain the MVC's for the admin section.

Anyways, to my question...

I thought it would be better to separate the admin section from the rest of the website, but that's where I'm getting stuck. I have made the above structure to work with it, but I'm not sure if this is the most effective way.

If a request site.com/videos/view/1/ comes to my site..

Module: Default Controller: Videos Action: View Params: array( '1' )

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

Module: Admin Controller: Pages Action: View Params: array( '1' )

Is this the right way to go about this? Or am I over-complicating it and doing something that's not worth doing?

Should I have a completely separate application framework for my admin section...? Do I even need to separate the admin section's MVC's from the rest of it all?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...