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

发布于 2024-08-19 17:35:06 字数 2129 浏览 1 评论 0原文

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

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

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

我尝试了几种不同的解决方案,但最终我自己都感到困惑,我不知道现在该往哪个方向走!我不太明白这一切...

“理论”框架结构

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

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

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

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

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

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

模块:管理员 控制器:页面 操作:查看 Params: array( '1' )

这是解决这个问题的正确方法吗?或者我是否过于复杂化并做了一些不值得做的事情?

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最后的乘客 2024-08-26 17:35:06

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

//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/
    }

}

您可以通过使用路由系统来概括这一点
只是看看你最喜欢的框架是如何做到的

另一条注释

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

希望这有帮助

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

記柔刀 2024-08-26 17:35:06

我知道这个问题很久以前就被问到了,但几天前我也遇到了同样的情况。

奇怪的是,提问者提出的解决方案基本上就是我所采用的。基本上,我借用了 ASP.NET MVC2 中的一个概念,称为“区域”。区域是网站的部分,有自己的控制器和视图(也是模型,但我不知道为什么......模型通常应该是通用的)。所以这和你最初的想法非常相似。

无论如何,遵循他们的文件夹+路由结构对我的应用程序来说非常有意义(管理类型区域、用户区域和中间的另一个级别)。看看它,你可能会发现一些成功。

我的路由只考虑区域。路线是硬编码的,所以如果我需要另一个区域,我只需调整我的路线文件。哦还有,如果指定了 $area,我的自动加载器设置为在区域文件夹中查找。

/admin/team/add/ 读作,区域:管理员,控制器:团队,操作:添加,

/team/add/ 读作,区域:[无],控制器:团队,操作:添加

文件夹结构有点像这样:

app/
   areas/
      admin/
          controllers/
          views/
      staff/
          controllers/
          views/
   controllers/
   models/
   views/

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/
兮颜 2024-08-26 17:35:06

我建议您使用 bootstrap.php 来管理所有路由,这样您就不会遇到诸如“我希望我可以在管理模块中再嵌套一个文件夹”之类的问题。

我也不会使用模块并将默认控制器保留在控制器/目录中,将管理控制器保留在控制器/管理目录中。模型和视图也是如此。

顺便说一句,不在应用程序的不同部分之间共享模型确实不聪明,它们在 99% 的情况下都是相同的。这就是为什么 mvc 如此强大。有时您甚至可以在前端和后端之间共享应用程序内的一些视图部分。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文