All frameworks discussed in this post are full stack web frameworks coded in PHP such as Code Igniter, Symfony, Kohana, Yii and CakePHP. Some (but definitely not all) of the points below may apply to micro frameworks such as FatFreeFramework, Limonade, Glue and Slim.
When should I use a framework?
Frameworks should be used when constructing web applications. Any application that involves a database, forms, sessions, cookies or a remote service (such as Twitter or Facebook) will benefit from being powered by a framework. There is no need to use a framework for a website that has only one or two pages, nor for command line utility scripts.
What features do frameworks provide?
- Database abstraction. Frameworks can abstract away SQL-vendor specific features. This allows you to change your SQL database without needing to rewrite any code. For example, switch from MySQL to Postgres or MSSQL. Some frameworks can also handle database relations automatically, which saves you having to write JOIN queries.
- Cache abstraction. Rather than using backend specific caching functions (such as apc_add and apc_fetch), you use a generic caching class which has support for multiple backends such as Memcache, APC and XCache.
- Form management. Symfony2 and CakePHP allow the developer to define forms as PHP code. The framework then handles all HTML widget generation, form validation and CSRF security automatically.
- Authentication. Most frameworks come with a generic user authentication module. They handle logging in and logging out, registration, session management and permissions. They are typically very easy to modify and add your own custom fields.
- Easy Debugging and Profiling. Kohana3 and Symfony2 come with debug toolbars. These allow you to inspect global variables, database queries, logs and load times in the current request.
- Internationalization. Built in translation frameworks let you easily create country specific versions of your website.
What benefits do I get from using a framework?
- Portability. Database and cache abstraction allows your application to be used on many different server setups. If your application is open source, more people will be able to install it.
- Shorter development time. Because your not writing the same boilerplate user authentication, database and form code, development time is shortened dramatically.
- Application security – Security features such as authentication and permissions are handled for you by the framework. Database inputs are automatically sanitised and most frameworks have cross site request forgery (CSRF) protection built in.
- Community supported – Frameworks have forums, mailing lists and IRC channels supporting them. If you encounter a problem with the framework, chances are someone else has and a fix has already been made.
- Plugins and modules. Members of the community release plugins and modules which you can download and use in your application. These would be things such as integration with 3rd party APIs (Facebook, Twitter) and features such as template engine integration.
- Enforces good coding standard. Most frameworks force you to follow the Model, View, Controller principle. This makes you (the developer) think about how your code will be structured before you write it, making it of higher quality.
- Future proof your application. Frameworks are well documented and tested. If another developer was brought in on the project, or you sold your project, the new developer would just need to read up on the framework documentation and then would understand the code. If a framework wasn’t being used then time and money would need to be put into training the new developer.
What framework should I use?
Everyone has their own opinion on which is the “best” framework. I recommend you have a look at all frameworks linked below, they each have different styles of structuring the application. If you have experience with Ruby On Rails then you may want to look at CakePHP and Trax. They both have ActiveRecord implementations and model themselves after Rails. QPHP will seem familiar for those with ASP.NET experience and Symfony2 will make people who know Java feel at home (Symfony2 overview). Also be sure to check out Kohana, Code Igniter, Yii, Lithium, Fuel, Alloy and Akelos before making your decision.