thinkphp的目录结构设计经验总结
用thinkphp开发了好些项目了;最近准备抽空写一些经验总结;希望能给刚开始接触tp的童鞋们提供一些开发的方案;少走一些弯路;少踩一些坑;这些绝对都是些精华干货;耐着性子阅读;相信收货大大的;
用thinkphp开发了好些项目了;最近准备抽空写一些经验总结;
希望能给刚开始接触tp的童鞋们提供一些开发的方案;少走一些弯路;少踩一些坑;
这些绝对都是些精华干货;耐着性子阅读;相信收货大大的;
可以结合git项目对照研究:thinkbjy http://git.oschina.net/shuaibai123/thinkbjy
先从thinkphp的目录架构开始吧;thinkbjy 白俊遥博客<br>
├─Application 项目逻辑目录<br>
│ ├─Common 公共模块<br>
│ │ ├─Common 公共函数目录<br>
│ │ │ ├─functioin.php 公共函数php文件<br>
│ │ ├─Conf 公共配置文件目录<br>
│ │ │ ├─config.php tp的配置 用于覆盖框架默认配置项<br>
│ │ │ ├─db.php 数据库配置 用户名 密码等<br>
│ │ │ ├─webconfig.php 项目的配置;网站名;是否开启网站等<br>
│ │ ├─Controller 公共控制器目录 <br>
│ │ │ ├─BaseController.class.php 应用最基础的控制器<br>
│ │ │ ├─HomeBaseController.class.php Home基础控制器继承BaseController<br>
│ │ │ ├─AdminBaseController.class.php Admin基础控制器继承BaseController<br>
│ │ │ ├─UserBaseController.class.php User基础控制器继承BaseController<br>
│ │ │ ├─...<br>
│ │ ├─Model 公共模型目录<br>
│ │ │ ├─BaseModel.class.php 应用最基础的Model<br>
│ │ │ ├─ArticleModel.class.php 文章model 继承BaseModel<br>
│ │ │ ├─UserModel.class.php 用户model 继承BaseModel<br>
│ │ │ ├─...<br>
│ │ ├─Tag 公共标签目录<br>
│ │ │ ├─My.class.php 自定义的标签库<br>
│ │ │ ├─...<br>
│ ├─Home Home模块<br>
│ │ ├─Controller Home控制器目录 继承HomeBaseController<br>
│ │ │ ├─ArticleController.class.php 文章控制器目录 <br>
│ │ │ ├─IndexController.class.php 首页控制器<br>
│ │ │ ├─ ... <br>
│ ├─Admin 结构同Home<br>
│ ├─User 结构同Home<br>
├─Public 资源文件目录<br>
│ ├─install 安装引导目录<br>
│ ├─statics 静态资源目录<br>
│ │ ├─bootstrap bootstrap框架<br>
│ │ ├─ueditor ueditor编辑器<br>
│ │ ├─js jquery等第三方js存放的目录<br>
│ │ ├─css animate.css等第三方css目录<br>
│ │ ├─ ... <br>
├─Template 视图文件目录<br>
│ ├─Public 公共目录<br>
│ │ ├─js 公共js目录<br>
│ │ │ ├─base.js 全站都引用的js文件<br>
│ │ │ ├─ ... <br>
│ │ ├─css 公共css目录<br>
│ │ │ ├─base.css 全站都引用的css文件 <br>
│ │ │ ├─ ... <br>
│ │ ├─images 公共图片目录 <br>
│ │ ├─public_head.html 全站通用的公共头部<br>
│ │ ├─public_foot.html 全站通用的公共底部<br>
│ │ ├─... <br>
│ ├─Home 前台Home视图目录 <br>
│ │ ├─Public 前台Home的公共目录<br>
│ │ │ ├─js home下调用的js文件目录<br>
│ │ │ ├─css home下调用的css文件目录<br>
│ │ │ ├─images home下调用的图片文件目录<br>
│ │ ├─Index 首页文件目录<br>
│ │ │ ├─index.html 首页 <br>
│ │ │ ├─ ...<br>
│ ├─Admin 同Home<br>
│ ├─User 同Home<br>
├─Upload 公共上传目录<br>
│ ├─images 上传的图片目录<br>
│ │ ├─avatar 头像目录<br>
│ │ ├─ueditor ueditor编辑器上传的图片目录<br>
│ │ │ ...<br>
│ │ ...<br>
├─Runtime 运行时目录<br>
├─ThinkPHP 框架系统目录
/Application/Common/Common/function.php 这个作为常用公共函数文件;
平时经常用的自定义函数都可以放里面;
比如说 之前写过的p函数 以符合人类阅读的方式打印php数组http://www.baijunyao.com/article/20
然后还建议写:判断用户是否登陆的函数、获取当前登陆用户id的函数、上传函数、图片处理函数、验证码函数、分页函数等等;
/Application/Common/Conf 公共配置项目录下 我建议至少创建如下3个文件
config.php、db.php、webconfig.php 为什么要创建3个配置项文件呢?我来详细的讲解这三个文件的作用;
config.php里面都是系统的配置项;我们这个文件主要是用来覆盖框架默认的配置项;这个文件好理解些;
db.php 数据库的账号密码等;单独放一个文件是因为很多时候;这个文件需要根据应用安装的时候填写的数据库账号密码生成的文件;
webconfig.php 这个文件独立出来的原因同上;
/Application/Common/Controller 公共控制器目录
这个目录下一定要建一个BaseController.class.php 为了开发维护的方便 我们开发要尽量避免改动框架的文件;
所以呢;为了不改动框架的Controller.class.php 我们自己建一个BaseController以后所有的Controller都继承它;
这样只要在BaseController里面写的方法;所有的控制器都继承到了;都可以用到;
但是呢;我们开发的项目可能会比较复杂;只有一个BaseController会比较杂乱;所以我们还可以根据业务需求再建AdminBaseController.class.php 可以在__construct构造函数中判断如果不是管理员;禁止访问;我们只需要所有把所有需要有管理员权限才可访问的控制器全部继承AdminBaseController; 这样这些控制器就不需要每个都判断是否是管理员了;
UserBaseController.class.php 等其他xxBaseController同样道理;比如说用户的个人中心必须是登陆状态才可访问等等;
/Application/Common/Model 公共模型目录
这个目录一定要建一个BaseModel.class.php 为什么呢?
巴拉巴拉。。。
巴拉巴拉。。。
好吧;我在组织语言的时候;发现越写越多;所以我准备单独开一片文章来写BaseModel的问题;
传送门: thinkphp的model模型的设计经验总结http://www.baijunyao.com/article/61
/Application/Common/Tag 公共标签目录
为了方便开发;我们还是必须要建一个自定义的标签库;My.class.php
什么是标签库呢?我们会发现在模板中tp内置好多标签很好用;比如说foreach、volist、eq;
官方的文档传送门:内置标签http://document.thinkphp.cn/manual_3_2.html#inner_tag
什么?不知道怎么自定义标签? 好吧;再来个传送门:thinkphp自定义模板标签http://www.baijunyao.com/article/21
/Public 资源文件目录
可以把boostrap、jquery等第三方插件扔到这里面
/Template 视图目录
建议把每个模块的View都移到这个目录中集中管理;不然开发的时候各种点目录切换;那酸爽;谁用谁知道;
/Template/default/Home/Public/js/base.js 和/Template/default/Home/Public/css/base.css 强烈建议全站都引用;里面写一些全站都可以用的样式或函数;比如css的reset;其他的看上面的目录就可以看明白的;就不多啰嗦了;
/Runtime 运行时目录
和Template一样;建议都移到最外层;方便我们删除缓存;
好吧;认真写博客真的很耗时;这篇博客写了整整3个小时;但愿能帮到童鞋们;
白俊遥博客 http://www.baijunyao.com/
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

PHP variables store values during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above
