沃梦达 / 编程问答 / php问题 / 正文

PHP MVC 框架的文件夹结构......我这样做对吗?

Folder structure of a PHP MVC framework... am I doing this right?(PHP MVC 框架的文件夹结构......我这样做对吗?)

本文介绍了PHP MVC 框架的文件夹结构......我这样做对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发我自己的 PHP 框架,我需要一些帮助来确定我是否朝着正确的方向前进...

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...

该框架既供我自己使用,又可进一步提高我的 PHP 技能.我遇到了许多问题,通过克服它们,我学到了很多东西,并且喜欢能够从无到有创造一些东西,所以我不想看到像只使用 Zend"这样的答案!;)

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"! ;)

我已经阅读了很多关于 Stack Overflow 和其他网站的文章,但不能完全得到我需要的正确答案,所以希望有人能给我一些有用的建议!

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...

'理论'框架结构

- .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

详情

  • index.php 是主文件,每个请求都通过 .htaccess 路由到这里.
  • private/ 显然不能公开访问.
  • public/ 包含所有公共文件.
  • app/ 包含所有特定于应用的代码.
  • lib/ 可以包含 Zend 或其他库(我也在自己开发),以使用自动加载器调用
  • bootstrap.php 是特定于应用程序的代码... 我需要这个吗?主要的'index.php'就够了吗?.
  • modules/ 将包含每个模块... 我需要模块吗?.
  • default/ 是包含大多数请求的 MVC 的默认模块(当admin"不是 URL 的第一部分时使用).
  • admin/ 是包含管理部分 MVC 的模块.
  • 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.

无论如何,对于我的问题...

我认为将管理部分与网站的其余部分分开会更好,但这就是我陷入困境的地方.我已经制作了上述结构来使用它,但我不确定这是否是最有效的方法.

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.

如果请求 site.com/videos/view/1/ 来到我的网站..

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

模块:默认控制器:视频操作:查看参数:数组('1')

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

如果请求 site.com/admin/pages/view/1/ 来到我的网站..

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

模块:管理员控制器:页面操作:查看参数:数组('1')

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?

我的管理部分是否应该有一个完全独立的应用程序框架...?我什至需要将管理部分的 MVC 与其他部分分开吗?

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?

抱歉问了这么大的问题,只是想为您提供尽可能多的信息!随意回答你能回答的部分=P

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

推荐答案

一个管理路由的解决方案是 CakePHP 所做的,您首先为管理字符串定义一个配置然后在您的控制器中使用具有特定命名转换的动作

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

另外一点

  1. modules 文件夹似乎不需要
  2. 我同意 antpaw 您应该添加一个全局视图和模型文件夹,以便在应用程序之间共享它们
  3. 我不明白为什么自动加载器在 config 目录中而不是 lib 目录的一部分,您也可以将 boostrap.php 移动到 config 目录

希望能帮到你

这篇关于PHP MVC 框架的文件夹结构......我这样做对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHP MVC 框架的文件夹结构......我这样做对吗?

基础教程推荐