PHP 3-tier architecture folder structure [closed]

若如初见. 提交于 2019-12-11 16:52:18

问题


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:

(folders are bolded)

  • admin
    • include
      • auth.php
    • page1
      • index.php
    • page2
    • index.php
  • include
    • css
      • style.css
    • js
      • jquery-ui-10.4.0 custom
      • javascript.js
    • responsive_gs
    • global.php
    • header
    • footer
  • lib
    • bll
      • class1.bll.php
      • class2.bll.php
      • class3.php
    • dal
      • class1.dal.php
  • users
    • include
      • auth.php
    • page1
      • index.php
    • page2
    • index.php
  • index.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.


回答1:


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.

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

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

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

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/

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.

Your index.php file would look something like this:

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

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

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.

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 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 :)

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 :)



来源:https://stackoverflow.com/questions/23555005/php-3-tier-architecture-folder-structure

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