Application Directory (Folders) Structure

本文介绍了一种基于Zend框架项目的文件和文件夹结构,该结构易于理解且具备灵活性,适用于大小不同的网站。文章还提出了一种核心CMS的概念,该CMS包含多个模块,每个模块都可以独立复制并应用于其他CMS中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

My goal was to create a structure that is very easy to understand, flexible enough for big or small websites and not totally different from what books or other guides recommend as a best practice files and folders structure for a Zend Framework project.

Another goal was to create a core cms, which has lots of modules that can be used on multiple websites. Every module is designed so that it can be copied and put in another cms without having to modify any file of that cms. The module of a directory holds all the files it need to work, i has its own bootstrap, its configuration files, its views and of course its own models.

This post contains the recommended structure for an application developed using the Zend Framework (ZF) based on the quick start, but there are several changes made by me. I won't use the controllers folder, i will put my default controllers in the homepage module directory which is my default module. As you can see the MVC strucure is in the modules directory...

The models directory has several sub folders, for mongo db models, for mysql (maria db) models, for session management models and another one for the xml read / write models.

The views folders in each module directory contain the view scripts for the actions and there is a helpers folders for module specific helpers. The helpers that can be used website wide are put in the helpers folder that is in the layout directory.

Every module also has a configs folder for module configuration files, like the config.xml, the translations.tmx file, the routes.xml file, the acl rules and resources xml file and eventually some other xml files containing the widgets layouts. There is also a main configs folder in the application directory where i store files which have values that can be used website wide by all modules.

Create the following application folders structure:

myapp
|-- application
| |-- caches
| |-- configs
| |-- layouts
| | |-- helpers
| | `-- scripts
| |-- logs
| |-- modules
| | |-- admin
| | | |-- configs
| | | |-- controllers
| | | |-- forms
| | | |-- models
| | | | |-- mongo
| | | | |-- mysql
| | | | |-- session
| | | | `-- xml
| | | `-- views
| | | |-- helpers
| | | `-- scripts
| | |-- homepage
| | `-- news
| |-- services
| `-- widgets
|-- library
| |-- My
| `-- Zend
`-- public
|-- captcha
|-- css
|-- fonts
|-- images
|-- javascript
|-- qrcodes
`-- uploads

* myapp is the main directory
* the application folder will contain everything related to your app (your php code), we put all our code one level lower then the public files so that they aren't publicly-accessible
* the public folder will contain files like your css, javascript and images (you can also rename this folder htaccess)
* the library folder is for your library and the zend library

Download the latest version of the Zend Framework and put it into the "library" folder.

This is how myapp directory is build, there are more layouts you could try. More Informations about Zend Framework folder layouts can be found here: http://framework.zend.com/wiki/display/ZFDEV/Choosing+Your+Application's+Directory+Layout

Ok now your ready for the next step, creating the index.php file ... to be continued ;)

原文地址:http://www.chris.lu/en/news/show/4d40a471d6186/


 

PS C:\Users\Administrator> <# >> .SYNOPSIS >> Initialize development environment and create project structure >> .DESCRIPTION >> Create standardized project directory structure with basic files >> #> >> >> function Initialize-Project { >> param( >> [Parameter(Mandatory=$true)] >> [string]$ProjectName, >> >> [ValidateSet("web", "python", "node", "dotnet")] >> [string]$ProjectType = "web", >> >> [string]$RootPath = "E:\ProjectEcosystem\ProjectMonitor" >> ) >> >> # 设置控制台编码为UTF-8防止乱码 >> [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 >> $OutputEncoding = [System.Text.Encoding]::UTF8 >> >> # 创建项目根目录 >> $projectDir = Join-Path -Path $RootPath -ChildPath $ProjectName >> New-Item -Path $projectDir -ItemType Directory -Force | Out-Null >> Set-Location $projectDir >> >> # 创建基础目录结构 >> $dirs = @('src', 'docs', 'tests', 'config', 'logs') >> $dirs | ForEach-Object { >> New-Item -Path $_ -ItemType Directory -Force | Out-Null >> } >> >> # 创建项目类型特定的文件 >> switch ($ProjectType.ToLower()) { >> "web" { >> # HTML基础文件 >> Set-Content -Path 'src\index.html' -Value @' >> <!DOCTYPE html> >> <html lang="zh-CN"> >> <head> >> <meta charset="UTF-8"> >> <title>$ProjectName</title> >> <link rel="stylesheet" href="style.css"> >> </head> >> <body> >> <h1>欢迎来到 $ProjectName</h1> >> <script src="main.js"></script> >> </body> >> </html> >> '@ >> # CSS文件 >> Set-Content -Path 'src\style.css' -Value @' >> body { >> font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; >> margin: 0; >> padding: 20px; >> background-color: #f5f5f5; >> } >> '@ >> # JavaScript文件 >> Set-Content -Path 'src\main.js' -Value @' >> console.log("$ProjectName 已启动"); >> document.addEventListener('DOMContentLoaded', () => { >> console.log("DOM 加载完成"); >> }); >> '@ >> } >> "python" { >> # Python主文件 >> Set-Content -Path 'src\main.py' -Value @' >> import logging >> >> logging.basicConfig(level=logging.INFO, >> format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', >> filename='../logs/app.log') >> >> def main(): >> print("项目 $ProjectName 已启动") >> logging.info("应用程序启动") >> >> if __name__ == "__main__": >> main() >> '@ >> # 需求文件 >> Set-Content -Path 'requirements.txt' -Value '# 项目依赖' >> } >> "node" { >> # Node.js入口文件 >> Set-Content -Path 'src\index.js' -Value @' >> const express = require('express'); >> const app = express(); >> const port = 3000; >> >> app.get('/', (req, res) => { >> res.send('欢迎来到 $ProjectName'); >> }); >> >> app.listen(port, () => { >> console.log(`服务器运行在 http://localhost:${port}`); >> }); >> '@ >> # package.json >> Set-Content -Path 'package.json' -Value @' >> { >> "name": "$ProjectName", >> "version": "1.0.0", >> "description": "", >> "main": "src/index.js", >> "scripts": { >> "start": "node src/index.js", >> "test": "echo \"Error: no test specified\" && exit 1" >> }, >> "keywords": [], >> "author": "", >> "license": "ISC", >> "dependencies": { >> "express": "^4.18.2" >> } >> } >> '@ >> } >> "dotnet" { >> # 生成随机GUID >> $guid = [Guid]::NewGuid().ToString().ToUpper() >> >> # .NET解决方案文件 >> Set-Content -Path "$ProjectName.sln" -Value @" >> Microsoft Visual Studio Solution File, Format Version 12.00 >> # Visual Studio Version 17 >> Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "$ProjectName", "src\$ProjectName\$ProjectName.csproj", "{$guid}" >> EndProject >> "@ >> # 创建项目目录 >> $projectSrcDir = Join-Path -Path 'src' -ChildPath $ProjectName >> New-Item -Path $projectSrcDir -ItemType Directory -Force | Out-Null >> >> # .csproj文件 >> Set-Content -Path "$projectSrcDir\$ProjectName.csproj" -Value @' >> <Project Sdk="Microsoft.NET.Sdk.Web"> >> <PropertyGroup> >> <TargetFramework>net8.0</TargetFramework> >> </PropertyGroup> >> </Project> >> '@ >> # Program.cs文件 >> Set-Content -Path "$projectSrcDir\Program.cs" -Value @' >> var builder = WebApplication.CreateBuilder(args); >> var app = builder.Build(); >> >> app.MapGet("/", () => "欢迎来到 $ProjectName"); >> >> app.Run(); >> '@ >> } >> } >> >> # 创建.gitignore文件 >> Set-Content -Path '.gitignore' -Value @' >> # 忽略日志文件 >> logs/ >> *.log >> >> # 忽略编译输出 >> bin/ >> obj/ >> dist/ >> build/ >> >> # 忽略环境文件 >> .env >> .venv >> >> # 忽略IDE设置 >> .vscode/ >> .idea/ >> '@ >> >> # 创建VS Code工作区 >> Set-Content -Path "$ProjectName.code-workspace" -Value @' >> { >> "folders": [ >> { >> "path": "." >> } >> ], >> "settings": { >> "files.exclude": { >> "**/.git": true, >> "**/.svn": true, >> "**/.hg": true, >> "**/CVS": true, >> "**/.DS_Store": true, >> "**/Thumbs.db": true >> } >> } >> } >> '@ >> >> Write-Host "✅ Project '$ProjectName' created at: $projectDir" -ForegroundColor Green >> Write-Host "🛠 Open project in VS Code with:" -ForegroundColor Cyan >> Write-Host " code '$ProjectName.code-workspace'" -ForegroundColor Yellow >> } >> PS C:\Users\Administrator> # 1. 导航到脚本目录 >> cd E:\ProjectEcosystem\ProjectMonitor >> >> # 2. 点导入脚本(注意空格) >> . .\Initialize-DevEnv.ps1 >> >> # 3. 创建项目(使用英文名称避免路径问题) >> Initialize-Project -ProjectName "MyWebApp" -ProjectType "web" >> Initialize-Project -ProjectName "DataAnalysis" -ProjectType "python" >> Initialize-Project -ProjectName "APIServer" -ProjectType "node" >> Initialize-Project -ProjectName "BackendService" -ProjectType "dotnet" >> >> # 4. 打开项目 >> code .\MyWebApp\MyWebApp.code-workspace >> Export-ModuleMember : 只能从模块内调用 Export-ModuleMember cmdlet。 所在位置 E:\ProjectEcosystem\ProjectMonitor\Initialize-DevEnv.ps1:199 字符: 1 + Export-ModuleMember -Function Initialize-Project + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (:) [Export-ModuleMember], InvalidOperationException + FullyQualifiedErrorId : Modules_CanOnlyExecuteExportModuleMemberInsideAModule,Microsoft.PowerShell.Commands.Expo rtModuleMemberCommand 鉁?椤圭洰 'MyWebApp' 宸插垱寤哄湪: E:\ProjectEcosystem\ProjectMonitor\MyWebApp 馃洜 浣跨敤浠ヤ笅鍛戒护鍦╒S Code涓墦寮€椤圭洰: code 'MyWebApp.code-workspace' 鉁?椤圭洰 'DataAnalysis' 宸插垱寤哄湪: E:\ProjectEcosystem\ProjectMonitor\DataAnalysis 馃洜 浣跨敤浠ヤ笅鍛戒护鍦╒S Code涓墦寮€椤圭洰: code 'DataAnalysis.code-workspace' 鉁?椤圭洰 'APIServer' 宸插垱寤哄湪: E:\ProjectEcosystem\ProjectMonitor\APIServer 馃洜 浣跨敤浠ヤ笅鍛戒护鍦╒S Code涓墦寮€椤圭洰: code 'APIServer.code-workspace' 鉁?椤圭洰 'BackendService' 宸插垱寤哄湪: E:\ProjectEcosystem\ProjectMonitor\BackendService 馃洜 浣跨敤浠ヤ笅鍛戒护鍦╒S Code涓墦寮€椤圭洰: code 'BackendService.code-workspace' PS E:\ProjectEcosystem\ProjectMonitor\BackendService> 弹出来了个窗口 写着发行说明1.13.0 (全是英文我没爱看 也看不懂)还有个”{}MyWebApp.code-workspace 打开聊天(ctrL+I)、选择语言(ctrl+K M)或使用模板填充(undefined)以开始使用。 开始输入以关闭或不再显示此信息。“
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值