Architecture of very complex php applications? - php

I want to know which php architecture strategies developers use in complex php applications . So far , i know the mvc structure which consists of models, views and controller (and controller plugins which handle with common tasks such as user access controller ). I know some good php frameworks which makes some common stuffs easier .But the problem starts when i thing about huge and complex php applications . because in these applications there are lots of stuff to do or lots of think to check, so i can not decide which code should be where .
Think about magento application , this is very huge application . when i review the source code of application , i can not understand the design strategy . i know there are some perfect design strategies which can handle very big php applications easily , because they can not build such a huge application with a very weak design strategy .the design strategy should support more than you want , so you can improve your code and application easily
To sum up , i want to how can i create bigger applications . Now the design strategies i use in my applications limits me , so i can not create more complex applications . i want to know which the design strategy can handle complex applications.
i know this is very abstract question , but this is because now my php background coming from amateur hobby not from academic . i want to do more , but i am in somewhere where i can not go one step more , because i can not find more complex info about coding . whatever ,to sum up , i want to know about design strategies for complex php applications such as magento .
Maybe the design strategies which i know (mvc , frameworks ci cake ...)can handle more complex applications than i think ..
if there are some mistakes in my questions please feel free to correct them , sorry for my inadequate english ..

I believe that part of your problem may lie in the fact that creating enterprise applications is a problem in any language, and the design patterns that can implemented are actually language agnostic.
I would strongly recommend that you familiarize yourself with Patterns of Enterprise Application Architecture by Martin Fowler. This is the seminal work for any other books that you may later pick up that cover the same concepts in a language specific format, and if you want to truly understand what is required to create robust, scalable applications on the web then you'll need to familiarize yourself with this book.
A very common and popular design strategy with web applications right now is the Model-View-Controller paradigm. This has to do completely with separation of concerns in your application so that you aren't mingling database access code with html output.
For a pretty good treatment of the topic I would suggest that you look here (Zend Framework specific but it covers the general topic well) and here for a discussion about Models specifically. Or if you want to look at a more generalized PHP MVC tutorial, Rasmus Lerdorf has one.
In addition to this (and again you can learn this from PofEAA by Martin Fowler) you will need to learn about Object-Relational-Mapping what the strengths and weaknesses are of the various design patterns.
Unfortunately there are many good ways to do things depending on your needs, but for every good way there are about a zillion horribly wrong ways to them.

Which frameworks have you examined? Examine symfony, Zend Framework, and CakePHP if you haven't already. And by examine, I mean actually write medium-sized applications using these frameworks. Simply reading code is often not enough to get a grasp of how it works. You often have to actually use it and try to modify it.
You may also want to check out the book PHP 5 Objects Patterns and Practice for some ideas of design strategies that you may apply to your application. You may also learn quite a bit by studying frameworks written in other languages. The designers of many of the PHP frameworks were heavily inspired by Ruby on Rails, for example.

It is indeed very abstract question and "very complex" is not very specific. When I hear people talking about "complex" applications I associate it with
a) Someone is using a complex architecture for a simple Problem. E.g. by using every design pattern and framework that sounded cool.
b) Someone tried to squish tons of entirely different usecase into a historically grown application, creating and using proprietary and undocumented interfaces and couple everything as tightly together as possible. Unfortunately CAN build huge applications with a bad design strategy and that is what makes them complex.
c) Legacy systems and Legacy System integration (ok see b)
Magento may be a big application, but the underlying Framework is still the Zend Framework, mainly its MVC part. So reading the Zend_Framework documentation will help you a lot to understand Magentos architecture as well (I won't recommend it the other way around by trying to dig into the Zend Framework through the Magento source).
I would recommend to actually start building a bigger application with one of the MVC frameworks yourself, because that is the best way to learn the architecture and its benefits and where the limits are.

If you haven't already, you should look into Object-Oriented Programming. There is a really great tutorial about that here. I think this is perhaps the most important thing that big web apps do that isn't necessarily intuitive to the amateur (myself included). The trick in MVC frameworks like Code Igniter is to build a series of classes (or objects) as either models or libraries.

Well, even if your question just all about PHP... If you handle your static content like images with PHP it will result poor performance no matter using you MVC or not. You should use front end like nginx for such things.
look at http://highscalability.com/ real stories from real life!
Also note NoSQL.

Catalog of Patterns of Enterprise Application Architecture
.

I tried to understand your problem and found that magento architecture is very powerfull but complicated. I got a solution by Zendfox, It is a web application framework, suitable for small to huge application development. It has very cute application architecture that can be manage very easily. It also has module developer to create custom modules for zendfox within few minutes wizard based tool.
So take a look at: http://www.zendfox.com

Related

Scalable app design in php

I'm a beginner php developer who is trying to build a social network for my school students. Knowing that the school has over 1000 students who are already active, I must have a plan of expanding / scale the code that I write.
Earlier it was just the LAMP Stack, now the modern web development is way more than that as I see, I'm truly kind of lost in what technologies to use and how to incorporate them to build a scalable app. I'm hoping to divide this application into 3 layers.
Application layer (phalcon,reddis,apache,php)[mvc api centric]
Database layer(mysql)
UI layer - (html/css/js/)
This is where i need help, is this design approach good for a scalable app ? where can i improve ? any explanations, links for further reading will be a highly appreciated.
Welcome to SO. I cannot think of a particular reference guide to direct you to (although the PHP manual is a good place if you end up stuck with how to do something specific). I would suggest reading a bit of several results when you search "Getting started with MVC in PHP" and noting what they agree on. That said, take a look at what I say below (and then ignore it as much as you please ;) ).
Firstly, you are wiser than many in sorting out a scalable design before launching into the project...
I'm excited to see Phalcon in your list there already. However, as DevDonkey suggested, start with something simpler first (Phalcon is very powerful but to really get to grips with it you need a good grasp of PHP, particularly object-orientated programming).
If you are completely new to PHP...
... try building a small app (products table, view/add/edit/delete functionality) and learn the beginnings of the language that way, as this answer suggests. Things will go wrong and you'll discover lots of headaches when you want to change one feature and it affects everything else but that will help you to understand the importance of...
MVC design
From your question I can see you have at least heard of this. This is really where the layers of your application lie:
Model - interactions with the database (retrieving/editing data) are handled through this. So you could have a MYSQL database and then your models provide a nice interface to interact with the data (generally you have one model for each table).
View - this is the last layer, what the user sees. So you will make use of your html/css/js knowledge here. On this topic, unless you really want to do your own css consider using a CSS Framework such as Bootstrap. It will really help speed up making your site look good and there are loads of free templates out there to use with it.
Controller - this is the application logic. The controllers request/manipulate data through the models and then decide what to send to the views for rendering.
Use a framework?
Using a good framework can make your application more reliable and quicker to build. But using a framework without understanding it will be frustrating, slow and possibly result in worse code than if you didn't use one to begin with (as you employ hacks to get around the pieces of the framework you don't understand). My current favourite is Phalcon but as a relative beginner to PHP I would suggest something more like CakePHP although both Laravel and Symfony are also popular.
Summary
Start small, learn, test ideas out and then build up to a bigger project.
Get comfortable using PHP (including OOP style) before using a framework.
Use an MVC framework
The layers you laid out in your question are good, but I would split it slightly differently (considering that MVC is the 3 layers)
Application Layer - controllers, written in PHP, handles logic/manipulation, often the biggest layer
Database Layer - models, written in PHP, you will also need a database which could be in your favourite database language - MySQL ;)
UI Layer - views, possibly written in PHP (depending on the framework) but also HTML, CSS and JS as well as well as a templating language if you wish (e.g. Twig or Volt), essentially a way to make the response from the controller nice for a human
First Project (for CakePHP)
This blog tutorial is a good place to start if you decide to use CakePHP.
Getting started with Phalcon
Phalcon is more powerful/verstile, but to get started with it I feel you have to be a better PHP developer than you do to get started with something like CakePHP. Take your time to understand each new concept with Phalcon, particularly Dependency Injection.
Even having used CakePHP for the past 2 years and being familiar with MVC patterns and PHP, I still worked my way through all 7 of the tutorials in Phalcon.
Having said this, my favourite thing about Phalcon is that it is highly decoupled - so it is fairly easy (after a while) to replace bits of it with your own extensions if it doesn't quite do what you want.
Note about Phalcon: It is not as popular as many other frameworks (although popularity is growing) and so you may have to spend some time digging around when you get stuck. However, the docs are improving all the time and the forum is very active. Unfortunately the number answering questions about it on StackOverflow is still small compared to many other frameworks.

Architecture more suitable for web apps than MVC?

I've been learning Zend and its MVC application structure for my new job, and found that working with it just bothered me for reasons I couldn't quite put my finger on. Then during the course of my studies I came across articles such as MVC: No Silver Bullet and this podcast on the topic of MVC and web applications. The guy in the podcast made a very good case against MVC as a web application architecture and nailed a lot of what was bugging me on the head.
However, the question remains, if MVC isn't really a good fit for web applications, what is?
It all depends on your coding style. Here's the secret: It is impossible to write classical MVC in PHP.
Any framework which claims you can is lying to you. The reality is that frameworks themselves cannot even implement MVC -- your code can. But that's not as good a marketing pitch, I guess.
To implement a classical MVC it would require for you to have persistent Models to begin with. Additionally, Model should inform View about the changes (observer pattern), which too is impossible in your vanilla PHP page (you can do something close to classical MVC, if you use sockets, but that's impractical for real website).
In web development you actually have 4 other MVC-inspired solutions:
Model2 MVC: View is requesting data from the Model and then deciding how to render it and which templates to use. Controller is responsible for changing the state of both View and Model.
MVVM: Controller is swapped out for a ViewModel, which is responsible for the translation between View's expectations and Models's logic. View requests data from controller, which translates the request so that Model can understand it.
Most often you would use this when you have no control over either views or the model layer.
MVP (what php frameworks call "MVC"): Presenter requests information from Model, collects it, modifies it, and passes it to the passive View.
To explore this pattern, I would recommend for you begin with this publication. It will explain it in detail.
HMVC (or PAC): differs from Model2 with ability of a controller to execute sub-controllers. Each with own triad of M, V and C. You gain modularity and maintainability, but pay with some hit in performance.
Anyway. The bottom line is: you haven't really used MVC.
But if you are sick of all the MVC-like structures, you can look into:
event driven architectures
n-Tier architecture
And then there is always the DCI paradigm, but it has some issues when applied to PHP (you cannot cast to a class in PHP .. not without ugly hacks).
From my experience, the benefits you get from an MVC architecture far outweighs its costs and apparent overhead when developing for the web.
For someone starting out with a complex MVC framework, it can be a little daunting to make the extra effort of separating the three layers, and getting a good feel as to what belongs where (some things are obvious, others can be quite border-line and tend to be good topics of discussion). I think this cost pays for itself in the long run, especially if you're expecting your application to grow or to be maintained over a reasonable period of time.
I've had situations where the cost of creating a new API to allow other clients to connect to an existing web application was extremely low, due to good separation of the layers: the business logic wasn't at all connected to the presentation, so it was cake.
In the current MVC framework eco-system I believe your mileage may vary greatly, since the principles are common, but there are alot of differences between, for instance, Zend, Django, RoR and SpringMVC.
If there are truly other good alternatives to this paradigm out there... I'm quite interested in the answers!
Sorry for the slight wall of text!
I think it would depend on what you're trying to do, personally. Magenta uses MVC pretty successfully, and it makes it fairly easy to add new functionality or modify existing.
Of course if you're trying to make something fairly simple, going with an MVC architecture could be overkill.
It's all preference. I have worked with old structures like XTemplates and Smarty and have now moved on to Codeigniter and Kohona. I like them very much and they work very well for everything I do on the web. For phone applications I can set up controllers for the functions that are needed to do it's data pulls as well. Working both in the Linux world and Windows World, for building ASP.NET Web Sites I don't see other way of building websites beside using MVC. Web Applications Projects in Visual Studio are still used but I prefer not to anymore. MVC Projects via Visual Studio is so easy to use and set up. You can right click on your controller methods and create views automatically. In every structure there is a good and bad but it's up to the developer to use whatever meets their needs.

When are frameworks used in PHP or any other language?

When do we have the necessity of using the frameworks in PHP. I have heard about zend framework and symfony. I have read about them but still didn't understand that exact point why we use the frameworks in software development.
Frameworks often provide you the boiler plate code that you would have to otherwise use, such as: Session Management, Templating, Database access etc etc.
In terms of proper software engineering, using these frameworks tend to promote proper design with patterns such as MVC(Model, View, Controller). By using this pattern, you can increase code reusability, separation of concern, etc.
By utilizing the database access that these frameworks provide you, you can write efficient models that can interact with the database while promoting code reusability. Another thing to consider with databases is security, such as SQL injections. Most frameworks now-a-days will automatically protect you against these type of attacks. If you were to write your own code(without a framework), you could end up leaving some sort of query wide open for sql injections.
For controllers, these frameworks tend to provide some sort of URL mapping. An example might be domain.com/posts/edit/5. The framework will then parse this url, and call the controller "posts", method "edit", and pass in the id 5. As you can see, if you weren't using a framework, you'd have to write all this code yourself, which would increase the amount of time coding and more chance of mistakes.
For the views, often times templating systems are in place to help reduce the amount of html, css, javascript etc you write. Not only that, they also provide structure for your views. In other words, they help you put your views in directories/locations that make logical sense(in terms of reusability, ease of use, etc).
Summary:
Because of all these features, you can see that not only will this reduce the amount of error/bugs in your system, but also decrease the amount of developing time.
Of course there are downsides to everything, and this is no exception. The main problem is the learning curve of learning these new frameworks. Often times frameworks provide so much that you have to learn all these new features just to get up and running.
Some popular PHP frameworks:
CodeIgniter
CakePHP
Zend Framework
Symfony
Getting Started Guides:
CodeIgniter
CakePHP
Zend Framework
Symfony
In terms of never using a framework before and wanting to get started, I'd highly suggest CodeIgniter. The learning curve is no where near as high as the other ones, however provide enough boiler-plate code and features that it should get you up and running.
If you want to do your own research(which you should!), here is a comparision table of all popular PHP frameworks:
http://en.wikipedia.org/wiki/Comparison_of_web_application_frameworks#PHP_2
Frameworks are usually used to accelerate the development of a project. Imagine having a bunch of extra new functions to help you with things like logins, page generation, database management, etc.. on top of the basic php functions. Basically Frameworks are made to save time and stop reinventing the wheel every time you make a new site/etc...
I have used CodeIgniter to build a few website and web applications.
The main advantage is that you get many pre-built components such as database abstraction, sessions management, email sending tools, and so on in one consistent interface and one package, so you don't have to spend time looking for these components that may or may not work well together.
Each framework has a different feel to it, a learning curve, limitations, you need to try them out and get a feel for it.
Many years ago, I was using Macromedia's Dreamweaver with a 3rd party extension to build my web applications. Since that time, Dreamweaver is owned by Adobe and the 3rd party extension is off the market, so I have legacy code with no support. For me, being messed around my market forces gave me the incentive to seek out a framework that was "desktop independent".
Summary: As a web developer, the primary benefits of a framework are ease of use, reusable components and greater control over rate of obsolescence.
Another point not made (i think) is that regardless of framework, but assuming it is well made, mature and has a community is that it has a structure that is imposed on the developers, whether it be MVC or REST, that future developers can quickly get to grips with when extending or maintaining the codebase.
So the naming convention for classes and objects & structures, the structure of the files and directories , the location of third party code, the templating system, localisation/internationalisation etc. in a well designed (and mature) framework will be well defined and accessible through third party or community maintained documentation.

Does using an MVC approach to (PHP-based) web applications necessarily mean that it will be more scalable than other approaches?

From what I understand MVC frameworks are pretty hefty (Zend,Cake,CodeIngniter), so it almost seems contrary to talk about scalability and suggest using MVC.
Zend, Cake, CodeIgniter... they all come with a bunch of stuff you don't need. A basic MVC framework is simple, and does not need many files to work.
Also, applications built upon a MVC structure are no more or less scalable than other approaches may be, but may be more organized. It's subjective.
Although the point of MVC is simple - having data access (Model), GUI creation (View) and flow control (Controller) as separate components - it can be implemented in very different ways.
From a purely technical viewpoint, MVC has very little to do with computational scalability. It's more about the human factor and having a way to organize huge piles of code into meaningful sub-units that are much easier to understand.
I work with codeigniter every day and compared to the others mentioned it's very light weight.
You have to consider that you will likely not use a lot of the functionality provided in the largest frameworks so just because the files are there, it's not necessarily heavy unless you include lots of the files in your actual executing code.
You can help make your own code scalable in these situations by considering when and when not to use functionality provided by the framework. Learn PHPs built in functions well as they will almost always be faster than anything in a framework (because they are written in C).
A good framework should really just help you structure you code and give you extra functionality when you ask for it, not lumber your application with a lot of extra processing.

Rewriting a php app in CakePHP

So, I'm very tempted to rewrite my application using a php framework, as I think it'll make it easier for folks to get involved, as well as improving the design of the app.
CakePHP looks like the best of the PHP web frameworks. Does anyone have any experiences of it? What are the caveats I should consider going from handcoded PHP to using a framework?
Not depending on the framework you'll chose, the first thing you have to know is that :
it'll take some time for you to know it
you'll do crapping things, during that time ^^
so, take into account the fact it'll take some time before you are fully operational :-)
I think those points are the most under-estimated points : using a framework takes not much time... Using it well and to the full extend of its abilities takes... Well, a couple of months, maybe... Which means, when you are at the end of your project you'll say "I should rewrite that with all the stuff I learned while re-writing it the first time" :-D
What it means is : learn what the framework can do, learn how to use it, and use it for a while on small applications, before starting rewriting your big one !
Then, there is probably no "best framework" : one framework may be very well suited for one project, and another one may be best for a second, different project.
I've never worked with CakePHP ; I really like Zend Framework. But that is a personal opinion, and O know people who really like symfony ; they are not wrong : symfony is great -- and I am not wrong either ^^
Still, we sometimes agree on some things ; like the fact that Doctrine (default ORM layer of symfony) is really great stuff, and we tend to use in both symfony and ZF-based projects...
If motivated, you can take a look at many posts on SO, about frameworks... Here are a couple of those :
What PHP framework would you choose for a new application and why?
Is Symfony a good framework to learn?
What, in your mind, is the best PHP MVC framework?
Is Symfony a better choice than Zend for a web development shop (10+) because it is a full stack framework?
Best PHP framework for an experienced PHP developer?
Good luck with those ^^
("Which framework" is a quite pationnating -- and subjective -- question ^^ )
CakePHP has it's good parts but there is no "best" framework. Here's a thread with some clues about what's good in most popular PHP frameworks.
If you never used MVC frameworks before (and Cake is MVC framework) I think you should first familiaze yourself with MVC architecture.
"CakePHP looks like the best of the PHP
web frameworks."
This is subjective. You should compare the pros/cons of other PHP frameworks that will suit your needs.
These posts may help you:
https://stackoverflow.com/questions/2648/what-php-framework-would-you-choose-for-a-new-application-and-why
Why do I need to use a popular framework?
https://stackoverflow.com/questions/249984/php-framework-decision-analysis-paralysis
FWIW, I used it for a time when I was doing some php development. I found it easy to use, and the rapid development aspect was great, and I would imagine has just gotten better in the last 3 years. There is a ton of help in the irc channel, and the documentation is good. I didn't stick around in PHP long enough to become an expert. However, I was just starting out as a programmer then, and ran into Larry Masters (the creator, aka phpnut) and he was just a good person to talk to about design principles, and an all around nice guy. Then again you don't have to be a super nice guy to write a good web framework (I'm looking at you DHH).
Most of the frameworks "bind" you to them, meaning you have to do things their way. If you want to do something they weren't designed to do, you usually have to hack it. For example, how many PHP frameworks currently support Facebook Connect?
Personally I prefer "frameworks" that you can use only the parts you want. Zend is like this, Doctrine and Propel are ORM that are designed to be used with other code. For example, the Symfony framework can use either.
Finally, I haven't found a popular PHP framework that scales well.
I have used cakephp for a couple of projects. From the moment I learned it I have never written php again without it (unless is fun code in which I want to try some new stuff, or learn other design ideas away from MVC). As mentioned, learning it will take some time. How much time it takes really depends on your background. If you have used another MVC framework for a web scripting language then you will learn it really quick; RoR developers will pick it up within hours/days. If you don't have experience with MVC frameworks then it might take you a little bit, but it will really save you time later on the road (including in that project you start with).
Until today, I still learn new things about CakePHP every time I start a new project on it, although I do dig into a lot of its source code (you definitely don't need to do this, documentation and help boards are more than enough).
I definitely recommend you looking into it. It will save you a lot of time and get your head thinking in a different way (if you are not used to the MVC).
Best of luck.
CakePHP's convention over configuration approach has a few advantages once you learn them:
it helps to keep you code organised and understandable
makes it easier for multiple developers to collaborate on the same application
makes it possible for developers to understand other developers' applications
You have two main options when rewriting a legacy application in CakePHP:
change the database schema to reflect the conventions - resulting in less code
code your models to interface with the legacy database - this book goes into all the details
Either way, once you have done the above, it's pretty much plain sailing, and a good learning experience.

Categories