Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I want to know what architectures are commonly used in developing PHP web applications. I've used MVC earlier for J2EE, but in J2EE everything is separated as part of the language itself (pojo/servlet/jsp).

I don't know how to utilise MVC in PHP as the separation of concerns is code-only. Can we use MVC in PHP as well and if so, how? Are there any other architectures that I should be aware of?

share|improve this question
1  
You should learn about PSR-0/1/2.. and read the book about patterns. Patterns has nothing to do with a language, its all about language syntax, not a language itself –  djay Jul 7 '13 at 15:44
    
I wanted to say Spaghetti, or Big Ball of Mud... but that's just because those are very common, particularly in older PHP applications. MVC seems to have been settled on though... –  Stephen Orr Jul 8 '13 at 10:40
    
@metal_fan "the book"? And what might that be? PoEAA? Design Patterns: Elements of Reusable Object-Oriented Software? Head First Design Patterns? Also patterns doesn't have anything to do with language syntax, but with patterns. –  PeeHaa Jul 8 '13 at 13:19
    
Design patterns are language agnostic. They're common terms for programmers from all languages. –  Jimbo Jul 8 '13 at 14:51

5 Answers 5

up vote 3 down vote accepted

MVC can be done in PHP, and most of the popular frameworks implement something that is either pretty close to actual MVC, or at least takes some of its ideas. If you're really orthodox about MVC, you'll find that most of the popular MVC in any language don't actually implement it correctly (the most common complaint being that the controllers are too "fat", handling a lot of presentation and domain logic), and PHP is no exception, but other than that, yes, MVC is currently one of the popular architectural approaches used in PHP.

Another one that's fairly popular is the Tiered architecture, typically Three-Tier; a request is picked up by the user-facing presentation tier, handed to the logic tier, which calls into the data access tier to get and modify persistent data, then does some calculations, then hands it back to the presentation tier, which takes care of massaging the data into a user-digestible form (typically a web page). The difference with MVC is that the Three-Tiered architecture bundles input processing (MVC: Controller) and output formatting (MVC: View) into the Presentation tier, but it strictly separates data access from logic, both of which are the realm of the Model in MVC.

Many projects also use some sort of hybrid between MVC and Three-Tiered, e.g. splitting the Model into domain logic and persistence. Sometimes, the tiers are split up and connected through web services (RESTful or similar) to allow for better scalability and reuse.

Unfortunately, however, another very popular architecture for PHP systems is "none at all". PHP is famous for "organic growth"; it has a very low barrier of entry, and many PHP projects started life as a static website with a tiny bit of dynamic server-side processing inserted here and there. These projects then grew out of hand, without any planned architecture at all; by the time the professionals are called in, it is often too late to turn the tide.

share|improve this answer
    
well i cant agree more on the last point. I just moved to php (from j2ee) just to reduce development time. However I am taken aback for the same reason. So my aim is to keep the code clean & reusable as much as possible, so that it can easily be ported later on. –  ay89 Jul 8 '13 at 16:59

Web-development is a fast paced world, and because most applications in the PHP world do not share the UNIX philosophy and were historically weak in code-reuse, you probably find less architectural patters by design which leads to patterns that come up by doing. Here are a few:

With the introduction of OOP in PHP 5.0 and more importantly 5.2 things started to change. Development speed has grown esp. since the release of PHP 5.3 (which now is end of live) and so more and more of the PHP code base out there adopts to SOLID principles. Architectural patterns are still hard to find.

You will find a lightweight comparison related to j2ee with scripting languages like PHP in Martin Fowlers book Patterns of Enterprise Application Architecture (P of EAA) which also shows many patterns that are good to know about in conjunction with the list of more anti-pattern (but still worth to learn about) I listed first.

Very popular in the PHP world is Nikic's Don't be STUPID: GRASP SOLID!. It should give you a good introduction to the field if you're coming from Java.

share|improve this answer
1  
This answer deserves soooo much more love –  PeeHaa Jul 8 '13 at 13:17
    
Yes okay +1 for hakre –  Jimbo Jul 8 '13 at 13:31

The problem with Model-View-Controller is that it's so open to abuse (and also opinion) that it's incredibly hard to get 'correct' from one developer's view to another, and that's without even throwing in the fact that simply putting code into a class does not make it "Objected Oriented Programming".

Symfony2 doesn't claim to be "MVC" more than a few times for the previous reason. It's a great framework, but utilises poor design concepts like the service locator pattern (an anti-pattern; ooh controversial) which hides class dependencies.

CakePHP and CodeIngiter are well known for being easy to get into - hence you get a lot of bad practice and poorly written applications from them.

If you want to just use a framework and get on with it, Symfony2 is a great choice. If you want more of a say in your application architecture, take a look at Silex.


Now, as for the "how" of doing MVC in PHP, here's one way of going about it.

Front Controller / Bootstrap

You utilise a front controller (maybe using a .htaccess) to take all requests through. From the request, you can do multiple things - depending on your level of abstraction you may have a Request object or you're just using plain old $_SERVER.

Here you bootstrap your required files and then you pass your data to the router.

Oh, and you'll also need an autoloader that knows exactly where to look for your files when you ask for them. PSR-0 is a good standard to follow here and includes the code for a basic PSR-compliant autoloader.

Router

Your router's job is to route the request to the appropriate Controller / Action. For example: application/login will have the router route to the login controller, and (as no action is specified) the index action (this is entirely up to you how you code it).

You can even utilise regex (like Symfony2 does) to figure the correct route for your request.

You'll typically then instantiate an instance of the controller class, then run the action on it by executing the method.

The Controller

You're in your controller. This is where things get complex, not easy. If you want to use GOOD OOP, don't hide class dependencies. This means you should pass the controller dependencies into the class, maybe using reflection to perform recursive object instantiation. Auryn is a brilliant example of this.

Regardless of whether or not you want to follow good practice, your controller is where you make requests to the model layer to retrieve data.

The Model

Your model is a layer, hence it is not pluralised. You can view what the model layer does here. In short, the layer consists of:

  • Domain Objects
  • Data Mappers
  • Services

These perform your business logic, as abstracted as you want, and return that data to the controller that requested it.

The View

Finally, you shove this data in a view and render the relevant template (via include).

The view goes even further here; you have views and templates, which are not the same, but that's the extent of my knowledge and, for all intents and purposes, simply including the relevant file with the required variables to display has been fine for me.

share|improve this answer

In PHP also we use architectures like MVC or HMVC. Conceptually it is same i.e. separation of different parts and dependencies.

Many frameworks in PHP use MVC architecture. CodeIgniter, CakePHP, Zend, Symfony, Yii are popular frameworks. You can look into them.

share|improve this answer
1  
-1 : Yii is the worst framework in the world which full of bad practices, including global state, MVC/PSR-0 misinterpretation. Then comes CI and Cake. And also HMVC isn't commonly adopted. –  djay Jul 7 '13 at 15:42
3  
Why -1? You may not like Yii. Many others like Yii. The question was about MVC usage. Not about personal framework preferences. HMVC is not commonly adopted now, but it is an architecture that is supported in PHP also. You can use HMVC in CodeIgniter for example. –  gurudeb Jul 7 '13 at 15:49
1  
People that have no clue on what 1) actually MVC is 2) SOLID principles 3) Law of Demeter 4) And maybe DI, like Yii. And also question is about patterns in PHP, not only MVC –  djay Jul 7 '13 at 15:52
3  
+1 just to offset the misguided -1 from @metal_fan: your own despite of Yii (only one of the frameworks mentioned) doesn't support a -1. –  cori Jul 8 '13 at 3:15
1  
+1 to further offset it - you didn't state your opinion on any of them, you merely stated a fact which is that they are popular frameworks. –  Jimbo Jul 8 '13 at 12:59

In PHP we generally use MVC architecture. Here in brief it is defined

http://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm

Many frameworks in PHP use MVC architecture. CodeIgniter, CakePHP, Zend, Symfony, Yii are popular frameworks. I suggest to choose the framework which suits you and your projects. Read these blogs you will have an idea what will be best for you.

http://davidjconnelly.wordpress.com/2011/07/03/the-best-php-framework-of-2011/

http://www.scriptiny.com/2013/04/compare-ponder-and-choose-the-right-php-framework/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.