如何构造PHP项目?

时间:2023-01-13 12:08:09

I am about to embark upon yet another large PHP project. This time, I intend to have the project folder be tidy! So I have a few questions concerning keeping my project clean and DRY:

我即将开始另一个大型PHP项目。这次,我打算把项目文件夹整理一下!所以我有几个关于保持项目清洁和干燥的问题:

  • How do I differentiate between PHP source files and PHP files that should be accessed by the browser? In other words, how do I make it clear which PHP files gives output and which gives function or class definitions?

    如何区分浏览器应该访问的PHP源文件和PHP文件?换句话说,如何明确哪些PHP文件提供输出,哪些文件提供函数或类定义?

  • I am planning on separating my PHP functions into static classes, separated by subject, for instance database::create() or editor::write(). What are your thoughts on this?

    我正在计划将PHP函数分离到静态类中,由主题分隔,例如数据库::create()或编辑器::write()。你对此有何看法?

  • I am planning on creating a PHP file "core.php" that will be included at the top of EVERY SINGLE PHP file in the project. This file will handle authentication and include base functions. Thoughts?

    我计划创建一个PHP文件“core”。这将包含在项目中每个php文件的顶部。这个文件将处理身份验证并包含基本函数。想法吗?

  • The project will be heavily Ajax based. This begs the question: Should my PHP functions be actual PHP functions, or should they be individual PHP files that takes GET or POST input? My plan is doing both. I create a folder for PHP source files and one for PHP "ajax" files (or something), and have the latter folder populated with files names by the functions they will perform. The PHP file will then simply contain a call to a function and write output for Ajax. Thoughts?

    该项目将大量基于Ajax。这就引出了一个问题:我的PHP函数应该是实际的PHP函数,还是应该是接受GET或POST输入的单个PHP文件?我的计划是两者兼顾。我为PHP源文件创建了一个文件夹,为PHP“ajax”文件(或其他文件)创建了一个文件夹,并让后一个文件夹使用它们将执行的函数填充文件名。然后PHP文件将简单地包含对函数的调用,并为Ajax编写输出。想法吗?

Any other comments or tips before I start this project would be great!

在我开始这个项目之前,任何其他的评论或建议都是很好的!


Edit

I may have failed to emphasize just how heavily Ajax based this web application will be. Codeigniter seems like a great tool for creating web pages or simple web applications such as a blog. My application will be a little different. Much like the uTorrent WebUI, my web application will be static on one page, having Ajax performing all the under-the-cover actions. It seems a little awkward to use Codeigniter for such a project.

我可能没有强调这个web应用程序基于Ajax的程度有多大。Codeigniter似乎是创建web页面或简单web应用程序(如blog)的好工具。我的申请会有一点不同。就像uTorrent WebUI一样,我的web应用程序在一个页面上是静态的,让Ajax执行所有隐藏的操作。在这样的项目中使用Codeigniter似乎有点尴尬。

Is there a different architecture out there created with this kind of application in mind?

是否有一种不同的体系结构在头脑中创建了这种应用程序?

4 个解决方案

#1


3  

Have you considered using a framework such as Codeigniter? Are you familiar with any sort of design patterns, such as MVC? Both of those would help dictate the location of files and separation of logic in your project.

您是否考虑过使用Codeigniter这样的框架?您熟悉任何类型的设计模式吗,比如MVC?这两种方法都可以帮助您在项目中指定文件的位置和逻辑的分离。

EDIT: Codeigniter uses MVC, which is just a great pattern for web programming. Check out these two videos they have for tutorials. Only about 30 minutes combined, and definitely full of good knowledge, both as a look at the way their framework works, as well as the insight offered by their folder structure.

编辑:Codeigniter使用MVC,这是一种很好的web编程模式。看看这两个视频教程。总共只有大约30分钟,而且肯定充满了良好的知识,这既是对他们的框架工作方式的观察,也是对他们的文件夹结构所提供的洞察力。

EDIT2: http://codeigniter.com/tutorials

EDIT2:http://codeigniter.com/tutorials

#2


3  

If you're expecting this to be a large project I would highly recommend using one of the many established frameworks out there already like Symfony or Zend. You'd be:

如果您希望这是一个大型项目,我强烈建议您使用Symfony或Zend这样的现有框架。你会:

  • cutting down drastically on the amount of code you'd have to write
  • 大幅减少您必须编写的代码量
  • avoid bugs in your components
  • 避免组件中的错误
  • learning best practices
  • 学习的最佳实践

But you if you insists on doing it yourself.

但如果你坚持自己做的话。

I am planning on creating a PHP file "core.php" that will be included at the top of EVERY SINGLE PHP file in the project. This file will handle authentication and include base functions. Thoughts?

我计划创建一个PHP文件“core”。这将包含在项目中每个php文件的顶部。这个文件将处理身份验证并包含基本函数。想法吗?

I cannot stress enough 'do not do this'. There is a rule among experienced PHP developers that any project with a large core.php file that it's a warning sign of bad development and should be best avoided.

我再怎么强调都不为过。在经验丰富的PHP开发人员中有一条规则,即任何核心较大的项目都是如此。php文件,它是糟糕开发的警告标志,最好避免。

Secondly there the is no need to reinvent the wheel when it comes to database abstract, look at the pear MDB2 or Doctrine projects.

其次,当涉及到数据库抽象、查看pear MDB2或Doctrine项目时,不需要重新设计*。

Large numbers of static classes are also a sign of poorly conceived development. Static classes are to be used sparingly as they are difficult to test, and often not needed.

大量的静态类也是构思糟糕的开发的标志。静态类要谨慎使用,因为它们很难测试,而且通常不需要。

#3


1  

Why not have one index.php that handles all of the execution and then have a .htaccess file that will direct urls to the correct location. This means that you can have all of you login and checking in one file.

为什么没有一个指数呢?处理所有执行的php,然后有一个.htaccess文件,该文件将url指向正确的位置。这意味着您可以让所有人登录并签入一个文件。

You can then have a condition at the top of all of your other file's so they cannot be directly accessed like:

然后,您可以在所有其他文件的顶部设置一个条件,因此不能直接访问这些条件,如:

<?php if(FROM_INDEX!="true") die("<b>ACCESS_ERROR</b><br /><br />Please use the main page instead of accessing this file directly.");

I also recommend Codeigniter as this has a lot of features like this already and will greatly speed up your development time.

我还推荐Codeigniter,因为它已经有很多这样的特性,并且将极大地加快您的开发时间。

#4


0  

I'm not sure how complicated of a system you are looking for. But I recently found a small framework that works off the url. https://github.com/ivorychicken/sammy

我不知道你要找的系统有多复杂。但我最近发现了一个小框架,可以在url上工作。https://github.com/ivorychicken/sammy

If you don't mind taking the time to play with it, you could probably use it as a starting point and build off of it.

如果你不介意花时间去玩它,你可能会把它作为一个起点,并以此为基础。

#1


3  

Have you considered using a framework such as Codeigniter? Are you familiar with any sort of design patterns, such as MVC? Both of those would help dictate the location of files and separation of logic in your project.

您是否考虑过使用Codeigniter这样的框架?您熟悉任何类型的设计模式吗,比如MVC?这两种方法都可以帮助您在项目中指定文件的位置和逻辑的分离。

EDIT: Codeigniter uses MVC, which is just a great pattern for web programming. Check out these two videos they have for tutorials. Only about 30 minutes combined, and definitely full of good knowledge, both as a look at the way their framework works, as well as the insight offered by their folder structure.

编辑:Codeigniter使用MVC,这是一种很好的web编程模式。看看这两个视频教程。总共只有大约30分钟,而且肯定充满了良好的知识,这既是对他们的框架工作方式的观察,也是对他们的文件夹结构所提供的洞察力。

EDIT2: http://codeigniter.com/tutorials

EDIT2:http://codeigniter.com/tutorials

#2


3  

If you're expecting this to be a large project I would highly recommend using one of the many established frameworks out there already like Symfony or Zend. You'd be:

如果您希望这是一个大型项目,我强烈建议您使用Symfony或Zend这样的现有框架。你会:

  • cutting down drastically on the amount of code you'd have to write
  • 大幅减少您必须编写的代码量
  • avoid bugs in your components
  • 避免组件中的错误
  • learning best practices
  • 学习的最佳实践

But you if you insists on doing it yourself.

但如果你坚持自己做的话。

I am planning on creating a PHP file "core.php" that will be included at the top of EVERY SINGLE PHP file in the project. This file will handle authentication and include base functions. Thoughts?

我计划创建一个PHP文件“core”。这将包含在项目中每个php文件的顶部。这个文件将处理身份验证并包含基本函数。想法吗?

I cannot stress enough 'do not do this'. There is a rule among experienced PHP developers that any project with a large core.php file that it's a warning sign of bad development and should be best avoided.

我再怎么强调都不为过。在经验丰富的PHP开发人员中有一条规则,即任何核心较大的项目都是如此。php文件,它是糟糕开发的警告标志,最好避免。

Secondly there the is no need to reinvent the wheel when it comes to database abstract, look at the pear MDB2 or Doctrine projects.

其次,当涉及到数据库抽象、查看pear MDB2或Doctrine项目时,不需要重新设计*。

Large numbers of static classes are also a sign of poorly conceived development. Static classes are to be used sparingly as they are difficult to test, and often not needed.

大量的静态类也是构思糟糕的开发的标志。静态类要谨慎使用,因为它们很难测试,而且通常不需要。

#3


1  

Why not have one index.php that handles all of the execution and then have a .htaccess file that will direct urls to the correct location. This means that you can have all of you login and checking in one file.

为什么没有一个指数呢?处理所有执行的php,然后有一个.htaccess文件,该文件将url指向正确的位置。这意味着您可以让所有人登录并签入一个文件。

You can then have a condition at the top of all of your other file's so they cannot be directly accessed like:

然后,您可以在所有其他文件的顶部设置一个条件,因此不能直接访问这些条件,如:

<?php if(FROM_INDEX!="true") die("<b>ACCESS_ERROR</b><br /><br />Please use the main page instead of accessing this file directly.");

I also recommend Codeigniter as this has a lot of features like this already and will greatly speed up your development time.

我还推荐Codeigniter,因为它已经有很多这样的特性,并且将极大地加快您的开发时间。

#4


0  

I'm not sure how complicated of a system you are looking for. But I recently found a small framework that works off the url. https://github.com/ivorychicken/sammy

我不知道你要找的系统有多复杂。但我最近发现了一个小框架,可以在url上工作。https://github.com/ivorychicken/sammy

If you don't mind taking the time to play with it, you could probably use it as a starting point and build off of it.

如果你不介意花时间去玩它,你可能会把它作为一个起点,并以此为基础。