开发者

What are some good ways to write PHP application with modules support?

开发者 https://www.devze.com 2022-12-30 05:18 出处:网络
I\'m starting to write a application in php with one of my friends and was wondering, if you have any advice on how to implement module support into our application.

I'm starting to write a application in php with one of my friends and was wondering, if you have any advice on how to implement module support into our application.

Or is there a way how to automatically load modules written in php by a php application? Or should i just rely on __autoload function?

And we don't need plugins that are installable via a web in开发者_开发知识库terface, we just need some clever way to associate web pages of our project to some classes (they will render the page) so index.php can call the right class and retrieve it's generated sub-page.

And we are not using any kind of framework, for now at least.


Re your comment: Autoloading in conjunction with strict file naming would be totally enough in this case IMO.

You could define a specific namespace (not in the new PHP 5.3 namespace way, just in the sense of a common prefix to names), e.g. Module_* for module class names.

You could then organize your modules in directories with class files that contain exactly one class definition, for example:

/modules/Mail/index.php   // defines class Module_Mail
/modules/Database/index.php   // defines class Module_Database
/modules/Image/index.php   // defines class Module_Image

Your autoloader function would then, whenever a Module_* class is requested:

$Database = new Module_Database("localhost", .....);

include the correct file from the right directory.

That's the way e.g. the Zend Framework does it, and does so pretty well.

Consider choosing a more specific namespace than Module_ to assure interoperability with other scripts and applications if that is an option in the future.

Slightly related: In a PHP project, how do you organize and access your helper objects?


It sounds like you are looking for a way to organise the different tasks that each page needs to carry out. In this case, take a look at the MVC pattern. It provides a simple way to seperate your data access (models) and how you render/present information (views).

You can map pages to functions with ease. If you store the information in an array of mapped values, and then use a function to compare the requested URL with each of the URLs in the array. Such an array could look like this:

$urls = array(
    '/' => 'index',
    '/aboutus/' => 'aboutUs',
);

There are several articles that discuss how to implement it in PHP in a couple of hours. This article is a very simple guide. I am not a fan of their use of the registry pattern but reading through should provide you with enough information as to how you can implement it yourself.


Regarding autoloading, you can also use spl_autoload_register, to define several (more than one) autoload functions, so each module can set up it's own implementation of autoloading.


All you need here is an MVC architecture, with one Controller class associated with each "module".

If you are not against using a framework, go for Zend MVC

It allows you to have the following principle :

  • An URL like this : http://yoursite.com/my-module/edit
  • Will more or less automatically call the editAction method in the MyModuleController class.
0

精彩评论

暂无评论...
验证码 换一张
取 消