MVC Frameworks for building PHP Web Applications


Published on

Hello and welcome to VForce Infotech Training on MVC Frameworks for Building PHP Web Applications. In this course we are going to look at PHP, and specifically we are going to look into some of the main frameworks that you can use to develop PHP applications. We are going to look at some of the patterns, most specifically the Model/View/ Controller pattern, which is at the root of every one of these frameworks. And then we are going to dive in and take a look at the Zend framework. We are going to also look at the SymFony framework. We will look at the lightweight CodeIgniter framework. And lastly will look at CakePHP. This course was designed to introduce you to some of the more popular PHP frameworks.

Hopefully you can choose what might be right for you and your project. I'll show you how to get started in each of them and you can take it from there. So let's get started.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

MVC Frameworks for building PHP Web Applications

  1. 1. MVC Frameworks for building PHP Web Applications Training at Vforce Infotech BY PRUDHVI REDDY
  2. 2. Main Frameworks
  3. 3. The MVC Process  Model view controller pattern  Which is at root of Every one of these frameworks
  4. 4. Why Framework?
  5. 5. Why use a Framework?
  6. 6. Do we always need a framework?
  7. 7. Model / View / Controller
  8. 8. The MVC Process
  9. 9. Framework Concepts
  10. 10. Deciding which Framework to use
  11. 11. ZEND
  12. 12. Symfony
  13. 13. CodeIgniter
  14. 14. CakePHP
  15. 15. ZEND 1.11 and 2.0 beta
  16. 16. Structure
  17. 17. ZEND 2.0
  18. 18. Installation
  19. 19. Code Organization
  20. 20. Issues
  21. 21. ZEND Components   MVC, Tooling  RAD --- when we Zend Tools or ZF command line tool  Database (no:of classes): profile how ur queries are working other interaction  Internationalization and localization  Authentication , Authorization, and Session management  Mails, Formats, and Search  Core Infrastructure ( debugging, cache, loading )  Recommend (  Tons of classes in Zend Library
  22. 22. Zend Flow and Architecture
  23. 23. Zend Framework Flow
  24. 24. Using Zend Framework   http://www.  Basic core library of the Zend framework  http://www. (server)(community edition ‘free’)  Download only (we only need this)  Copy this to your WAMP, MAMP directory on your system and download the files from frameworks ( In MAMP use command line)  Now find PHP.INI file in WAMP or MAMP
  25. 25. Configuring PHP.INI file  Inside the php.ini file you're going to look for the include_path. This variable stores all the paths where PHP looks for PHP classes. We want to makesure to add the Zend framework library to this path. Each of the paths isseparated with a colon. You can see the first path is a dot, which means it'sgoing to include the current directory of your PHP file. You notice we also have a php5.3.6 library. Now we need to add our Zend framework, and the framework specifically is located at Applications/MAMP in the svn directory, /zendframework/trunk/library.  Keep in mind if you downloaded this, you are not going to have the svndirectory, nor are you going to have the trunk. When you're done, you're goingto want to make sure to restart your PHP server so that these changes will take effect. As you can see, we have a few choices as to how we can download and install the Zend framework. We can download the framework to install on our own PHP server, either in a scaled- down minimal zip or with all the bells and whistles. Conversely, you can use subversion and go to the repository directly. Either way, we'll want to add the library to our php include_path and optionally prepare the Zend tool for some command-line assistance getting started.
  26. 26. The Development Environment  One advantage of using the Zend framework is the integrated tool set, which includes the Zend studio IDE. While it's by no means required that you use this development environment, it does contain a number of tools that can make it helpful in working with applications in the Zend framework. Zend studio is Eclipse-based. It's currently at version 9. You'll notice when you start up, you get a couple of prompts. One will prompt you to add a toolbar to your browser. If you like to do this, this can help for debugging, profiling, and quickly jumping into the studio, as well as searching the PHP and Zend docs from your browser.  Additionally, you'll be prompted if it detects a Zend server or not. You don't have to use Zend server with Zend studio. However, when you go to do debugging, it can be a lot easier to do so. Let's look at some of the things you can do in here. You'll see when you first get started, you get this Welcome screen. There are a number of add-ons you can plug in that will give you some help in terms of syncing with Git, using subversion, RSS feeds, linking to Amazon web services. There are all kinds of things that can be helpful with your development.  You can create new Zend projects by simply right-clicking and going to create a new PHP project. You can give it a name, and notice it prompts you what version of the Zend framework you want to use. Once it's created, you can use some of the other tools to launch wizards to ease some of the other development tasks. For example, if I want to create a new controller, I can right-click on the controller folder and go to New > Zend Framework Item. I can choose which type of controller I want to create or some of the other Zend modules as well.  I can do some customization in addition to naming it. I can determine where I wanted to reside. It will give me a short preview of what's going to be generated and when I click Finish, it will generate the file for me. Notice how it includes the action.php file and makes sure that this controller extends that class. Finally, when you're working inside of the Zend environment, you're going to getsome nice code hinting. Note how these different elements pop up. It will give me options of what's available in specific objects, what the arguments are that can be taken, and what type of data will get returned.  So all in all, Zend Studio can provide a helpful environment. It's not required, but it might be something worth considering to shorten your development time. It can ease your work in a collaborative environment, and it can ease deployment. That all said, to give a fair comparison, we are going to build our Zend application and all of our other applications using a simple text editor.
  27. 27. Screenshots of creating new php project, launch other development tasks like Controller
  28. 28.  While this is largely important, the true advantage of the Zend framework is the depth and breadth of the libraries available to you. The Zend Framework Reference Guide is a great starting point to see some of the tools available, in alphabetical order. You can see Zend has an ACL, or Access Control List, methodology that allows you to find different groups and who can access what parts of an application. It also has an authorization functionality. It has the ability to integrate with barcodes. You can implement caching that allows you to have intelligent caching and manage all of the data in your cache, both between your database and in caching templates themselves. You can implement CAPTCHA. You can access cloud data. You can configure all kinds of different things directly and programmatically through the API. There's a number of different features in there to handle dates and currencies and formatting different type of strings.  Dojo is a really powerful tool that you can use to do all kinds of view helpers and decorators and elements. The Dojo application itself has its own web site at, and if you go to the Features, you can see all the things that are available to you. There's a lot of tools for desktop that allow you to integrate different elements into your forms. You can create really nice interfaces right out of the box, and because it integrates with Zend, it's really just a matter of calling on methods of different classes.  It can enhance your mobile application development using HTML5 and CSS3. It's a really beneficial way for creating graphs and tools and all kinds of UI elements that you wouldn't otherwise have available to you. Zend also has the Gdata services, which allow you to talk to Google, and you can integrate with Google Calendars, with Google Docs, with YouTube data, Picasa, all kinds of different things that allow you to pull this functionality into your own applications.  One last thing to look at is to keep an eye on the Zend 2.0.0 beta. 2.0.0promises to be a much more accessible version of Zend, and in addition, it will perform well, and we can assume that they're going to add a number of different features and libraries along with it. Because of the contribution of so many large companies to the Zend framework, it really does have a lot of tools for developers. AMF, Dojo, Google, and other services, authorization email, and other well-tested APIs can give you such a head start in so many aspects of your coding.  So through these movies you can get a pretty good picture of the Zend framework. Some complexity in terms of getting started, but loads of libraries, and flexibility in terms of how you actually implement your applications. If you think this is a framework worth checking out, I definitely recommend delving deeper into the API, and most importantly, diving in with your own application and seeing how it goes.
  29. 29. Intro to Sympony2  Symfony is one of the more popular PHP frameworks out there, and for good reason. They have a solid code base, a decent community, and good documentation. The framework itself has a lot to offer. It's flexible, uses a decent and modular architecture, it has a pretty deep toolbox, and some nice rapid application development features.
  30. 30. Installation
  31. 31. Code Organization
  32. 32. ISSUES
  33. 33. Symfony components   A key consideration of a framework is what components are available, which isreally to say what pre-tested code that I might want to include in my application is available to me. In the case of Symphony, there are a few key components, but because of the new bundle architecture, many more third- party components are available as well. Many of Symfony's components are for the framework itself, components for handling data such as cookies and generating client code, Doctrine 2 for data handling, the controller and routing classes, and of course the template engine of Twig. There are also a number of components that will help you with development, for testing, for handling the services, that is, the service container, the forms and validation API. The application aesthetic is for handling assets like JavaScript and CSS libraries.  There are also tools for logging, security, and caching. Additionally, you canuse SwiftMail, which is good for sending emails, and there is also a number ofclasses for translation and internationalization. There's a web site called At this web site you can find a number of bundles written by third-party developers that can help you shortcut a lot of your development process. So Symfony2 contains a number of libraries and constructs for the main aspects you'll need for your PHP development: the MVC, testing and logging, building forms, handling authentication, and all of that kind of stuff.  If you wish to implement AJAX, you can simply use the library of your choice,jQuery or Dojo or any other library you might want to use. Another thing to remember is that because of the bundle architecture, there is always the possibility someone is already created a bundle to do what you need to do.
  34. 34. Symphony flow and architecture  Core Structure
  35. 35. The Model
  36. 36. The View
  37. 37. The Controller (and Routing)
  38. 38. Download  (Zip or .tgz)   Download and extract and place the file in root folder directory  Now go to localhost:port/symfony/web/config.php to check configrations and installation
  39. 39. The View : Twig and templates  Symfony2 handles the view portion of applications through the use of templates. These templates can be written in PHP directly or you can write them using Twig. Twig is a template engine written for PHP that's also managed by Sensio Labs, who manage the Symfony project.  Why do we use Twig? There is a lot of good reasons to use Twig. The main one is that it offers shorthand notation for a lot of tasks, including outputting variables and looping through queries. Additionally, it ends up being rendered into PHP and that PHP can be cached. So we really don't lose any performance. We actually gain some. Twig also supports some advanced concepts like template inheritance that can be really helpful in our application development. And finally, because Twig is a different language that we're using for the View layer, it's less likely that we're going to be tempted to pull any sort of logic or anything into that layer. So it keeps our partitions clean.  Symfony has a really comprehensive view layer, using templates. If you prefer, you could write these templates in PHP, but Symfony includes the Twig template engine, which can make our development a little faster and feature some powerful things like template inheritance. All in all, Symfony offers a fairly simple, yet powerful way of implementing your view layer.
  40. 40. The Model: Doctrine 2  No matter what framework you use, a large amount of application design and tasks ultimately involves working with the data layer. Symfony2 comes bundled with the Doctrine Object Relational Mapping, or ORM, library. While Symfony doesn't require using Doctrine, it's definitely a tool worth learning. Let's have a look at how we can handle data with Symfony and Doctrine. The first thing we need to do for our database to work is go back to the config.php in the web root of our Symfony directory
  41. 41. Overview  Symfony 2 is a fairly comprehensive framework. We've seen how Symfony uses a number of constructs and sub technologies to manage different aspects, including YAML, Doctrine and Twig. For some, these are its strengths, shorthand methods to make development time faster. For others, they add the burden of learning technologies and additional overhead. With this version of Symfony, they have design flexibility into it, allowing you to use these aspects or not. In addition to the basics we've covered, there are number of things to learn in order to get started working with Symfony. There are a couple of places to look. The Symfony book can provide a good jumping point for learning some of the fundamentals, for getting in and learning more about routing and the controller and handling dynamic routing and Rest friendly URLs. You can use the templates to create helpers, and there's all the special tags. There's escaping and there's a number of things you can do for JavaScript integration. Symfony has a built-in security construct that you can use for authentication, and it can handle some client management as well.  Just like with other aspects of Symfony, you can use different ways to implement these technologies. There are varieties of caching mechanisms you can use. These include HTTP caching and Edge Side includes, and these will be key to making sure your applications can perform well. Locales and translations can help with internationalization. Another thing to look into is the Service Container. The Service Container is a great way to manage the objects inside your application. It uses a concept called dependency injection, and it's one of the key selling points of Symfony 2. In addition to the book, there is the Cookbook and inside the Symfony Cookbook, there are a number of different examples, broken down into different categories, that can help you learn different aspects of working with the Symfony application.  The Symfony framework has evolved in version 2 to become a powerful and more flexible development framework. In these movies, we have introduced the concepts that are important to understanding Symfony 2, including how to implement routing and controllers, creating views using templates, and managing data with Doctrine. There is much more to learn, and I definitely recommend visiting the Symfony Book and Cookbook at
  42. 42. CodeIgniter (CI)  CodeIgniter is considered to be one of the lighter and more flexible frameworks of those currently available for PHP. It has a pretty decent following, good documentations, and a solid community to work with. PHP is backed by EllisLab.
  43. 43. Installation
  44. 44. Code Organization
  45. 45. ISSUES
  46. 46. CodeIgniter Classes  http://codeigniter/user_guide/  CodeIgniter comes with a host of classes that can jumpstart your development and enable you to do a number of different programmatic tasks. The best way to find these is to look in the user guide at If you open the table of contents, you can see there are number of different classes and a number of different documents available for your help. The two columns on the right contain the key classes that will be usable by you, and they are broken into three major classes. Helpers, used to be called plug- ins, these are procedural. You use a special load helper method to load them, and then you can call on a number of functions.  These can help you with CAPTCHA, They can help you build URLs and manipulate data, including dates, numbers, and strings. You can work with directories and files. You can send emails, write URLs. You can even output emoticons using this Smiley Helper. The driver class allows you to access different types of data, including database. Yu can work with your cache this way and you can also include different JavaScript classes using the JavaScript class. The library classes contain fully functional classes that you can pull in and use for some of the more heavy crunching within your application. This can include encryption, interacting with language elements for internationalization, working with browser detection using the user agent class, XML, zip files, handling track-backs, image manipulation, and there's even a shopping cart built-in.  As you can see, in addition to the MVC pattern enabling, CodeIgnitor contains three types of classes that will help your application development: drivers to load databases and JavaScript libraries like jQuery, helper classes to perform simple helper tasks, and library classes that contain all kinds of supporting functionality. The documentation is also a good source for you and one of the benefits to using CodeIgniter.
  47. 47. Core Structure
  48. 48. The Model
  49. 49. The View
  50. 50. The Controller
  51. 51. Request Route
  52. 52. Downloading and setup  Download it from  If using GIT Download it  Put your files in Root folder  Before you start if you want to change [‘base_url’] = ‘’; (leave it as it is if using localhost)
  53. 53. Overview    We've seen so far how to plug a basic application into CodeIgniter and use it to help organize our code and hopefully shorten our development time. One of the advantages of CodeIgniter being an active open source project is that it comes with a lot of stuff out of the box: caching, profiling, and a slew of libraries to jumpstart your application development. One of the key advantages of CodeIgniter is a large community. In addition to having fairly decent docs, there's also a wiki page where you can find a lot of information including FAQs and a number of tutorials, and a decent forum that's quite active. You can see the last post was about two minutes ago.  I briefly covered routing and URL mapping inside of CodeIgniter, but this is a key aspect to using CI, and it can be great when you have an ecommerce or adrilldown type of web site. I always recommend, when you're getting to know a new environment, to go through the tutorials in the getting started. In CodeIgniter they have a number of examples inside of this tutorial in how to create a news application, and it really shows you how everything works inside of the environment. Additionally, all of this information will help you determine what classes are available and how you can do things like manipulate images, use jQuery library, create captchas, and even create a simple shopping cart application.  If you don't see a feature you need, there is actually uservoice web site for CodeIgniter. This allows you to submit ideas that you think you'd like to see in the next version of CodeIgniter. We've now explored all the basics of the CodeIgniter framework. Its advantages are that it's easy to get started. It's flexible. However, those same traits can also make it so that no two CI applications are identical. CI has a very large and active community. If you're interested in learning more, the web site will offer links to the forums, tutorials, the wiki, and other documentation.
  54. 54. CakePHP  Intro to CakePHP 2.0  CakePHP is another of the full-stack frameworks. Its aims are fast adoption and development speed, specifically writing as much functionality as possible with as little code as possible. Developers who use it claim this to be true and swear they've been saved from repetitive stress disorder because of it. CakePHP is often criticized for lack of flexibility beyond the basics, performance, and the usual criticism of full-stack architectures. Cake was created in 2006, and it's backed by a non-profit, Cake Foundation. It was created by a Polish developer who wanted to emulate Ruby on Rails. A key goal is not to reinvent the wheel every time, which is also shared by Ruby on Rails.  Cake requires PHP 5.2.6. It has a lot of features that I call automagical, which means if you name things correctly it'll end up building a bunch of code sets behind it that you don't need to actually write. Scaffolding and the creating of Create, Read, Update, Delete methodologies is quite automated. There is a command-line utility called BAKE that you can use to generate code. There are a number of components and features built-in the CakePHP that you can use for doing all kinds of tasks.  Cake has recently released 2.0, and with it they've re-architected a lot of their structure. One of the key re- architectures is the Request and Response mechanism. Exception handling and error handling has been improved. They've added a lot more native features, including jsonencode, PHP Data Objects, and SPL. They've improved the internationalization features. One of the criticisms of CakePHP previously was its performance, and they've done a lot in this version to improve performance, including adding lazy loading and intelligent caching. They added a new authorization system for handling authentication of users.  And finally, they've dropped support for PHP 4, which means there's not a lot of extra code inside of the code base to check for all these different things, whether they exist or not. CakePHP is a full-stack framework that aims to shorten developer time while maintaining flexibility. With the release of 2.0 in 2011, we're seeing better performance and a slew of new and improved features. Let's dive in and take a look at CakePHP and how it works.
  55. 55. In brief
  56. 56. Installation  Cakephp website or github  Can add config for URL mapping  Has an installer page for config database
  57. 57. Code Organization
  58. 58. ISSUES
  59. 59. CakePHP components  CakePHP divides up their class library by the ways in which the classes are used. There are a few different groupings of classes. Let's take a look at some of what's available to you out of the box in the CakePHP core library. If you go to the CakePHP book, you can see these different libraries are broken up by category on the left navigation. The first category is general-purpose classes. These are libraries that are available and reused throughout CakePHP. It includes collections, some global constants, and functions like internationalization features, timing, and object loading.  Components are for use inside of your controllers. This includes authentication and Access Control Lists, cookie session management, emails, the request handling pagination, and other security constructs. There are a number of helper classes available to you inside CakePHP. These can be used in your views and for creating views. You can use these helper classes to do caching, create forms, write HTML, import JavaScript, parse numbers and text in time, work with pagination, work with feeds, and also manage session information.  Behaviors are library classes that you can use in your model. There are ways that you can control your find methodologies using the containable class. You can use hierarchical data if you want, using the Tree. This also includes classes for translation and for managing Access Control List behaviors. And finally, there are the core libraries. These are simple utilities that can be used throughout your application, including the application library itself, the caching libraries, cleaning data, handling emails, working with files, using HTPP socket. There are number of different classes for handling how you name things, specifically the inflector class, using strings and handling XML.  The internationalization and localization classes are located in here. You also have logging the router and security classes available to you. And finally, because of the active CakePHP community, there are a number of third-party plug-ins available as well. CakePHP 2.0 includes a number of classes that can be used throughout your CakePHP applications. These can perform all kinds of tasks, from string parsing to emailing and authorizing users. We touched on the basics here, broken down by the different types of your application. Additionally, you can look to the CakePHP developer base to find third-party plug-ins that might be helpful to you.
  60. 60. Core structure
  61. 61. The Model, View, Controller  Let's take a look from a 10,000 view in how the CakePHP framework is laid out. This will help us hit the ground running when we actually get into writing our application. The first thing you'll notice when you download and install Cake is the directory structure. Here's how it's basically laid out. Everything in your application is going to be located in the app directory. This includesconfiguration information, The console will contain Bake, but there are someother console classes available to you as well. The controller will house your controllers. The view will hold all of your view templates. Model is where you're going to place all of your data models, and finally, the web root is where you're going to place any of the assets that you need inside of your application.  Additionally, you're going to have the library directory, or lib. This is going to contain the core Cake libraries. The vendors folder is where you're going to put any third-party tools, if you want to use different template engines orsomething like that. The plug-ins is where you can add any CakePHP plug-ins you have. When you're creating the data model you're simply going to create a file in the /app/model directory. This class is going to extend the AppModel class. Whatever you name the class will coordinate how it connects to the database. Specifically it will determine what the database table is that it connects to, and we'll look more into this in the movie about the data model.  That's pretty much all you need to do in Cake to create a class. This willhandle both the entity management as well as all the add, update, and deleteinformation to the database. Of course, if you want to add additional behavioror add additional functionality, you are welcome to do so, inside of that class.The View layer is typically written in templates that have a .ctp ending insideyour views folder. If you want, you can use a template engine like Twig orSmarty, but typically they are written straight in PHP.  There are four different types of view templates you can create. You can create layouts, and layouts are the skins or wrappers that you wrap around your application, or parts thereof. Views work with the controller, and these will be specific actions inside of your application. For example, one view that we're going to create is the form, and another view you might create is the Thank You page. Elements are little subapplications that will show up within your application. They can be as simple as displaying links, or they can be more complicated, like a forum.
  62. 62.  Helpers are functional little bits that can do all kinds of things to help build forms, add JavaScript functionality, or work with feeds. The controller handles the core routing and how everything works inside of the CakePHP application. Every application is going to have an App Controller main class. You can then create your own controllers and methods to customize things how you like. If you're working with the Create, Read, Update, Delete methodology, you're going to want to make sure that the name of your controller correlates to the name of your database table, which will also map to the model name. If you want, inside of your controller you can add additional models than the one that matches your controller name, using the uses variable. Templates are going to load automatically. Again this happens because of the naming conventions.  The controller defaults to have HTML form and session helpers. You can include different ones if you like or if you don't want to use some of these, you can specify which ones you want to use. You can generate all of these MVC elements using the command line if you prefer
  63. 63. CakePHP WORKFLOW
  64. 64. Using CakePHP  Download at– 2.0.5 and extract and place it in root folder
  65. 65. Overview  We built a very basic application using CakePHP and while it gave us a good idea of how the framework is organized, we really only scratched the surface of what you can do. In this movie we are going to discuss what else you can do with CakePHP, and hopefully guide you where to find it. One of the things we didn't do is use the Bake console application. Using Bake, you can generate your own code at the command line. You can see there's different options to set your database configuration, create models, views, controllers. You can create entire projects, fixtures, and even test cases. It's fairly straightforward how you specify what to do in the command line.  Every aspect of the MVC paradigm that we've looked at is much more customizable. Because of all the plug-ins, behaviors, helpers and library classes, you can add all kinds of functionality to enhance how your model behaves, how your view renders, and how your controller handles every request and response. Additionally the Cake request and Cake response classes allow customizable featuring of how you handle these aspects of your application. We looked at some of the libraries earlier, but there are ton of classes in here that can help you to do everything from build Access Control Lists, send emails, and create security paradigms.  To learn more, definitely check out There are lot of references there. They have a lot of movies you can use to watch tutorials. CakePHP also has a Google group, which can be really helpful if you need questions answered. Lastly, the Cake foundation is offering some classes, and that could be helpful. So overall CakePHP is a full-stack framework that includes tons of features for rapid application development. It has a large core library and an active community. The next step in learning any framework is just to dive in. You really don't know till you try it. So take a look at and see where it goes.