PHP 3层架构文件夹结构 [英] PHP 3-tier architecture folder structure

查看:133
本文介绍了PHP 3层架构文件夹结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PHP启动一个家庭网站项目,我打算使用3层架构来实现。但是在这种架构中,我找不到文件夹结构的标准/首选项。我们在我工作的地方使用的文件夹结构如下:

I'm starting a home website project in PHP and I intend to do it with a 3-tier architecture. But I can't find anything on the standards/preferences of folder structure in such an architecture. The folder structure we use where I work is the following one:

(文件夹以粗体显示)


  • 管理员

    • 包括

      • auth .php


      • index.php


      • css

        • style.css


        • jquery-ui-10.4.0自定义

        • javascript.js


        • bll

          • class1.bll.php

          • class2.bll.php

          • class3.php

          • bll
            • class1.bll.php
            • class2.bll.php
            • class3.php

            • class1.dal.php


            • include

              • auth.php


              • 索引。 php

              而且感觉很容易维护。但是在开始这个项目之前,我还是想听听一些意见。

              And it feels very maintainable. But I'd still like to hear some opinions on it before I start this project.

              这个问题被搁置了,因为它显然无法客观地回答。抱歉,我认为在此问题上有一些行业标准。

              The question's been put on hold since it's apparently unanswerable in an objective manner. I apologize, I thought there were some industry standards over the matter.

              推荐答案

              它似乎不能很好地与PSR-0或PSR-4,您可以在此处了解更多有关PSR的信息。我认为应该实践PSR-0或PSR-4,因为它使您能够完成模块化的文件夹结构,同时轻松地创建其他人以及您可以以最小的设置重用/使用的代码。

              It doesn't look like it would work well with PSR-0 or PSR-4, you can read more about PSR here. I think PSR-0 or PSR-4 should be practiced because it allows you to accomplish a modular folder structure while easily creating code that other people as well as you can re/use with minimal setup required.

              我开始通过确定应用程序的核心组件来创建项目的文件夹结构。例如,使用登录系统进行身份验证,注册,发送密码提醒等非常普遍。一旦有了一个模块,就创建一个 composer 程序包,该程序或多或少遵循以下模式:

              I begin creating my folder structure for a project by identifying the core components of my application. For example, it is quite common to use a login system to authenticate, register, send password reminders, etc... Once I have a module in mind, I make a composer package that follows more or less this pattern:

              src/
                  Assets/
                      css/
                      js/
                      less/
                      sass/
                  Controllers/
                      AuthController.php
                      UserController.php
                  Repositories/
                      UserRepository.php
                  Services/
                  Views/
                      partials/
                          login.blade.php
                          status-menu.blade.php
                      password/
                          recover.blade.php
                          reset.blade.php
                  MyServiceProvider.php
                  routes.php
              composer.json
              

              为了使作曲家自动加载名称空间,您需要指定PSR-0或PSR路径-4兼容。例如,在我的composer.json文件上方的登录包中,如下所示:

              In order to get composer to autoload your namespaces you need to specify a path that is PSR-0 or PSR-4 compliant. For example in my login package above my composer.json file would look like so:

              {
                  "name": "robert/login",
                  "description": "description",
                  "authors": [
                      {
                          "name": "Robert Stanfield",
                          "email": "your@email.com"
                      }
                  ],
                  "require": {
                      "php": ">=5.4.0"
                  },
                  "autoload": {
                      "psr-4": {
                         "Robert\\Login\\": "src/"
                      }
                  },
                  "minimum-stability": "stable"
              }
              

              请注意自动加载部分中的psr-4行...您可以通过作曲者文档进一步了解此内容。

              Notice the psr-4 line in the autoload section... you can look more into this through the composer documentation.

              现在,我将所有代码放置在SVN或GIT之类的版本控制系统中,因此可以在可能会用到的其他任何项目中使用它。

              Now I place all that code in a version control system such as SVN or GIT, so I can use it in any other project I may find a use for it in.

              您的主要项目可能是锅炉板项目,可以在项目之间重复使用...像这样:

              Your main project could be a boiler plate project that can be reused between projects... something like this:

              composer.json
              index.php
              public/
              vendor/
              

              现在您可能想创建一个 Core 包含您应用特定代码的软件包或类似名称,不能在其他项目中使用。

              Now you'll probably want to create a Core package or some similar name that contains your apps specific code, that cannot be utilized in other projects.

              您的 index.php 文件如下所示:

              // Use composer's autoloader
              require('vendor/autoload.php');
              
              // Code to handle loading all your service providers...
              // maybe have like an array of service providers and then foreach over them...
              

              现在,我可以简单地使用UserRepository类,例如通过编写以下代码:

              Now I can simply use the UserRepository class, for example by writing the following code:

              use Robert\Login\Repositories\UserRepository;
              
              class UserController
              {
                  protected $userRepo;
              
                  public function __construct(UserRepository $repo)
                  {
                      $this->userRepo = $repo;
                  }
              
              
                  public function showInactiveUsers()
                  {
                      $users = $this->userRepo->getInactiveUsers();
                      // TODO: generate $view
                      return $view;
                  }
              }
              

              请记住,您可以在自己的软件包中要求软件包使用作曲家,因此解耦代码应该没有问题。通过使用composer,您可以相对容易地使用PHP社区已经创建的软件包。

              Keep in mind you can require packages within your own packages with composer, so you should have no problem decoupling your code. By using composer, you get the added benefit of using packages already created by the PHP community relatively easily.

              主要项目 composer.json 文件应该需要您创建的软件包...例如,如果我的登录软件包位于git://example.com/robert/login.git

              The main projects composer.json file should require the package you create... for example if I had my login package located at git://example.com/robert/login.git

              {
                  "minimum-stability": "stable",
                  "repositories": [
                      {
                          "type": "git",
                          "url": "git://example.com/robert/login.git"
                      }
                  ],
                  "require": {
                      "robert/login": "dev-master"
                  }
              }
              

              Composer可以安装您软件包的任何版本...您也可以在composer文档中阅读有关它的更多信息,它基本上可以与您的标签和分支名称配合使用:)

              Composer can install whatever version of your package... you can read more about that in the composer documentation as well, it basically works with your tags and branch names :)

              您将要创建一个程序来编译和/或缩小您的资产,并将其放置在公用文件夹中。在纯JavaScript或CSS的情况下,这减少了HTTP请求并减少了带宽。您可以使用诸如 gulp 之类的东西来实现此目的。

              You'll want to create a program to compile and/or minify your assets as well and places them in your public folder. This reduces HTTP request and reduces bandwidth in the case of plain JavaScript or CSS. You can use something such as gulp to accomplish this.

              希望我涵盖了所有内容……这是一个很大的主意,但是一个简单的主意:)

              Hope I covered it all... it's quite a large idea, but a simple one :)

              这篇关于PHP 3层架构文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆