navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

MVC Frameworks for Building PHP Web Applications
Mark Todd

MVC Frameworks for Building PHP Web Applications

with Drew Falkman

 


In this course, author Drew Falkman tours four open-source PHP frameworks: Zend, Symfony, CakePHP, and CodeIgniter. The course covers basic framework concepts; explores the Model-View-Controller (MVC) pattern as a means of simplifying, organizing, and maintaining code; and details the ins and outs of developing basic applications in each of the four frameworks.
Topics include:
  • Why use a framework?
  • Exploring basic framework concepts
  • Comparing Zend, Symfony, CakePHP, and CodeIgniter
  • Downloading and installing each framework
  • Exploring MVC in each framework
  • Creating basic PHP projects

show more

author
Drew Falkman
subject
Developer, Web, Design Patterns, Web Development
software
PHP , Symfony , CakePHP , Zend Framework , CodeIgniter
level
Intermediate
duration
3h 35m
released
Feb 29, 2012

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:04Hello and welcome to MVC Frameworks for Building PHP Web Applications. In this
00:09course we are going to look at PHP, and specifically we are going to look into
00:12some of the main frameworks that you can use to develop PHP applications.
00:17We are going to look at some of the patterns, most specifically the Model/View/
00:21Controller pattern, which is at the root of every one of these frameworks. And
00:25then we are going to dive in and take a look at the Zend framework.
00:29We are going to also look at the SymFony framework. We will look at the
00:33lightweight CodeIgniter framework. And lastly will look at CakePHP. This course
00:38was designed to introduce you to some of the more popular PHP frameworks.
00:42Hopefully you can choose what might be right for you and your project. I'll show
00:46you how to get started in each of them and you can take it from there.
00:49So let's get started.
Collapse this transcript
What you should know
00:00In order to get started with this course, I'm assuming that you have a basic
00:03understanding of the PHP programming environment. If you don't, a great place to
00:08start is PHP with MySQL Essential Training, here in the lynda.com Online Trading Library.
00:14To take it even further, you can check out the related course PHP with MySQL
00:18Beyond the Basics, also here in the lynda.com Online Training Library. In
00:23addition, I'm assuming you have a basic understanding of the HTML environment,
00:27object-oriented programming, and a general idea of what design patterns are and
00:32what their purpose is in the programming environment.
00:35This course is designed to give you an overview of some PHP frameworks that make
00:39use of the MVC design pattern. If you don't have a PHP server set up on your
00:44local machine, there are few options for you. You can install each of the
00:48components separately or you can install packages like MAMP, WAMP, or XAMPP,
00:54depending on your platform of preference.
00:56Please note that you will need to configure the path to the PHP executable so
01:00that you can run the command line in some of these frameworkss. Depending on your
01:04platform, this change will need to be made in different places. Setting up PHP
01:09itself is beyond the scope of this course.
01:11For information about each of the AMP stack implementations, refer to Installing Apache,
01:17MySQL, and PHP, by David Gassner, here in lynda.com Online Training Library. In
01:23order for the exercise files to work correctly for you, it's important to follow
01:26this course in the order presented here.
01:29Each movie in chapter builds on previous information, so skipping around might
01:33get you into trouble, especially when we are working with the database. Because
01:37PHP is platform-independent and your system might be set up differently than
01:41mine, you may need to take some additional steps to get some of these commands working.
01:46Before we get started here, make sure you have a solid understanding of how PHP
01:50is set up on your system, including where the PHP executable is located and how
01:55to change some of the various environment variables. If you get stuck, you can
01:59always refer to Installing Apache, MySQL, and PHP for your specific server package.
02:04That all said, I'm excited to get started showing you these great PHP frameworks.
Collapse this transcript
Using the exercise files
00:00If you're a Premium member of the lynda.com Online Training Library or if you're
00:04watching this tutorial on a DVD-ROM, you will access to the exercise files used
00:08throughout this title. Inside the exercise files, you'll notice a few things.
00:13The db.sql is a SQL script that you can use to generate the database that we will
00:18use throughout this course.
00:19The files in the _START_HTML directory contain all the design files that we will
00:24work with in the different frameworks. There's a folder for each movie that has
00:28associated files. If you look inside of these folders, there is a start state,
00:33which is the state of the code when the movie begins, and there's an end state,
00:39which is where I'll be when I'm done with the movie.
00:41Since the frameworks rely on many files being present at the same time, you
00:45need to make sure to open the contents of each of the start folders to begin
00:49with. Then you can work with it like a project, as I'm going to do in each of the units.
00:54Seeing the overall structure of the project should give you a better
00:57understanding of how these frameworks function. Some editors like TextMate on
01:01the Mac, or Aptana for Mac and Windows, will allow you to open these folders and
01:05see all of the files, as opposed to opening one file at a time.
01:09If you are a Monthly member or Annual member of lynda.com, you don't have access
01:13to the exercise files, but you can follow along from scratch with your own assets.
01:17So let's get started.
Collapse this transcript
Installing Apache, MySQL, and PHP
00:00Throughout the course, I am going to be using MAMP, which is a server that
00:03includes Apache, MySQL, and PHP. If you're not using MAMP, you want to make sure
00:09to have Apache, MySQL, and PHP installed in your system. If you don't have it
00:13installed or set up already, a good place to get started is the Installing
00:17Apache, MySQL, and PHP course, here in the lynda.com Online Training Library.
00:23Another thing I'll be using is the command line. This is by default configured
00:27to work in a Macintosh environment; however, if you're using Windows, you want to
00:32make sure you have it configured correctly. You can find information on how to
00:36do this at the PHP.net web site.
Collapse this transcript
1. About PHP Frameworks
Why use a framework?
00:00As you likely already know, PHP is a solid application development platform. It
00:05provides services for handling email, interacting with databases, and a lot more.
00:09Because of it's power and flexibility, it's easy to get in there and start coding your application.
00:14So what is a framework and why use it? Let's have a look. Coding in straight PHP
00:18can work, but if you're not careful, you can end up with spaghetti code: long
00:22lines of code that have multiple interactions and typically have HTML code
00:27interspersed within them.
00:28It's easy when you're writing in straight PHP to do redundant work, that is, to have
00:33multiple PHP files that essentially do the same thing. When you go to make
00:37changes you are then going to end up having to change both of these files for
00:40everything to work correctly.
00:42Without any solid form of organization, it's easy to have files here and there
00:47and not really know how your application is organized. If you're not careful, it
00:50can be difficult to reuse the code that you have set up for yourself. If you
00:54change one thing, there is always a good possibility that you will break
00:58something somewhere else.
00:59Everything needs to be tested. When you writing all your PHP from scratch you're
01:03going to need to make sure that every piece of code is tested intensively. When
01:08you're working in PHP applications, if you don't have a granular structure, it
01:12can be difficult to have a team of developers working on the same code base.
01:16Frameworks can provide some help. More than anything, they provide a recipe for
01:21creating clean and organized code. When your code is granular like this, it's
01:26easy to test each individual piece and make sure it works in different cases.
01:31And when things are well tested, it's easy to reuse these little pieces here and
01:34there throughout your application.
01:36For the same reason, it's easy to make changes. Changing one small functional bit
01:40will then update that functionality in every place it's being used. Organization
01:44is good for its own sake. In most of these frameworks, you have a lot of
01:48pre-built and pre-tested tools included.
01:51Because everything is broken up in a more granular fashion, it's easier for
01:55different developers to be working on different aspects of the same application.
01:59The question is, should I always use a framework? You don't necessarily need to.
02:03If you're developing a smaller application, sometimes breaking up the code can
02:07end up meaning a lot of little steps, when you're really trying to do one simple thing.
02:11Learning each of these frameworks requires a bit of time. There's different
02:15constructs to be learned, and how they organize the code, everything is going to
02:19have to be understood in order for you to get in there and work quickly.
02:22There's no benefit necessarily to over- organizing your code. If you have a small
02:26application with a few different aspects, there is probably no need to break it
02:30up into a million pieces. The end result is that in an application using the
02:35framework we have our code broken up in a fashion that's more organized, it's
02:38easier to reuse, and it prevents us from redundant programming.
02:41We can test these smaller pieces. We can get a head-start using services
02:45included in the framework. It can also be easier for teams to negotiate working
02:50on overlapping functionality. Ultimately, frameworks lead to better organization
02:54of code, which in most cases can make or application development experience less painful.
Collapse this transcript
MVC is the MVP
00:00At the core of every framework is the concept of patterns or architectural
00:04patterns. Patterns are simply abstract ways to organize your programming. One of
00:09the most basic patterns that's used by virtually every framework that matters is
00:13what's called the MVC pattern. MVC stands for Model, View, Controller, and this
00:19pattern represents the concept of breaking down your application into three coherent parts:
00:23The model, which is the data layer; the view, which is the user interface layer;
00:28and the controller, which is going to mediate user interactions and create
00:32updates to the model.
00:34So to break down an interaction of the MVC pattern in the framework, it will go
00:38something like this. At the view layer, you're going to have the user interface.
00:43Typically in a web-based application this is going to result in HTML or HTML 5.
00:49The user interface is the view layer and when the user interacts with it, say
00:54they click on a button to add an item to the shopping cart, this will then send
00:58input to the controller, either through a JavaScript handler or possibly through
01:02submitting a form or sending it to a URL.
01:05If necessary, the controller will update the model. It will notify the model of
01:10the changes and this can then update it. In our shopping cart example, the
01:14controller would essentially add an item to a shopping cart data in the session.
01:19The view can then go back and query the model data if it wants to display it,
01:23for example, shows a user what's in the shopping cart. Notice that the pattern
01:27doesn't dictate how this is done. Each framework can do it however it wants to.
01:32If you aren't using the framework, you can see how it would be tempting to do
01:35this all in a single page.
01:38The PHP frameworks we will be looking at all rely on the Model/View/Controller,
01:42or MVC, pattern for their code organization. Using this pattern, they can break
01:46our application up into nice cohesive parts. The model layer will handle the
01:51data, the view layer is going to generate the user interface, and the controller
01:55is going to handle the interactions.
01:57So you can see why we call the MVC the most valuable pattern.
Collapse this transcript
Framework concepts
00:00When learning about frameworks there are a few core development concepts to
00:04understand, both when evaluating which framework to choose and in learning how
00:08to use a particular framework. Making these sorts of one-to-one comparisons can
00:12often be tricky, especially given how the documentation is organized within each
00:17of the different frameworks.
00:18But it's still really important if you're trying to determine which one you'd
00:21like to use. Fortunately, I've spent many hours putting together this material
00:25for you throughout the course, but the first step will be to understand the
00:29development concepts with which to look at these frameworks.
00:32The first concept to look at his pattern usage. All of these frameworks utilize the
00:37MVC pattern. The question is, how strictly and in what way did they enforce it
00:42and what other patterns do they utilize? For example, CakePHP has a very strict
00:47naming and code-organization enforcement, whereas CodeIgniter is much less
00:52dogmatic about how you place things, but it does still enforce MVC.
00:56What type of framework is it? Is it the glue framework or is it a full-stack
01:01framework? A full-stack framework comes with an entire set of constructs that
01:06you will use, and you pretty much have to use everything or not use it at all.
01:10Whereas a glue framework allows you to break up and use different pieces of the framework.
01:16Zend is a good example of a glue framework. You can use the different library of
01:20classes if you want to, of Zend, without actually using the MVC aspect of it.
01:25Another thing to look at is how code is organized. Namely, you are going to want
01:30to determine if it makes sense to you how everything is broken up. How is the
01:34data handled from within the framework? Does it use an object relational mapping
01:38tool like Doctrine? Can you persist through a tool like MongoDB, or does it use
01:43it fairly basic PHP or PHP's data tool, called PDO? What types of component
01:50classes and helper classes are available from within the frameworks? Does it
01:54provide methodologies for performing authentication, for sending emails, for
01:58building forms and validating those forms? Does it provide the ability for caching?
02:03How modular is the frameworks? Can use different parts of it? Or how difficult is
02:08it to integrate PHP that wasn't created from within the framework? Is there
02:13anything included in the framework that's going to help you with your user
02:17interface development? Some of the frameworks will include easy access to
02:21libraries like jQuery and Dojo, which have JavaScript and visual elements that
02:27will be easy to implement.
02:28Some of the frameworks will also provide you with tools to generate HTML5 forms
02:32that can be really helpful if you're trying to deliver to mobile and devices.
02:36The last thing to look at will be developer tools. Will you be able to use your
02:40existing development environment for this framework, or will you have to adapt
02:44another one? Does the framework itself provide tools for profiling your
02:49application and seeing how well it performs? Does it provide tools for easy
02:53debugging, and does it provide constructs for you to do testing?
02:56There are a number of concepts that you will need to understand, both when
02:59comparing frameworks and when you begin to learn them. We've looked at some of
03:03these, including pattern usage, code organization, data-handling mechanisms
03:08available helper classes, modularity, user interface helpers, and developer
03:13tools. Understanding how a framework incorporates these elements will help you to
03:17understand how it works.
Collapse this transcript
Finding the right solution
00:00In addition to the programmatic concepts introduced in the framework concepts
00:04movie, it will also be important to look at some other key elements when deciding
00:08which framework to use. This includes things such as licensing, the weight of
00:12the framework, documentation, community support, complexity, and performance. All of
00:17these are key considerations when you are determining which framework to use for your application.
00:21Some frameworks are very lightweight and provide a basic structure for you to
00:25follow, but most of the coding is up to you. In other frameworks like Symfony you
00:29have a lot of tools, but all of your application development is tightly tied to
00:33that framework, and it may also be limited by the limitations of the framework
00:37itself, including performance and possibly features and more.
00:40Which license you choose can make a difference. There are a few different
00:44license agreements that these open- source projects can use. Some of these
00:48licenses are extremely flexible and allow you to do what you like in your
00:51commercial applications. However, others require you to do things like open your
00:56own source up for other developers. So make sure you choose the right license for your project.
01:01The documentation for a framework can make all the difference for getting up and
01:04running. Make sure to take a look at the framework's docs for whichever one
01:08you're interested in and see if they make sense and are comprehensive to you.
01:13Community support is a huge factor in determining which framework to use. While
01:17most of the frameworks discussed in this course are very active, it's important
01:21to look and see if you can get your question answered from a Google group, or if
01:24you can find more information from a chat or wiki.
01:27Popularity can be key determination. How many people are using a framework?
01:32While in many ways this is very difficult to determine, one way to find out is
01:36to look at Google Trends. What you'll see here is the result of searches that have
01:40happened at Google based on the different frameworks.
01:43You can see here that Zend appears to be the most popular and the other three
01:46are a little bit lower on the scale. However, that could be due to other search
01:50terms. So take this kind of result with a grain of salt. No survey method is
01:54going to have complete accuracy, but this can give you a general idea of what's
01:58going on out there in the community.
02:00Complexity is another factor to consider. Some frameworks have a quicker
02:04learning curve while others require a higher-level understanding, both in terms
02:08of understanding the PHP language itself and object-oriented programming, but
02:12also in terms of understanding constructs within the framework. Make sure not to
02:15get in over your head and also make sure not to choose an easier one that lacks
02:19features that you need or want.
02:21Scalability or performance are other key factors consider when determining a
02:25framework. How an application will scale as usage increases can be extremely
02:29important. If you're building a personal blog for a small audience, this may
02:33matter less than if you're building an enterprise application. But be sure to
02:36understand what the restrictions are.
02:38There are a number of non-programmatic considerations when choosing a framework.
02:42These can have wide-ranging effects depending on the type of application you're
02:45building. Make sure to weigh all these items, including licensing, documentation,
02:49complexity, and performance when you're choosing your framework. Now that we have
02:53an understanding of what to look for, let's compare the frameworks we will be
02:57looking at in this course.
Collapse this transcript
Comparing Zend, Symfony, CakePHP, and CodeIgniter
00:00In this course we cover some of the main PHP frameworks. There are many more out
00:04there, but these are what seem to be the most popular and well-used ones currently.
00:09The concepts presented in the other movies of this unit are key concepts to use
00:11in your determination. In this movie, we are going to look at each of the
00:15frameworks and discuss and highlight their differences and similarities to help
00:19you choose which one is right for your project.
00:21Zend is backed by Zend Technologies. Zend is fairly popular and well used,
00:27specifically because of its corporate backing and its support with partnerships
00:31such as Google, IBM, Adobe, and Microsoft. It's been around since about 2005 and
00:37uses the flexible BSD license.
00:39The code is very well tested and is considered by many to be enterprise-ready.
00:44It is a loosely coupled framework, which means you can use different elements
00:48from Zend with or without using the MVC aspects of it. It has a very large
00:53library that does all kinds of things, including searching, handling data,
00:58sending mail, internationalization, caching, and all kinds of other things.
01:03It has a very large community. Zend technologies produces an IDE and also a
01:08server and provides service and support, so this can be really helpful to know
01:13that you have corporate backing when you're working on your project. There is a
01:17bit of a learning curve getting involved with Zend.
01:19One of the downsides of the Zend framework is that it's fairly complicated in
01:23getting started. The analogy that I use with the Zend framework is J2EE. There
01:29is a lot available, and there's a little complexity once you get involved getting up and running.
01:33The Symfony framework, we are going to look at Symfony 2.0 in this course. It
01:38was first released in 2005 and is backed by a company called SensioLabs that's
01:42based in France. Symfony uses the flexible MIT license. Symfony is a full-stack
01:48environment, but 2.0 adds some features that make it more flexible.
01:52The model layer of Symfony is handled using Doctrine's object-relational mapping
01:57utility. A lot of the coding you will do in Symfony can be done in a number of
02:02different ways. It's done essentially by writing configuration files, and these
02:07can be written in the YAML, XML, PHP, or using PHP annotations, depending on what
02:13you're trying to do.
02:14It has a pretty good library for some of the basic functionality you'll need in
02:17most applications, including authorization, building forms and validating them,
02:22and internationalizing applications. It will integrate with PHPEdit, NetBeans,
02:27and other eclipse-based IDE, and of course you can always use text editors.
02:31There is a little bit of a learning curve getting involved in Symfony, mainly
02:35because you are going to need to learn new constructs. For example, you'll need
02:38to learn Doctrine if you don't use it already, and you may need to learn how to
02:42write these different types of descriptor files.
02:44Because Symfony is full-stack, it can tend towards not performing well. However,
02:50with the release of 2.0, they promise better performance, due to more intelligent
02:55caching. The documentation is pretty good. There's a good community involved
02:59with Symfony, and you have a lot of backing with chats and newsgroups and things like that.
03:04The best analogy for Symfony is Ruby on Rails, especially given its use of YAML.
03:10CodeIgniter is backed by a company called EllisLab. It's been around since 2006,
03:15so it's a little newer. CodeIgniter has a special license. It used to be a
03:20proprietary one, and they've updated it with version 2; however, it's a little
03:24tricky so you may well look into it if you're looking at distributing your application.
03:28One of the key advantages to using CodedIgniter is it's extremely lightweight
03:32and performs very well. It's a very flexible framework, which can be a benefit
03:37and also a curse. Getting up and running with CodeIgniter is a fairly simple
03:41task. There are a lot of libraries available to you in Codigniter, but oddly they
03:46didn't include anything for authorization.
03:48There is, however, a good community behind CodeIgniter and a lot of third-party
03:52libraries available to you. If you're looking to integrate it with your existing
03:56IDE, it'll work with the plug-in for Eclipse, and it also supports CodeLobster.
04:01The documentation is pretty helpful. CodeIgniter is probably the closest thing
04:06to writing straight in PHP. All you're really doing is extending PHP classes, and
04:11how you organize the code is still fairly flexible.
04:14The last framework we are going to look at is CakePHP. CakePHP has been around
04:19since 2005, and is the only one that's managed by a nonprofit foundation, Cake
04:23Foundation. CakePHP, like Symfony, uses the MIT license, which is very flexible. It
04:29is a full-stack framework, and it was inspired originally by the Ruby on Rails framework.
04:34CakePHP has a very large library that includes helper classes for all kinds of
04:39things like Ajax, creating forms, internationalization, email, and handling
04:44authorization, and access control lists. It has a fairly short learning curve,
04:49especially given that you're learning a lot of new constructs. There are a lot of
04:53rapid application development tools, including scaffolding, which will auto-
04:57generate a lot of coding for you.
04:59The CakePHP framework is often called automagical because there's a lot going on
05:05behind the scenes. This is what provides for those rapid application development
05:08tools. However, when you get in to deeper customizations, this can be a little bit
05:13difficult. Additionally all this automatical functionality can slow down
05:17performance. There is some caching, but it's still a fairly heavy framework. The
05:22CakePHP documentations are very good, and they even have a lot of movies on their web site.
05:27CakePHP has a very active community. You can integrate Cake with a lot of the
05:32main IDEs, including Neptune, which is currently in beta, Code Lobster, cuisina
05:37NuSphere, and of course integrating with Eclipse. The best analogy for CakePHP is
05:43what it emulated originally, Ruby on Rails.
05:46As you can see most, of these frameworks contain very similar features. The
05:50details of how everything works is really the crux of your decision, and that's
05:54what this course is all about. Generally speaking, Symfony and CakePHP are
05:58tightly coupled frameworks that have a very specific implementation. Along with
06:02that comes a lot of rapid development features, but a learning curve on your
06:06first application. CodeIgniter is probably the simplest of all and a really good
06:10performer, but it doesn't offer as much in the way of rapid application development.
06:15And lastly, the Zend frameworks is the current leader, due to its corporate
06:18support, loose coupling, integrated IDE, and well-tested and good-performing code base.
Collapse this transcript
2. Introducing Zend Framework
Intro to Zend 1.11 and the 2.0 beta
00:00Zend is unique among the PHP frameworks because of its corporate backing. Zend
00:05Technologies makes money selling their IDE and integrated server software, and
00:09has made a key goal of their project to being enterprise ready.
00:13Because of this, it appears to be a favorite framework for the enterprise, and has
00:17garnered partners of the likes of IBM, Adobe, Microsoft, and Google. Zend has a
00:22reputation as having a well-tested code base, decent documentation, a wide
00:26library features, and a loosely coupled approach.
00:29Zend is backed by Zend Technologies. They've had significant partnerships with a
00:34lot of development organizations throughout the enterprise. Their code base is
00:39well tested both by these organizations and by the Zend team.
00:43There is a huge community of developers using the Zend tools. The framework
00:48itself has had over 10 million downloads. There is an assurance to having Zend
00:52Technologies back you up with support. If you get stuck with the frameworks, you
00:57can always call them for instance- based support or for consulting.
01:01Zend Technologies also has a certification program, so you can become a Zend-
01:06framework-certified developer. There are a number of very high-profile sites
01:10that are using the Zend framework.
01:12Zend framework is shipped with the BSD license, which is considered to be very
01:17commercially friendly. Additionally, you can download the Zend server. It's a
01:22full-stack server that includes the Apache web browser, MySQL database, and PHP,
01:27as well as the framework. There's a free community version available, and you can
01:31also get licensed versions that have specific performance-enhancing utilities built-in.
01:38Zend also provides Zend Studio, which is an integrated development environment.
01:43It has debugging code help, specifically for all the Zend frameworks libraries. It
01:48will help you with project creation and rapid application development, and is
01:52built on the Eclipse toolset.
01:55The structure of the Zend framework is a glue framework, which means it's
02:01essentially an ad hoc framework. You can use the libraries and not use the MVC
02:06if you prefer to do that. Additionally, there are tons of libraries available to do
02:10all kinds of things from within the framework.
02:13There is a lot of UI support for Dojo, jQuery, and Ajax to build highly
02:19interactive user interfaces. They are performance-based tools that allow you to
02:24do logging, testing, profiling, and caching. These are the things that are really
02:29key in an enterprise environment. And of course, there's the core libraries that
02:33can do everything from talk to your databases to handling email, and much more.
02:38Currently Zend is in 1.11 version. That's the version we're going to use for the
02:45movies and for this course, but there is a beta version of 2.0 that has
02:49currently been released.
02:51Because the documentation isn't quite as ready, there will be a little more of a
02:55learning curve and it might be a little difficult to get up and running until
02:58they're fully in release mode.
03:01Zend 2.0 is one of the new wave of PHP frameworks that requires PHP 5.3. 5.3
03:08introduced a lot of object-oriented concepts, including namespaces, which allow
03:13for very tight and object-oriented development.
03:17The goals of the Zend 2.0 are based on a survey they did in 2009. One of the
03:23key principles is to ease the learning curve of getting involved in Zend.
03:27Performance is also a key aspect of Zend 2.0.
03:32Another feature they aim for is to make extending the framework, what they say,
03:36trivially simple. In other words, you can develop your own plug-ins and plug
03:40your own libraries in with relative ease.
03:43Overall Zend is the most commercial of the PHP frameworks we'll be looking at.
03:47Its advantages lie in its loosely coupled environment, its many libraries, well-
03:53tested code base, corporate support, and good documentation, and help.
03:57Next, we are going to take a look more in depth at Zend's features and
04:00components.
Collapse this transcript
Zend concepts, advantages, and issues
00:00The Zend framework is considered a glue framework, meaning it contains a number
00:04of different pieces from which you can pick and choose. In order to really make
00:08the Zend frameworks work for you, it's important to understand a few key
00:12principles. In this unit, we'll look at those principles, and we'll also discuss
00:16the advantages and issues regarding the Zend framework.
00:19In order to install Zend framework, you're first going to download it at the web
00:22site, and there are two versions available to you: the minimum version and the
00:27full version, which will include a number of different libraries, including the Dojo library.
00:32You can also install Zend without vendors, which means you can pull in the Zend
00:38framework at will by the command line, and sometimes that's advantageous. Zend
00:44also ships with a command-line tool called zf. This can enable the auto-creation
00:49of projects and other things as well from within the Zend framework environment.
00:54Generally, when you go to set up your Zend application, you're going to need to
00:58create some kind of web server mapping to make sure that the URLs map out to
01:03the root of your web server.
01:05In terms of code organization, the Zend framework is very object oriented, which
01:11means most of what you'll be doing will be extending existing classes from the
01:15Zend library and doing other things in terms of working with classes, methods,
01:21and other object-oriented constructs. It's highly decoupled, and is very
01:26extensible and ready for plug-ins.
01:28The MVC interpretation of Zend is quite literal. When you create your
01:34controller, you'll extend the Zend_Controller class. Likewise, when you go to
01:39create views, you're going to use the Zend_View class.
01:42In terms of the model, however, there is no specific model provided. When you do
01:49go to create your model, you can optionally use the Zend_Db and its class of
01:54tools, or you can use Zend_Table. These will require a lot of code writing in
01:59order to make your model work, but they definitely do the job.
02:03Additionally, if you'd like, and you want to speed up your development process,
02:07you can use 3rd party object- relational mapping tools, such as Doctrine. There
02:11are tons of components available from within the Zend library. It's truly one of
02:16the advantages of using it.
02:18Another advantage is its support for multiple deployments. Within a single
02:23config file, you can have information for different databases and different
02:27configurations for your development, your testing, and your production deployments.
02:32There are some issues, however, with the Zend framework. Probably, the number one
02:37issue is the learning curve in getting started with Zend. The framework itself
02:42has a fairly large footprint. Even the minimum is over 30MB, and if you download
02:47the full version it's over 91.
02:49One criticism of the Zend framework is that it has no real rapid application
02:53development tools like scaffolding that highly automate the creation of code or
02:58abstract it so you don't have to write it yourself.
03:01Another criticism that people have of the Zend framework is that there's no
03:04built-in data model. This means different applications can have very different
03:09implementations of how the data is being accessed.
03:12Generally speaking, Zend requires writing a lot of code. There are no shortcuts.
03:17Because it's loosely coupled and because there's so much available in the
03:21framework, one criticism is that it's difficult to learn everything and to
03:25understand everything that's available to you within it.
03:28Because of all the different corporations and the people involved in the
03:31community, it sometimes can be slow for changes to be made. There are some
03:36issues with configuring Zend for your hosting provider.
03:40In terms of performance, Zend is generally better than the full-stack
03:44frameworks; however, it can't really compete with lightweight framework like
03:48CodeIgniter. 2.0 may provide fixes for this.
03:52Overall, the Zend framework is an object-oriented ad hoc framework that allows
03:57you to pick and choose both which libraries you wish to use and how to structure
04:00entire aspects of your application, including the model layer.
04:04In order to better understand what's available in this framework, it's a good
04:07idea to learn about what components are available, and that sounds like a great
04:10next thing to do.
Collapse this transcript
Zend components
00:00One of the key potential benefits of a framework is what components are
00:03available to you. That is to say, what pre-tested and preconfigured
00:07functionality is available out of the box. This is one area where the Zend
00:12framework certainly shines.
00:14There are a few major categories of the Zend components. The MVC or Model-View-
00:19Controller components are key because they are how you're going to organize your
00:23code when you're using the Zend framework. This includes the Zend_Controller
00:27that we talked about and the Zend_View class. But in addition, you have the Zend
00:32Bootstrap for starting up the Zend_ Controller router, which is going to handle
00:36the routing of URLs, and a ton more that you can use.
00:39Zend also includes a number of tooling and rapid application classes. Primarily
00:45these are going to be used when you use the zf command-line tool. It's called
00:50Zend_tool, and a lot of the related classes are built for PHP CLI, or Command Line Interface.
00:58Zend comes with a number of classes for you to access and work with databases.
01:04This includes database adapters, the Zend_Db adapter, which will allow you to
01:09talk to SQLite or other SQL engines. You can also use this to profile how your
01:15queries are working, to connect to tables, and do all kinds of other interactions
01:19with your databases.
01:21Zend supports internationalization and localization. So if you determine a
01:26user's locale, you can deliver different currency values, different date times
01:31and formats, different measurements, and even translate different parts of your web site for them.
01:36Zend provides a number of tools to enable authentication. This includes session
01:42management, the authentication itself, and also using ACLs, or Access Control
01:47Lists. Since many of Zend applications are service-based, Zend provides a number
01:53of tools for both consuming and exposing services.
01:56You might use, for example, the Zend AMF server to allow Flash to call on
02:01methods within your class. You may also enable the Zend Rest server which allows
02:07you to create a Rest-based web service.
02:10On the consuming side of things, you can use the Zend Rest client to talk to
02:14other services, maybe like the Yelp or the Facebook API. Or you can use a class
02:19like the Zen Gdata class which allows you to talk with most of Google's services.
02:24Zend contains tools that allow you to send email, to perform full index searches,
02:30and to generate PDFs. Zend also contains a number of core infrastructure
02:35classes. These can help you interact with the cache, enable debugging, help with
02:41loading, and all kinds of other things that will be used internally by your applications.
02:46I highly recommend going and looking at the Zend Framework Reference. This is
02:52inside the programmer's reference guide, and it will give you a list of all the
02:56classes and their purposes.
02:58Because of the number and nature of Zend contributors, there are tons of classes
03:03available to you in the Zend library. Just by simply looking at the classes of
03:07categories available, you can see how much pre- tested code is available to you in the Zend framework.
03:13Definitely refer to the Zend Framework Reference. What I discussed is only a
03:18small fraction of what's actually available to you, and it's definitely worth
03:22taking a spin through and seeing all the libraries.
Collapse this transcript
Zend flow and architecture
00:00Before we dive in and actually use the Zend framework, it's a good idea to first
00:04go over how the framework actually works. And this can be helpful in both
00:08assessing if this is a framework you'll be comfortable using and also preparing
00:12to actually use it.
00:14The first thing to look at is the core directory structure, and you can see here
00:20under your project name you're going to have a set of subdirectories. The
00:23application is going to house most of your files.
00:27The library is going to hold the Zend core library. The tests allow you to put
00:32unit tests in there that you're going to run against the aspects of your code,
00:36and the public directory will be what's exposed to the web site. This is where
00:40you can put things like your style sheets and images and things like that.
00:44Finally, you're going to have the Bootstrap.php. This is the main index file
00:49that's going to be used in all page requests. So this is going to load the
00:53controller, among other things.
00:55The model aspect of Zend is not strictly enforced. You can use the Zen_Db class,
01:02which uses what's called the factory design pattern. It's also going to use
01:06PHP's data object extension.
01:09There is help for security, in terms of handling issues like SQL injection. You
01:15can also use the Zend_Table class. This is going to implement the Table Gateway
01:20Pattern. Whichever one you use, you are generally going to have to write a lot
01:23of code, which is to say you are going to have to write the methods that are
01:27going to insert, update, delete, and grab data from your database. If you want
01:33to, you can also use Doctrine or some kind of other object-relationship-mapping tool.
01:39The view layer, when we implement it, we are going to use the Zend_View class.
01:44This implements the factory design pattern. If you want to, you can use
01:49Twig of Smarty, but by default it's going to use just plain PHP templates to render
01:55your HTML or whatever type of view you're going to generate.
01:58The Zend_View_Helper class can provide a number of other tools to help you
02:03format or escape output. The crux of the work you're going to be doing in the
02:08Zend framework is in the controller. It's required that you have one controller,
02:12but if you want, you can have more.
02:15The Zend_Controller_Front class is what you're going to extend inside of your
02:19Zend_Controller, and it's really the foundation of all the controllers. The
02:24request is going to be encapsulated in the special class, called
02:29Zend_Controller_Request_Http, and you can use that to access form and URL variables.
02:35There's a router interface that allows you to dictate what your URLs look like
02:39and how you determine what controller to load and what method to call in that
02:43controller. There is also a Zend_Controller_Dispatched. The dispatcher allows
02:49you to do what's called dispatching, which is really just handling the request.
02:53This can also forward to different controllers and things like that.
02:57And finally, the action and this is the core, how Zend processes the request. It
03:03will have the request data and it will generate a response, which is then going
03:07to be handled by the Zend_ Controller_Response_Http class.
03:12This is really how the flow works inside of the Zend framework. You can see the
03:17request object is always peripherally available as you pass through the
03:21different aspects of the control. The request is handled by the router, which
03:26then has a preDispatch, Dispatcher, postDispatch, and all of these are really
03:31just a way of saying you have a number of different areas where you can
03:35interact with the data that's coming in and determine what's going to be sent
03:39back to the browser, or whatever it is that's going to be rendering your user interface.
03:43So as you can see, Zend has a clear process for the flow of an application. The
03:48bootstrap loads the controller. The controller handles the request routing,
03:52dispatching, and finally, the response.
03:55The view is going to be handled by the Zend_View and the simple Zend_Db and
03:59Zend_Db_Table classes can be used for the model. This is a highly object-
04:04oriented approach. It might seem a little complex for first-time users, but
04:07ultimately it provides developers with a lot of flexibility and control.
Collapse this transcript
3. Using Zend Framework
Downloading and setting up Zend Framework
00:00In order to use the Zend framework, we first need to download it. Unlike other
00:05frameworks, Zend has a few options in how you can use it. Because you can
00:10optionally use Zend's preconfigured server, this can be a valid option, both the
00:14community, that is, free, edition or the commercial edition. In this movie, we'll
00:20discuss the different options and where to find them.
00:22The main framework downloads can be found at the download URL on the main
00:27framework.zend.com site. On this page you will also see links to download the
00:34GData libraries, the InfoCard libraries from Microsoft, the AMF libraries to
00:41interact with Flash, the simple CloudLibrary, and then you also have access to
00:47the archives to download older and different versions of Zend framework.
00:52You can also download the serverm which we'll talk about in a second. When you
00:57go to download the framework, you'll see you have options to download the full
01:03framework, which includes unit tests, demos, docs, as well as the Dojo toolkit.
01:09The minimal package is going to contain just the basic core library of the Zend framework.
01:15If you want to download the server, you can go to the zend.com web site, which is
01:20Zend Technologies' web site. And on their Downloads page, you will see the links
01:25to download both the Zend server Community edition, which is the free version, and
01:31the Zend commercial server includes a number of tools for profiling and
01:37handling caching and cluster management.
01:39We're going to use the framework only. So you can just download the framework
01:44file. The easiest way to do it is to simply download the zip file--it can either
01:51be a tar.gz file or a zip file-- and extract it to your computer.
01:59Once this is complete, you can name it whatever you like. It will by default
02:04have the version number on it. If you wanted to just call it Zend framework that
02:08would be acceptable as well.
02:10You can then install this into your Applications/MAMP directory and then use it
02:16throughout your MAMP, or wherever else you like on your hard drive; just make
02:19sure to note where it is. Another way to download the framework is by using the
02:25subversion tool. If you're using Mac OS 10, subversion will be installed with the Xcode suite.
02:32The first thing we want to do is we want to go into our MAMP directory, so I am
02:36going to use the Change Directory command. For more information about using the
02:41command line in Mac OS 10, you can check out the UNIX for Mac OS X Users course
02:45here in the lynda.com Online Training Library.
02:50Now that I'm in the MAMP directory, I am going to create a new directory called
02:54svn, I am going to change into that directory, and I am going to create one
03:01final directory where I'm actually going to place the download called Zendframework.
03:07Finally, I am going to change into that directory and from this directory I am
03:13going to perform my subversion checkout. So the way we do that is we use the
03:16command line svn checkout, and we point to the location of the subversion trunk.
03:24In this case, it's http:// frameworkzend.com/svn/framework/standard/trunk.
03:43This will then download the full version of the Zend framework, including all
03:47the documentations in Spanish and English and a number of other languages, so it may take a while.
03:54Once you've completed downloading all of the Zend framework from subversion,
03:58you're going to need to create an alias. The alias we're going to use is zf for
04:03Zend framework, and we're going to want to make sure to point it to the zf.sh in
04:08the bin directory of Zend framework. This is located specifically in
04:12applications/MAMP/svn/ zendframework/trunk/bin/zf.sh.
04:23Once you have set that up, you can test this out by typing in "zf show version"
04:29and this should give you the version of the Zend framework that you're using.
04:32Keep in mind that if you're setting this up and you didn't download from
04:35subversion, you don't need to refer to the trunk directories, as they won't exist
04:40outside of subversion.
04:43The last thing to configure Zend framework is to do some changes to the php.ini
04:49file. In MAMP, you can find this in the PHP directory in php5.36 and in the conf
04:57or c-o-n-f directory.
04:59Inside the php.ini file you're going to look for the include_path. This
05:05variable stores all the paths where PHP looks for PHP classes. We want to make
05:11sure to add the Zend framework library to this path. Each of the paths is
05:16separated with a colon. You can see the first path is a dot, which means it's
05:20going to include the current directory of your PHP file.
05:25You notice we also have a php5.3.6 library. Now we need to add our Zend
05:32framework, and the framework specifically is located at Applications/MAMP in the svn
05:38directory, /zendframework/trunk/library.
05:47Keep in mind if you downloaded this, you are not going to have the svn
05:51directory, nor are you going to have the trunk. When you're done, you're going
05:55to want to make sure to restart your PHP server so that these changes will take effect.
06:01As you can see, we have a few choices as to how we can download and install the
06:05Zend framework. We can download the framework to install on our own PHP server,
06:12either in a scaled-down minimal zip or with all the bells and whistles.
06:17Conversely, you can use subversion and go to the repository directly. Either
06:21way, we'll want to add the library to our php include_path and optionally
06:25prepare the Zend tool for some command-line assistance getting started.
Collapse this transcript
The development environment
00:00One advantage of using the Zend framework is the integrated tool set, which
00:04includes the Zend studio IDE. While it's by no means required that you use this
00:09development environment, it does contain a number of tools that can make it
00:12helpful in working with applications in the Zend framework.
00:14Zend studio is Eclipse-based. It's currently at version 9. You'll notice when
00:20you start up, you get a couple of prompts. One will prompt you to add a toolbar
00:24to your browser. If you like to do this, this can help for debugging, profiling,
00:29and quickly jumping into the studio, as well as searching the PHP and Zend docs from your browser.
00:36Additionally, you'll be prompted if it detects a Zend server or not. You don't
00:41have to use Zend server with Zend studio. However, when you go to do debugging,
00:45it can be a lot easier to do so.
00:48Let's look at some of the things you can do in here. You'll see when you first
00:52get started, you get this Welcome screen. There are a number of add-ons you can
00:56plug in that will give you some help in terms of syncing with Git, using
01:02subversion, RSS feeds, linking to Amazon web services. There are all kinds of
01:08things that can be helpful with your development.
01:11You can create new Zend projects by simply right-clicking and going to create a
01:16new PHP project. You can give it a name, and notice it prompts you what version
01:21of the Zend framework you want to use. Once it's created, you can use some of
01:28the other tools to launch wizards to ease some of the other development tasks.
01:34For example, if I want to create a new controller, I can right-click on the
01:38controller folder and go to New > Zend Framework Item. I can choose which type of
01:44controller I want to create or some of the other Zend modules as well.
01:48I can do some customization in addition to naming it. I can determine where I
01:56wanted to reside. It will give me a short preview of what's going to be
01:59generated and when I click Finish, it will generate the file for me. Notice how
02:05it includes the action.php file and makes sure that this controller extends that class.
02:11Finally, when you're working inside of the Zend environment, you're going to get
02:15some nice code hinting. Note how these different elements pop up. It will give
02:20me options of what's available in specific objects, what the arguments are that
02:24can be taken, and what type of data will get returned.
02:28So all in all, Zend Studio can provide a helpful environment. It's not required,
02:33but it might be something worth considering to shorten your development time. It
02:36can ease your work in a collaborative environment, and it can ease deployment.
02:41That all said, to give a fair comparison, we are going to build our Zend
02:44application and all of our other applications using a simple text editor.
Collapse this transcript
Creating a project
00:00The first step in creating a Zend MVC application is to create the project. This
00:05entails setting up the core directory structure and getting prepared so you can
00:09dive in and write the code for your data, view, and controller.
00:12Additionally, we are going to create the database that we are going to use for
00:14our application. Let's go into the Zend command line utility, so I am going to
00:20go into Terminal. Zend has a command line utility that makes creating new
00:25projects easy. We configured this in the download and setup movie. So I am going
00:30to go to my Terminal window, and in my Terminal window I am going to navigate to
00:35the web directory of my map route.
00:38In this case, it's Applications/MAMP/ htdocs. I can now use the zf tool, which I
00:48created in Alias for, and call create project, and call whatever you like. In
00:54this case I am going to call it explorezend. So you can see this created a web
00:59project, and it gives you some information. It says it did find or didn't find
01:03this. Nothing to worry about it at this point. Let's go ahead and navigate to
01:08that directory and take a look at what got created.
01:12So in my explorezend you can see I now have an application directory, docs,
01:19library, public, tests. The main thing that we are concerned with here is going
01:25to be all this config files. We are going to put our controllers in here. Notice
01:31it comes with a default setup with an error controller, which is used for
01:35handling error messages, and an index controller, which is going to handle the main page.
01:40In addition, I have this Bootstrap.php file, and in there you can see its
01:46Bootstrap and it extends the main Zend_Application_Bootstrap_Bootstrap class.
01:53This is going to load everything in my application. So let's go ahead and
01:59navigate to this in our browser. In the case of MAMP, we are located on port
02:048888. If you are using MAMP or ZAMP, you don't have to worry about those port
02:08numbers, and then we type in explorezend/public. This is our root directory, and
02:16you can see it's an empty file, because we haven't installed anything, but it is
02:19working. We are not getting an error.
02:22The Zend tool that we were using, zf, offers a lot more help. If you type in zf
02:27and then the question mark, it will give you a list of everything that's
02:32available to you from this command line. The last thing we want to do is create
02:41our database. I am going to open MySQL Workbench, which is available from
02:46mysql.com. I am going to create a new connection. I am going to call this localhost MAMP.
02:55If you are using ZAMP or MAMP, you don't have to worry about changing the port
02:59number. In the case of MAMP, it's installed on port 8889. And you want to make
03:04sure you have the correct username and password. If you want you can test your
03:08connection and make sure it's okay. Now I will connect to my database, and I am
03:15going to go ahead and open file that I have already created, called db.sql. I am
03:23going to open that in my text editor. Go ahead and select everything and copy it
03:28and we will just run it directly.
03:30When you execute the SQL command, it will create a new database called
03:35php_framework_test, and inside will be a table called mailing_list that will
03:41contain a number of different columns to handle the userid, first_name,
03:46last_name, email, address, et cetera. This is going to go along with our
03:50application that we are creating.
03:51It's a very simple application that's going to allow us to take in mailing list
03:56subscribers. So this statement is designed for MySQL and to execute the database,
04:02and this is pretty much all we'll have to do using MySQL. If you'd like to learn
04:07more about MySQL, you can refer to the course MySQL Essential Training, here on
04:12the lynda.com Online Training Library. If you really wanted, you can create the
04:16project directory structure and start files yourself;
04:19however, the Zend command line tool saves us the step. We just ran the zf create
04:24project in the directory we wanted to place our project and voila! We are ready
04:29for action. Now we can go on to create our application. We are going to create
04:32the model, the view, and the controllers.
Collapse this transcript
The view
00:00In order to begin working with the view in Zend, we're going to need to dive a
00:03little deeper into the architecture. There are a few steps and patterns that need
00:08to be put into place. Zend can help us out with this configuration through the Zend tool.
00:13In this movie, we'll look at how the view layer of Zend is implemented. When you
00:16implement the view in Zend, we're going to use a couple of different patterns.
00:22One is called the two-step view, and the two-step view essentially says we're
00:27going to have one step that's going to grab the layout or the design of every
00:31page and then another step is going to pull in the page-specific view.
00:35Composite view is another pattern that's implemented by Zend, and that just
00:40basically says our view is made up of separate view elements. It's atomic, essentially.
00:46All right! So the first step is we're going to enable the layout, and the way we
00:50do this is we use the Zend tool in our command line. We're going to go into our
00:56directory again, so we're in Applications/MAMP/htdocs, and then we're going to
01:06go into /explore. Now that we're in here, we
01:12can go ahead and create zf, which will open the tool, and there's a special
01:16command called enable layout.
01:20So what this will do is a couple of different things. It's going to add a layout
01:25entry to our config file, and it's going to create a default layout. So let's go
01:30ahead and edit our application. And I'm going to open it up directly in my text editor.
01:38So here's my Zend application, and if you look in configs--let's was first open
01:43application.ini. This is our config file inside of our Zend Application. And you
01:48can see this is where enable layout added a new layoutPath, APPLICATION_PATH, and
01:54layouts/scripts. Sure enough, if we go into APPLICATION_PATH, is this application
02:00directory, layouts, scripts, and you can see we have this layout.phtml file. What
02:07we're going to do is we're going to take our application that's already been
02:11designed for us in Dreamweaver by our designers.
02:14So I'm going to open the HTML file, and you can see all the HTML is right here.
02:25And if you want to look at it in the browser, we can do that as well. So
02:32essentially what I want to do is I want to take out the blue with the image and
02:39our footer and I want to use that as the default wrapper for anything I use in
02:42this application. Then everything in this Join the Explorers! down to and
02:48including the form, is going to be the page-specific view.
02:53So let's find where all that information is. So here you can see, is our
03:02HTML, our scripts, our header. Here's our body. Join the Explorers! starts right
03:07at this h1. So let's go ahead and cut everything out before that, so go ahead
03:16and highlight it and cut it. And then let's go back into our layout.phtml and
03:26paste it at the top of the file. So this will essentially append everything.
03:33This HTML will come at the top of every file that's loaded in our application.
03:38Notice I have this php echo statement here, echo $this->layout()->content. So
03:44the layout object is returning a property called content, and that's going to
03:49have the data for each specific page that gets loaded. So essentially, this is
03:54going to enforce the laying out of our information in each page.
04:00Back in this file, I want to scroll down to the end of my form and I want to
04:12essentially cut this closing div tag down to the end of the file. And I'm going
04:18to go back into my layout.phtml, and I'm going to paste this as the footer. So
04:29now you can see the content is going to be output always in between all this
04:35other information that I'm including.
04:37To ensure that all these joined files that are being referred to that include
04:44our style sheets and our JavaScripts, we want to make sure to copy that folder
04:50into the public directory of our Zend application. So let's go ahead and
04:55highlight this, copy it, and then we'll go into htdocs and then in explorezend,
05:03we'll go into public and paste all that information. So that will ensure
05:10everything is available to us.
05:12So now we want to go into our Bootstrap class, which is
05:15application/Bootstrap.php. Inside of here we're going to initialize the doc type
05:23it's called. So we're going to create a protected function called _initDoctype.
05:32And in this function we're going to do a few things. One is we're going to get
05:38access to our view object. And the way we do that is we use the "this" keyword,
05:44which essentially refers to this Bootstrap class, and then we can call the
05:50special bootstrap method and pass to it view. That will essentially bootstrap
05:58the view and then what we can do, we can create another variable called view
06:04and set it equal to, and then we'll use the getResource method, which is going
06:11to then essentially load up our view resource.
06:14And then finally, now that we have this loaded, we can declare the doc type using
06:19a special doc type method of that resource. In this case, we're going to say XHTML1_STRICT.
06:31There are other options available, and you can find these in the Zend docs,
06:35including HTML5. In order for this to be available, we need to go into the
06:42application.ini and we need to assign a special property in here called
06:50resources.view. And we're going to set it to an empty array. And this all looks
06:58very strange, but this is a special property and when you add this to the
07:04application.ini, it will ensure that you can access your view resources and set
07:10all these different properties inside of your application.
07:14The last thing we want to do is we want to make sure that the rest of our
07:18initial file, the form and the header-- so we can go ahead and select all and
07:27then cut it, so you should have an empty file now--We want to make sure that
07:33this all shows up for the main index. All of the indexes and all of our
07:40page-specific views are stored in the scripts property, under the name of the
07:47controller. So in this case, we are essentially setting up the design for the
07:53index controller. So if we go into the index folder of the scripts, in the views
07:58folder we'll find index.phtml. And this was our default view. We're going to go
08:04ahead and select everything and paste over our own file.
08:11So now everything should be initialized, and when I run the application, we
08:16should be able to see it all. So this is our application. The one last thing you
08:20may want to do--and this is completely up to you--is you can replace these script
08:25sources and titles and things by using methods that are available within the this construct.
08:33The Zend framework has a specific methodology for implementing our view
08:36framework. The basics are to register the layout in a .ini file. Then plug our
08:42core wrapper in the registered file. In this example, it was the layout.phtml.
08:47We can then access the layout context within its file in order to output
08:51different elements.
08:53This is just an introduction. You can take it from here using Zend's view
08:56helpers and a number of other features that you can find in the Zend documentation.
Collapse this transcript
The model
00:00Because Zend only provides rudimentary tools for handling database access,
00:04creating the model is going to be very customized and frankly a cumbersome
00:07exercise. That said, it's really not that different from creating a generic PHP
00:12application. You create the database in SQL, you create a class or set of
00:17classes to interact with the database, and you create a class to represent the
00:20data in PHP object-oriented programming format.
00:24Fortunately, the Zend command-line tool is there to provide a little assistance.
00:28The first thing to know is structural. Zend is going to use a front controller
00:32class to load different modules, which are aspects of our application. Basically,
00:37everything that's in the application directory in the Zend project is a module.
00:41In fact, this is the default module of your application. An instance of a module
00:45autoloader class is created every time the bootstrap is initialized, and it is
00:50going to use the prefix of that model. In our case, it's going to be
00:53application_. So our model, table, and form classes are all going to have that prefix.
00:59The first thing you need to do in order to interact with the database is we need
01:03to edit our application.ini file. We need to set a few parameters that are going
01:08to specifically determine how our Zend application will talk to the database. So
01:13these are all going to be in the resources element, and they're all also going to
01:18be in the sub element db. The first one is the adapter type. In this case, we're
01:25going to call it PDO_MYSQL. We're going to use PHP's data object models to
01:31handle everything, and that kind of happens behind the scenes.
01:34The next element we're going to create is going to be in the params. In fact,
01:40all the rest of them will be params, and these are just how it's going to
01:42interact with the database. In this case, it's the host, which is localhost.
01:48Because we're using a nonstandard port with MAMP, we need to specify that as
01:54well. If you're using 3306, it's not crucial that you do this. I'm sorry. This
02:02should be port. Then we're going to type in our dbname, which is the name of our
02:08database, php_frameworks_test. resources.db.params, the username that we're
02:21going to use to access the database--in this case, root. It's not always a best
02:25practice to use the root, but since I'm just in the development environment
02:28here, it should work just fine. And make sure you spell all these correctly. It
02:32can be easy when you're typing a lot to make mistakes. The password, you'll put
02:38whatever your password is, and that should pretty much set up everything so that
02:42it can be accessed. So this allows database access from your application to
02:48whatever your database is, whether it's in MySQL or in another type of database.
02:53You can also use the Zend command-line tool to do this by typing "zf configure
02:58db-adapter" at the command line. Now where you go from here, Zend is pretty much
03:04agnostic about. You could create your own custom model if you wanted to. You
03:09could use Doctrine. You could use that Zend_Db class, which uses a special
03:14pattern called the table data gateway or row data gateway. We're going to use
03:18the Zend_Db_Table, which uses the data mapper pattern. It's what they use in the
03:23Zend QuickStart docs, and it seems to be sort of the default usage, so we're going to use that.
03:28In order to do this, the first thing we'll need to do is create a class that is
03:33going to represent the Db_Table. Go to our command line. We can use the special
03:38command zf create db-table to do this. And first you're going to name whatever
03:45you want to call your Db_Table. In this case, we're just going to call it
03:49MailList, and then you specify the name of the table in the database that this
03:53is going to correspond to. Note that I'm still in the explorezend folder in the
03:57htdocs directory of my MAMP.
04:00So it created a new file for me, MailList.php. And if I go back to my text, you
04:06can see in the models directory I now have a new folder DbTable, and I have a
04:11MailList.php. And inside of it I'm creating this class
04:14Application_Model_DbTable_MailList, and it's setting the Database Table to
04:20mailing_list. This file will also store the name of the data table in the _name
04:26protected variable.
04:27Now let's create our value object or data transfer object, which is going to hold
04:32the data. It's going to essentially have a property for each of the fields in
04:36our database table. We can go back to the command line and we can use a tool,
04:41create model, and you simply call it whatever you want to call it. In this
04:46instance, we're going to call it MailList. And it will create a new class for us
04:51directly in the models directory. There is nothing in this class, however, so
04:56we're going to have to copy and paste some information that I've already used to create this data.
05:01Let's take a look at the code that we have here. Notice that we have a protected
05:08variable that corresponds basically with each of the field names inside my
05:12database table, but it starts with an underscore. Then if you scroll down,
05:17you'll see that I have a get and a set property that correspond to each of
05:21those, and they'll each take some kind of value for the set or they'll return
05:25some kind of value for the get. So this is essentially a way of interacting with
05:30this data and saving one record from the database.
05:34The last thing we'll need to do is create our mapper. The mapper is what's going
05:39to provide the functionality to integrate between this data, which is the Zend
05:43application, and the data that's persisting in the database. Again, from the
05:47command line, I can create another class, create model. I'm going to call this
05:52MailListMapper. And if you noticed last time, all it really does when you run
05:57create model is create an empty class for you. It doesn't actually write any of
06:02the methods or give you any head start on anything. But it's a quick way to get
06:06the class created. And so I'm going to go ahead and copy in some information
06:10I've already created, a mapper class. And this mapper class has the core
06:20functionality that I need. This mapper class stores the Db_Table that is used to
06:25access the database table. If you remember, the Db_Table_Abstract has a number
06:30of methods built into it that allow us to interact with the database. So by
06:35creating an instance of it, we create a mapping to our database.
06:39So I've created this public function save. It takes one argument, which is our
06:44data object. It then sets all of the variables to an array. The field names of
06:49the array correspond to the names in our database. We can then just call the
06:54simple insert or update method that will then do all the work to the database
06:59that we need to do. So this is one example where you do get a little bit of a
07:03shortcut, a little bit of help from Zend, and that's the abstract class that's
07:07providing this information.
07:09So as you've seen, Zend is really a model-agnostic framework, which is to say
07:14you can basically do things how you like. It does provide a few classes that
07:18represent some common patterns. Zend_Db is one of them, and the one we are in
07:22over here is Zend_Db_Table. These can help you get started, but if you want to
07:27use something different, either in your own system or Doctrine or another kind
07:31of ORM, it's not only acceptable, but it'll likely save you time in the long run.
Collapse this transcript
Controller, form, and actions
00:00The last step to our subscription form application is to set up the form itself
00:05and tie everything together so that when the user submits information, it's
00:08going to get saved to our database. In this movie, we're going to first look at
00:11how we can use Zend framework's form API to generate the form programmatically,
00:16and then we're going to tie it all together so the form submission is saved to our database.
00:20So, the first thing we're going to do is we're going to go ahead and use the
00:23Zend tool to create a new form. So I'm at the command line interface and I'm in
00:29my explorezend folder, which is in the htdocs directory of my MAMP installation.
00:35So I can go ahead and type "zf create form" and I'm just going to call it index.
00:41Usually the form matches whatever the controller is that you're using, or the
00:45method of the controller. In this case, it's index. So, let's go ahead and create it.
00:50And if you go into the application directory now, you can see it's created a new
00:54folder, forms, and it's placed index.php inside of it. So, what I'm going to do
01:00is I'm going to copy a form that I've already created into this form. You can
01:09see it's called Application_Form_Index. So it's always whatever the name of your
01:16module is, which is application, form, and then whatever the name your form is
01:20after that, and then extend Zend_Form.
01:23By extending Zend_Form, I have a number of methods available to me, including
01:27setMethod, so I can set the method of the form to post, and then addElement is a
01:32really important method. This allows me to add all my form elements into my
01:37form. The majority of these are text inputs.
01:41So, in order to add text inputs, all I need to do is pass for the first
01:45parameter text. The second parameter is going to be the name of the input
01:50itself. It's going to be the identifier. And we want to make sure that this
01:54matches with the corresponding field from our data model. So in this case,
01:59first_name, last_name, email, these all correspond to our data model.
02:04The next element is an array, and this array will take a number of different
02:08options. The key one that you'll probably always want to use is the label,
02:13because this will set the label for the form element, but there are some other
02:16options. If you look at the Email Address, you can see I set the label, but I
02:20also set the field to be required. I create a filter, which will make sure to
02:25trim it so that there're no extra characters in it. And finally, I added a
02:28validator. You can add as many as you want. In this instance, I'm just adding a
02:33single one for email address that will make sure that whatever the user enters,
02:37it's in the valid email address format.
02:39You can also use the addElement to add other elements like check boxes and radio
02:44buttons. You can see the way that it works is slightly different. On line 42,
02:49I'm adding a new element. When you do it this way, you essentially pass an
02:54object in, and the object is going to be some instance of a Zend_Form_Element. In
03:00this case, I'm using the MultiCheckbox, and I'm calling it interests. I'm passing
03:04through all the multiple options that are available.
03:07I also do the same thing to add a radio button, so the user can select how many
03:11tours they've been on. You can add a text area. It's pretty much similar to
03:16adding a text input. Finally, I add a submit button, and you can set the label if
03:21you want to. The last thing I do is I'll add a hash element. This provides
03:25protection for me from cross-site hacks.
03:28So, now that I've got my form all built, the next thing to do is to add it into
03:34my controller and into the view. So let's go ahead and go into the view itself.
03:40So I'm going to go into the index.phtml. And you can see this already has the old
03:45form in it. So let's go ahead and highlight that entire form, and let's just get
03:50rid of it. Since we're going to replace it with our existing form, we don't need it any longer.
03:55So, now I'm going to go ahead and I'm going to use, in a PHP block, I'm going to
04:00call a special function. In this case, I have a property called form that's
04:06going to be passed into me from the controller, and I'll show you how I do that
04:09in a second. And it will have a setAction method. The action is going to
04:15determine where the form is going to be submitted to. In this case, I'm going to
04:20send it to this->url. So it's essentially going to submit to the current page.
04:28And then the next thing I need to do is simply echo the form itself, which will
04:36output it into my view.
04:38Since I removed the old form, I want to go into my old layout and get rid of
04:43things that are related to that old form. Specifically, I had a couple of
04:46JavaScripts up here on line 6 and 7 that I can remove. Since it was built in
04:51Dreamweaver, they were using the spry framework to create it. There's also a
04:55code block down at the bottom around Line 50, and I can get rid of that as well.
05:00That's just going to prevent any JavaScript errors that might have occurred from
05:04having that additional scripting.
05:06The final thing that I need to do is add this element into the controller
05:10itself. So you can see in the indexAction, all I really need to do in here to
05:18output the form is set that property. So this->view->form = form. And then I'm
05:30going to create that form object, and all it's going to be is a new instance of
05:35that form that I created. Application_Form_Index. So, make sure you save
05:44everything, and let's go ahead and run it.
05:49So, you can see here I've got my design wrapped. I have all of my form elements
05:55laid out. It's not super pretty. There're a lot of ways you can use decorators
06:00and other elements inside of the Zend framework to make it look prettier, but I
06:04didn't want to make the form overly complicated.
06:06So, there's our form. The last thing I need to do is make it so that when I
06:11submit it, it will enter the data into the database, as well as provide any
06:16validation. So let's go back into our controller. The first thing what we're
06:22going to do is we're going to get the request object. So we're going to set a
06:27new variable called request and set it equal to this->getRequest. So now I have
06:35my request object and then after my instantiation but before I output the form,
06:42I'm going to add a little if block.
06:44It's going to be a two-layer if block. The first layer is going to check first.
06:48I am going to use the request object. Say if request->isPost. In other words, if
06:56the form has been submitted then execute what's in this code block. And then
07:03we'll do another test here and we'll say if form->isValid, and then we'll pass
07:11our data, request->getPost. So that checks for validity and then once you've
07:23determined that, then we can use our model to save the data and to write it to
07:31the database, so Application_Model_MailList.
07:38And let's say mapper, so we'll create an instance of our mapper object, which
07:43does all of the database interaction. And we simply call the save method and
07:50we'll pass to it. In order to make sure that the MailList data is in there,
07:56we're going to pass to the constructor of our object form->getValues. So this
08:04will then get all the values from the form submission, put it into our data
08:09model, and that is what will then get saved to the database.
08:12Now, if you want to, optionally, you can use helpers to do other things to
08:17include elements. You can also set variables. In this case, what we're going to
08:21do is we're going to go ahead and save this and then _helper, and we're going to
08:28use the redirector helper and this is going to redirect the user to the thanks
08:35method of our controller. And we could have that do whatever we want. So there's
08:40our basic form submission handling.
08:43So, the last thing we need to do is go back to our form and test that it's
08:47working. So, we can go ahead and fill it out. Drew. Now, if I don't put
08:54the correct email address in, so notice, I'll just put drew, but not at or
09:00anything, it should determine that I've input that incorrectly and reload the
09:06form. And you can see, it even puts a message right by that form field. It says
09:11"'drew' is not a valid email address in the basic format local-part@hostname."
09:16So, what I'll do is I'll type in drew@lynda.com, which I don't believe is an
09:21email address. It's certainly not mine. And we can submit it with a correct
09:26email address format. Now, what I suspect to get is an error page because it's
09:31going to go to an action that doesn't exist, which is thanks, but basically,
09:37it's all working. So we've implemented it all. So this is a Zend framework
09:43application in action.
09:45You can see there're a few more steps involved that writing a basic PHP
09:48application, but the result is a much more organized code base. The Zend
09:53framework does take a little getting used to. There are a lot of features, but
09:58there's also a lot of complexity getting started. At each point of the code we
10:02wrote in this application, there're tons of jumping-off points that you can take
10:06to go into deeper complexity and add other features. The advantage is that this
10:11framework is well used and there're a lot of tools available to you once you get
10:15past that initial learning curve.
Collapse this transcript
Taking it from here
00:00What we've covered in the Zend movies thus far is really just an introduction to
00:03the MVC aspect of using Zend. While this is largely important, the true
00:08advantage of the Zend framework is the depth and breadth of the libraries available to you.
00:13The Zend Framework Reference Guide is a great starting point to see some of the
00:17tools available, in alphabetical order. You can see Zend has an ACL, or Access
00:24Control List, methodology that allows you to find different groups and who can
00:27access what parts of an application.
00:29It also has an authorization functionality. It has the ability to integrate with
00:34barcodes. You can implement caching that allows you to have intelligent caching
00:40and manage all of the data in your cache, both between your database and in
00:45caching templates themselves. You can implement CAPTCHA. You can access cloud
00:50data. You can configure all kinds of different things directly and
00:54programmatically through the API. There's a number of different features in
00:58there to handle dates and currencies and formatting different type of strings.
01:03Dojo is a really powerful tool that you can use to do all kinds of view helpers
01:08and decorators and elements. The Dojo application itself has its own web site at
01:14dojotoolkit.org, and if you go to the Features, you can see all the things that
01:19are available to you.
01:21There's a lot of tools for desktop that allow you to integrate different
01:25elements into your forms. You can create really nice interfaces right out of the
01:30box, and because it integrates with Zend, it's really just a matter of calling
01:34on methods of different classes.
01:37It can enhance your mobile application development using HTML5 and CSS3. It's a
01:43really beneficial way for creating graphs and tools and all kinds of UI elements
01:48that you wouldn't otherwise have available to you.
01:51Zend also has the Gdata services, which allow you to talk to Google, and you can
01:56integrate with Google Calendars, with Google Docs, with YouTube data, Picasa, all
02:03kinds of different things that allow you to pull this functionality into your own applications.
02:07One last thing to look at is to keep an eye on the Zend 2.0.0 beta. 2.0.0
02:13promises to be a much more accessible version of Zend, and in addition, it will
02:19perform well, and we can assume that they're going to add a number of different
02:23features and libraries along with it.
02:24Because of the contribution of so many large companies to the Zend framework, it
02:29really does have a lot of tools for developers. AMF, Dojo, Google, and other
02:34services, authorization email, and other well-tested APIs can give you such a
02:40head start in so many aspects of your coding.
02:43So through these movies you can get a pretty good picture of the Zend framework.
02:47Some complexity in terms of getting started, but loads of libraries, and
02:51flexibility in terms of how you actually implement your applications.
02:55If you think this is a framework worth checking out, I definitely recommend
02:59delving deeper into the API, and most importantly, diving in with your own
03:03application and seeing how it goes.
Collapse this transcript
4. Introducing Symfony
Intro to Symfony 2.0
00:00Symfony is one of the more popular PHP frameworks out there, and for good
00:03reason. They have a solid code base, a decent community, and good documentation.
00:08The framework itself has a lot to offer. It's flexible, uses a decent and
00:12modular architecture, it has a pretty deep toolbox, and some nice rapid
00:16application development features. It's not too overwhelming to get started in either.
00:20Symfony is backed by Sensio Labs. It's a French company. They don't have the
00:25clout and they don't have the big partners like Zend does, but they still offer
00:29support and service if you need it.
00:30Symfony2 is one of the new 2.0 frameworks. These are built, generally speaking, on
00:35PHP 5.3, which has a good design and object-oriented layout.
00:39Symfony is built on the MIT license, which is flexible, and it's also good for
00:44commercial distribution.
00:45Symfony2 is more open and flexible than previous versions of Symfony have been.
00:49You no longer need to use it as a full stack if you just wish to use parts of it.
00:53There is a good community, and you can also find a lot of third-party bundles to download.
00:57In terms of performance, historically it's been one of the criticisms of Symfony
01:02in versions 1.2, 3, and 4. However, in Symfony2 they've re-architectured it to do
01:08some intelligent caching, and it should perform a lot better. Their personal
01:12tests say it's three times faster than the previous version and also faster than Zend 1.10.
01:18In terms of structure, it's a flexible full stack. On the model end, it's going
01:23to use Doctrine 2. For the view layer, you're going to use Twig, which is a
01:28template engine. In both instances if you wish to use your own methodology,
01:32that's acceptable as well.
01:33A lot of the work in Symfony is done using YAML or other config files. They've
01:39made it more flexible now, so that if you wish, you can use PHP, XML, or PHP
01:44annotations instead of using the YAML.
01:46Using Symfony, you package your application into bundles. These bundles are easy
01:51to distribute, and additionally, it's also easy to use third-party bundles to
01:55save some coding time.
01:56With Symfony2, it's a lot easier to use other PHP code. If you want, you don't
02:01even have to use the MVC layer of Symfony; you can just use the classes.
02:06There are a few core libraries. The core MVC libraries will help you with rapid
02:11application development. Additionally, there is support for
02:14internationalization, a security construct, and the ability to manage your assets.
02:18The development tools for logging, testing, and caching your application are also included.
02:24One of the key tenets of Symfony is to use Java patterns, so it's a very
02:28well-structured application development environment.
02:31Symfony2 is the latest release from Sensio Labs, and it holds a lot of promise
02:35as a decent-performing, easy-to-learn, and modular framework for PHP developers.
02:39Of course, the only way to really know is try, so let's dive in and see what we discover.
Collapse this transcript
Symfony concepts, advantages, and issues
00:00Symfony has historically been a full- stack framework. However, with Symfony2,
00:04there is a little more flexibility to use elements from it or not. But its real
00:08strength lies in the stack it provides, and once you get a feel for the way
00:10Symfony handles things, you'll likely find it fairly easy to get started and work with.
00:15When you download a Symfony distribution, it's basically just a sample
00:19application with the directory structure, libraries, some bundles, and a default
00:24configuration. So it has the core of what you need and some samples to help you
00:28get started with it. You may need to edit some permissions on your file system
00:32to make sure that Symfony can work correctly.
00:34Additionally, if you want to have clean URLs, you can do some web server
00:38mapping. In terms of organizing the code, Symfony is very object oriented. It
00:44has a web-based tester, which will make sure that everything is configured
00:47properly, and you can use this to set up your database as well. It's a full-stack
00:52framework, but if you want, you can just use parts of it.
00:55Many things that you do in Symfony are done by doing configuration files.
00:59Historically, this was done in YAML, but now you can use XML, PHP, or PHP
01:04Annotations. These are used to handle routing, to handle setting up the data
01:09model, and a number of other elements within Symfony environment.
01:12When you get to the view layer, you can write your view using the Twig template
01:16engine, or you can use straight PHP if you prefer.
01:19By default, the model uses Doctrine 2. This is also a 5.3 PHP application. If
01:26you prefer, you can write these in straight PHP, but Doctrine offers a lot of
01:30shorthand tools for creating your model.
01:32Symfony2 also offers a new service container, which can handle instantiation of
01:37your components and will also handle lazy loading and managed dependencies.
01:41While Symfony is great for rapid application development, some are critical that
01:46because we need to learn new constructs, this can add a little time to the
01:50learning curve. One of the advantages of 2.0 is that it adds flexibility in
01:55terms of how you write this. You can write things in XML, PHP, or Annotations,
01:59meaning you don't have to learn a new language like YAML.
02:02Full-stack frameworks have their own issues. These can include performance. They
02:07can include the ability not to reuse code written in one framework in other PHP
02:13environments. 2.0 is supposedly more flexible, but some argue that this defeats
02:18the purpose. The real purpose of using a framework is specifically to have those
02:23advantages that you get by using a full stack.
02:25The question of performance remains. It's always tricky to benchmark and compare
02:30these applications on a one-to-one basis.
02:322.0 did add a number of features to enhance performance, including intelligent
02:37caching and also lazy loading.
02:39One other criticism of Symfony is that for a full-stack environment, some think
02:43it still requires too much typing to do simple tasks.
02:46As one of the new breed of PHP frameworks, Symfony2 has been well-received in
02:50its short life. It boasts better performance, good MVC architecture, flexibility
02:56in terms of whether you want to use it as a full stack or not, fast coding tools
03:00using config files and templates, and a bundle architecture that makes for easy
03:04sharing and integration of PHP code.
Collapse this transcript
Symfony components
00:00A key consideration of a framework is what components are available, which is
00:04really to say what pre-tested code that I might want to include in my
00:08application is available to me.
00:10In the case of Symphony, there are a few key components, but because of the new
00:13bundle architecture, many more third- party components are available as well.
00:17Many of Symfony's components are for the framework itself,
00:21components for handling data such as cookies and generating client code,
00:25Doctrine 2 for data handling, the controller and routing classes, and of course
00:30the template engine of Twig. There are also a number of components that will
00:33help you with development, for testing, for handling the services, that is, the
00:37service container, the forms and validation API. The application aesthetic is
00:43for handling assets like JavaScript and CSS libraries.
00:46There are also tools for logging, security, and caching. Additionally, you can
00:51use SwiftMail, which is good for sending emails, and there is also a number of
00:55classes for translation and internationalization.
00:58There's a web site called knpbundles.com. At this web site you can find a number
01:03of bundles written by third-party developers that can help you shortcut a lot of
01:08your development process.
01:09So Symfony2 contains a number of libraries and constructs for the main aspects
01:14you'll need for your PHP development: the MVC, testing and logging, building
01:19forms, handling authentication, and all of that kind of stuff.
01:23If you wish to implement AJAX, you can simply use the library of your choice,
01:27jQuery or Dojo or any other library you might want to use.
01:30Another thing to remember is that because of the bundle architecture, there is
01:34always the possibility someone is already created a bundle to do what you need to do.
Collapse this transcript
Symfony flow and architecture
00:00Let's take a look from the 10,000 foot level of how the Symfony2 framework is
00:04laid out. This should enable us to hit the ground running when we start writing our application.
00:08The first thing you want to look at is the directory structure of the sample
00:12application in the Symfony distribution. You can see there are some key
00:16directories here. The main ones that you'll be concerned with is the app
00:19directory, where a lot of your config files are going to reside. It's also going
00:24to hold your cache information, your logs, and some other libraries.
00:27The SRC, or source, directory is where your PHP code is going to reside. It's
00:32going to be broken down by bundles, and each of these bundles will be grouped
00:37like packages would be. Inside of each bundle you'll have a directory for your
00:41controller and you'll have a resources directory where you can put your view
00:45templates, your config files, if you have any, and then any public web files.
00:50Additionally, you can store your tests in here for unit testing. The vendor
00:54directory is going to hold the core libraries for Symfony and Doctrine and
00:58anything else you might be using. In addition, and we don't have listed here, is
01:03your public web directory, and there's also a bin directory that's going hold the console.
01:08The model layer in Symfony is handled by Doctrine 2. If you want, you can use
01:13other methodologies. Using Doctrine 2, you can write the classes yourself, or
01:18you can use the console to generate a lot of code. You're going to need to
01:22create an entity class that's going to hold your different data types. These
01:26might be users or products or things like that. You're then going to map your
01:30class, your entity class, to connect to the database.
01:34Doctrine can actually handle the generation of your database tables and fields.
01:39Once you've created these classes, you'll use Doctrine to persist the data.
01:44Whenever you make changes to the mapping class, it will save those changes to the database.
01:49For example, if you add a new instance of an entity class, it will create an
01:54insert and insert it into your database. You can also use repositories inside of
01:58Doctrine. This can allow you to retrieve objects.
02:01Doctrine is its own application and its own project that's located at
02:06doctrine-project.org.
02:09The view layer is template based. If you want, you can use PHP templates, but
02:14generally the Twig template engine is what a lot of people like to use. The
02:19advantage of using Twig is that it provides a lot of shortcuts.
02:22For example, if you want to output a variable, you simply wrap the name of the
02:26variable in curly braces. This would be different from the PHP methodology,
02:31where you'd need to create a PHP tag, write the echo command, and then output your variable.
02:36Twig also offers a number of filters, some mechanisms for looping and other
02:41things, all of your Twig templates will be cached, and Twig templates support
02:46inheritance so one template can inherit another.
02:49You can even embed controllers directly inside of your templates. Let's talk
02:54about the controller and how we handle routing inside of Symfony. The routing is
02:58typically handled using configuration files, which can be YAML, XML, PHP.
03:03The URL is given to determine which controller to execute and which function of
03:08that controller will render the current page. The controller then will set up
03:12the application. It can perform a number of different actions, including talking
03:16to the database, specifically through your Doctrine models. And it can render
03:20the templates directly.
03:22Symfony supports dynamic routing, meaning you can embed variables directly in the URL.
03:28For example, this would open the products controller, call on the edit function,
03:32and pass a variable with the value 123 to that function.
03:36There are a couple of different environments built-in. By default, you're going
03:40to run the app.php file. This will be the application or production environment.
03:46You can also use the _dev environment at app_dev.php.
03:51The _dev is going to have a toolbar by default, and it's also going to make sure
03:57that your cache is cleared on every execution.
03:59As you can see, Symfony has a pretty solid methodology for implementing the MVC
04:04and separation of concerns. It even goes so far as to use entirely different
04:08technologies, like Doctrine and Twig, for different layers of the application.
04:12Additionally, Symfony uses a lot of configuration files that can now be written
04:17in XML and sometimes PHP, in addition to YAML. The idea is that this will save
04:22you coding time, but ultimately it will be up to you if this paradigm makes sense.
Collapse this transcript
5. Using Symfony
Downloading and setting up Symfony
00:00You can download Symfony2 from the symfony.com web site. When you download
00:04Symfony, it's basically a pre-built application along with the framework library
00:09that you can install and begin customizing right away. The web site you'll
00:13specifically download at is symfony.com/download, and you will see you have a
00:17few options of how you want to download it.
00:20You can download in ZIP or in TAR format, and additionally, you can download
00:25without vendors. Without vendors means it's not going to include the core
00:30Symfony and Doctrine and other libraries with it. You can grab those later when
00:35you go to deploy your application or when you're developing. This can be
00:38advantageous if you want to make sure you have the latest version of your
00:42vendors and make it easy to download and install.
00:45Additionally, you can download from Git. Symfony is available at the github.com
00:50web site in the repository github.com/ symfony/symfony. Once you've downloaded
00:56Symfony, you'll then want to extract your application from your ZIP file or your
01:02Tarball and then copy that directory and paste it into the web root of your PHP
01:10server. In this case, I'm using MAMP, so I am going to go into the htdoc, and I
01:15am going to paste it directly in htdocs, in a folder named Symfony. You can name
01:20this what you want. This will be what the URL is. Symfony is just fine.
01:24There are a couple of command-line tools that will help you get set up. One of
01:28them is called the check.php, and this will make sure that everything is
01:33necessary and PHP is configured correctly to run Symfony. In your command-line
01:38interface, you want to first go to the directory where you installed Symfony. So
01:43we are going to cd /Applications/MAMP/htdocs/Symfony.
01:51Now we are going to use the PHP command-line interface and point to
01:56app/check.php. One thing to keep in mind when you run this is that it uses the
02:03php.ini file to determine what the settings are. The php.ini file in my case is
02:11running from Mac OS X's default PHP server. That is not the same php.ini file
02:18that I am using for MAMP, so these may or may not be the correct settings. But
02:21it's a good thing to check if you know you're using the right php.ini file. So
02:26that's one thing to be aware of.
02:28If you know that it's using the right php.ini file, then you can see that it's
02:33checking the version, and it will also make sure that the date.timezone setting
02:37is set. In my case, it's telling me it's not set, but I know that MAMP actually
02:42already has that set in its php.ini file, so I am pretty sure I'm okay. But I
02:47wanted to show you how to use this command-line interface.
02:50There's also a special config.php file that will check your Symfony installation
02:56inside of your web environment. So we are going to go to that file and it's
03:00going to be at localhost: 8888. If you're using another PHP server like WAMP or
03:06XAMPP, you are not going to have to worry about that port number. And then we'll
03:10go into Symfony/web/config.php. So it says "Welcome to your new Symfony project"
03:18and it will give you some recommendations about what you should do in order to
03:22make sure that Symfony is configured properly for your environment.
03:26Most of these are done inside of the php.ini file, so make sure you've done it
03:31to the correct php.ini file, for one thing, and that you restart the server to
03:35make sure that those changes will take effect.
03:38In my case, I don't have any alerts; I only have recommendations. So let's go
03:42ahead and take a look at the demo app we're going to use. This particular one is
03:46at web/app, and we are going to go to the dev environment, which is _dev.php.
03:52You will see when it loads, it loads up my document and my application, and it also
03:58shows me this Debug bar at the bottom. This Debug bar can be really valuable
04:03because it will tell you different information about what's going on. For
04:06example, you can see it's going to the WelcomeController, calling the
04:10indexAction method. It also shows you how long it took for it to load.
04:15So that's the basic of getting Symfony downloaded and installed. All the main
04:19files we need to get started, including some demos that have bundles using the
04:24main constructs of the framework, like authentication and security, are installed
04:28and we can customize, or we can remove them. The next step is going to be
04:32creating our own bundles and building our own application.
Collapse this transcript
Routing, controllers, and bundles
00:00Once you've downloaded and set up your Symfony project, the next thing you need
00:03to do is some configuration to tell your Symfony application how it's going to
00:07respond to requests, that is, we are going to tell it which controller to
00:10access. The way that you package your code in Symfony is using bundles, so we
00:15are going to create our own bundle and inside of there will be our new
00:18controller. When you want to create a new bundle, there are a couple of ways you can do it.
00:23One is you can create each thing individually by creating your own directory
00:27with your own controllers directory and your controller PHP and do everything
00:31manually. Another way to do it is using the PHP app console to generate it. If
00:36you go into your Symfony directory that you downloaded into your PHP web root,
00:41you can simply type php app/console. So this will run the console command-line
00:47interface application that's in the app directory in your Symfony install and
00:51specify generate:bundle, and this will generate the bundle for you.
00:57You can optionally pass some parameters. In this case, I am going to set up a
01:01namespace. I am going to call it Acme /Bundle/JoinBundle. You can call it
01:08whatever you want. Acme is the default package space that they use in the demos,
01:12so I am just going to go ahead and use that, and I'm calling it JoinBundle
01:15because this application is to allow users to join our email list.
01:19When you're done, go ahead and hit Enter and you can see it loads the bundle
01:23generator, and it will give you some information about what you are doing, and
01:26then it will start to walk you through a wizard. The bundle namespace you can
01:30see in the square braces, is defaulting to what I entered which is how it should
01:34behave. So if I hit Enter, it will use the default value. Again, you can use
01:38these default values at will. The bundle name is just fine as AcmeJoinBundle.
01:43That's what it will be referred to from within the application. We do want to
01:47write it into the source directory of our Symfony install.
01:50The Configuration format, we are going to use YML instead of annotation. Do we
01:55want to generate the whole directory structure? We are going to go ahead and do
01:58that, just so you can see. Basically, it's going to create a copy of some of the
02:02demo controller information and put it in there. Now it's going to make sure
02:05that you've entered everything correctly. You can go ahead and confirm that.
02:09Then it will ask you a couple of options.
02:11There's one, do you want to update the Kernel? And yes, we do. The Kernel
02:15essentially loads all these bundles into the application. Let's go ahead and do
02:19that so we don't have to mess around with it. Then do we want to update the
02:22routing? I'm going to say no, we don't, because I am going to go and show you
02:25how to do that, because it's an important thing to learn inside of Symfony.
02:31So it's warning me and telling me that the routing is not configured. But let's
02:32go ahead and go into our Text Editor and if you look down below inside of the
02:37Acme directory, you can see it's created this bundle JoinBundle file. Let's go
02:42ahead and open this up a little more. And inside of this directory, you can see
02:46I have a Controller and my DefaultController is set up here. Notice it has one
02:51single action, indexAction, that takes name property, and is going to point to
02:56this something here to do some rendering. What this is is this is pointing to
03:01the default directory for the views, and it's going in there and grabbing
03:05index.html.twig, and that's located down here inside the Resources directory.
03:12Notice twig uses this special annotation to output that variable. So let's go ahead
03:18and set up a routing for this. But before we do that, I want to show you how the
03:21demo application is set up. This is installed by default with your Symfony
03:26download. So if you go to app_dev. php in the web root of your Symfony
03:31application, your routing will always come after that. You can map this if you
03:35want to, using the .htaccess file in Apache or in your web server in a different
03:40manner if you are using a different web server, but this is going to go to
03:44whatever is mapped for the demo. In this case, it's going to the demo controller.
03:48I am going to go to the Hello World application, which is basically what our
03:51index is. The World here is the name variable. So if you wanted to change this
03:57to your own name, it would then output that information. So it's a dynamic URL
04:02routing, and if we wanted to do this for our application, we can do it in the routing.yml file.
04:08In the instance of the demo application, it's being done using annotation. So
04:13they have it all configured in that way. If you look at the demo controller, you
04:17can see these annotations before each of these actions that configure the
04:21routing, and it's the @Route annotation that specifically is doing it. So if you
04:25look, you have @Route and then hello, and then we use the name variable, and
04:30that's setting it up. We are essentially going to do the same basic thing, only
04:34we are going to use YAML.
04:36So in our routing file, we can go ahead and type in whatever you want to call
04:40it. In this case, we are just going to call it join. It's kind of like the
04:44identifier inside of the application. Then you define the pattern that is going
04:48to be parsed out when it's being routed through the Symfony application.
04:53In this case, it's going to go to /join/ and then we are going to put our pattern
04:59for our variable. Once you have entered the pattern, you will then enter how you
05:03want the pattern to be rendered. In this instance, you are going to specify the
05:07default property and it's going to be an array, and we are going to define the
05:10controller property of the array.
05:12So we start with _controller and then after the colon, we can specify
05:17AcmeJoinBundle, which is our bundle, and this is the name we created when we ran
05:21the generate bundle. Then you specify the controller, which is default, and then
05:27you specify the action in the controller you want to execute. In this case, it
05:30was the indexAction.
05:31So we are going to go ahead and call it index. So if you look in your
05:36DefaultController, you can see you have the indexAction that takes the variable
05:40name. So this should all be rendered correctly when we go to that URL. So let's
05:45go ahead and change from the demo controller and instead specify join/our name.
05:53You can see it simply outputs Hello Drew! because it takes that variable, passes
05:58to this, and uses this render method, which then opens our default template.
06:02Since we don't actually want to use a variable, we can go ahead and erase this
06:07and we can erase the passing of the array to the template. This will give us the
06:12opportunity to add our view when we are ready. So we can go ahead and go into
06:16the routing and erase the name value there as well. So now when we go to join,
06:22we are simply going to load the index.html.twig file. And we will configure this
06:28to show our application when we get into the view section.
06:31So as you can see, creating an application in Symfony2 is a matter of creating
06:35our own bundles, and our bundles are just groups of application code. We then
06:39configure routes that will direct specific URL patterns to execute methods
06:44within our controllers. Deciding when to create new bundles and controllers in
06:47routing is all up to you.
Collapse this transcript
The view: Twig and templates
00:00Symfony2 handles the view portion of applications through the use of templates.
00:04These templates can be written in PHP directly or you can write them using
00:07Twig. Twig is a template engine written for PHP that's also managed by Sensio
00:12Labs, who manage the Symfony project. Let's take a look at our controller. Inside
00:17of our controller, you can see I'm calling this render statement, and the render
00:20is calling a specific .twig file. The way this works is render is passing in this
00:26variable AcmeJoinBundle, which is the identifier of the bundle. After that, since
00:31it's rendering it, it's going to look for a specific directory inside of the
00:34View directory and pull this file.
00:37In this case, it's looking at the default directory. If you look inside of our
00:41bundle, you can see we have a views folder. This is where all the views are
00:45going to be stored for our application. The default directory is where this
00:49particular Twig file resides, and this is the file that eventually is being used
00:53to render our application.
00:56Why do we use Twig? There is a lot of good reasons to use Twig. The main one is
01:01that it offers shorthand notation for a lot of tasks, including outputting
01:05variables and looping through queries. Additionally, it ends up being rendered
01:10into PHP and that PHP can be cached. So we really don't lose any performance. We
01:15actually gain some. Twig also supports some advanced concepts like template
01:20inheritance that can be really helpful in our application development. And
01:23finally, because Twig is a different language that we're using for the View
01:26layer, it's less likely that we're going to be tempted to pull any sort of
01:30logic or anything into that layer. So it keeps our partitions clean.
01:34Let's go ahead and plug in the design that we had created into our application.
01:39I'm going to go into the _START_HTML folder and grab the join.htm file, which
01:45houses our design. So let's go ahead and open it in our text editor, and let's
01:50select everything and copy it to the clipboard. I'm then going to go back into
01:54my text editor and I'm going to create a new file inside of this default folder,
02:00and I'm going to call it base.html.twig. This is going to be the base template
02:05that I can use for any page that's going to be rendered in my application. So
02:08it's essentially going to be the design wrapper.
02:12So let's go ahead and paste it all in, and now we're going to clean it up a
02:15little bit. For one thing, we're not going to use any of the spry code that we
02:20have because it's not necessary any longer. We're going to write our forms and
02:23all of our JavaScript directly into our application. So go ahead and delete this
02:28script block down at the bottom of the file. Then we can scroll up to where
02:32other files are being included. Spry also includes two CSS style sheets and a
02:37couple of JavaScripts, which we can also delete because we won't need those any longer.
02:42The last thing I'm going to need to change is the reference to the main style
02:46sheet, which also includes references to all the assets, background images, and
02:50things like that. The first thing I'm going to need to do is make sure those
02:52files are going to exist in my application. So I'm going to go back into the
02:57_START_HTML directory and I am going to copy the join_files. That folder houses
03:03not only that CSS but also all those images. I'm going to then go into my MAMP
03:09web root, into my Symfony doc, and into the web dock of the Symfony, which is
03:16essentially the web root of my Symfony application. And I can paste that item
03:21inside of here. Now that we've set that, we can use a special Twig function. So
03:27I'm going to wrap double curly braces around the reference to the style sheet,
03:32and I'm going to use the special function asset. And then I'm going to wrap a
03:36single quote around my style sheet location.
03:38What this will do is this will ensure no matter how this gets rendered, whatever
03:42the root is that's rendering this file, it's always going to look to that web
03:47root of my Symfony application into the join_files and get the main.css. So it
03:52will make sure that those links don't break.
03:54The final thing we need to do is cut out the content that's specific to this
03:57form. Because we're going to use this base template to render anything within
04:01our application, we want to have a straight, clean block that we can reuse in
04:06all of the different pieces. So let's delete everything inside of this div tag,
04:11the main article is the ID. So we'll go from this h1 all the way down to the
04:16closing form tag, and we can just delete it.
04:20So now we have this empty div tag, and we can define a special region that's
04:25going to be dynamic inside of our template. The way we do that is by using the
04:29block tag. So I'm going to specify block and I am going to give the block a
04:33name. In this case, I'm going to call it body. You could put information inside
04:39of the block if you wanted to, but in this instance, I'm simply going to end the
04:43block. So it's an empty block. I can use other templates that inherit this
04:48template to fill it in, which is exactly what we're going to do on our main index template.
04:53The way we do that, again, is with the curly braces. In the percent sign, we're
04:58going to specify that this template extends, and then you specify what template
05:03it's going to extend, AcmejoinBundle: Default:base.html.twig. So notice it uses
05:13the same notation that was used in my controller to reference the base.html
05:18inside the default folder that's in the views of the bundle. Symfony is smart
05:23enough to know that because I'm including it in the view layer that that's
05:26where it's going to look.
05:27The only other thing I really need to do in this template then is define what I
05:31want in the body block. So I just create a new block here, just like I did in my
05:37main template, and you can close the block as well. Whatever goes in between the
05:44open and closing block statement is what's going to be output for the block in
05:48the parent. If you want to output items in addition to what's in the parent,
05:52there is a way to do that. You'll have to refer to the twig docs to find out.
05:56So what I'm going to do is I'm simply going to specify <h1>Join our community,
06:02and I'm just going to put a few line breaks so that we've got some white space
06:07in our page and our doesn't look too compressed. So that's basically it. I've
06:11created this template that inherits from the original. It's going to fill the
06:16block in with this specific information.
06:18The last thing to do is to test it. So I'm going to open my browser. My
06:23application is located at Symfony/web/app_dev.php/join. So you can see it
06:33rendered everything appropriately. Notice that all the background images and
06:38colors, everything is in place, and it says, "Join our community," which is exactly
06:43what I wanted it to say.
06:44Additionally, you can use Twig to embed entire controllers in a single template.
06:49You can add shortcuts for outputting variables or looping through data sets.
06:53There is a ton of other things you can do. Symfony has a really comprehensive
06:57view layer, using templates. If you prefer, you could write these templates in
07:01PHP, but Symfony includes the Twig template engine, which can make our
07:05development a little faster and feature some powerful things like template
07:08inheritance. All in all, Symfony offers a fairly simple, yet powerful way of
07:13implementing your view layer.
Collapse this transcript
The model: Doctrine 2
00:00No matter what framework you use, a large amount of application design and tasks
00:04ultimately involves working with the data layer. Symfony2 comes bundled with the
00:08Doctrine Object Relational Mapping, or ORM, library. While Symfony doesn't require
00:13using Doctrine, it's definitely a tool worth learning. Let's have a look at how
00:17we can handle data with Symfony and Doctrine.
00:19The first thing we need to do for our database to work is go back to the
00:23config.php in the web root of our Symfony directory. From this page, you'll want to
00:29go to configure your Symfony application online. This will allow us to enter our
00:34database configuration information. So make sure to put the correct information
00:38to connect your database. In this case, we're using MySQL at localhost. You can
00:43set the name of your database to whatever you like. Symfony is fine. In the case
00:47of MAMP, it resides on port 8889. So I want to make sure to reference that port,
00:52and I want to enter the password, so that it can have access.
00:56Go ahead and click Next Step. This will generate a global secret. Among other
01:01things, this will make sure that you can't get hacked from cross-site request
01:05forgeries. If you want to generate a new secret, you can click the Generate key;
01:09otherwise, go ahead and go to the Next Step. What this will do is create a
01:13number of parameter values and set them into your parameters.ini config file.
01:20Now your application can connect to the database. There's one other
01:24configuration thing that we want to do to make sure that Symfony knows where and how
01:28to find your database.
01:29If you open the config.yml, this contains a number of different configurations.
01:34I'm going to add another line called unix_socket, and I'm going to point to the
01:39mysql.sock file that's located in the MAMP directory. This will ensure that
01:45Symfony is connecting to the correct version of MySQL on my computer. If you're
01:49not using MAMP, make sure that this is going to point to the correct mysql.sock.
01:53Using the command-line, I can generate my database directly. The first thing I
01:58want to do is go into my Symfony folder, which is in the Apache web root.
02:03Once I'm in here, I can use the PHP console to call on my special Doctrine
02:08functionality. So I'll specify doctrine:database:create, and this will create
02:15the database for me. So you can see, "Created database for connection named
02:19Symfony." So I now have an empty database. The next thing is to generate an
02:24entity, and there's two pieces to the entity. One is going to be the class
02:28itself, which is going to have all those fields that match my database table, and
02:32from there, I can actually generate the schema itself.
02:36So, let's go ahead and type in PHP app/ console, and again we're going to use
02:41Doctrine. But this time we're going to use the Generate command, and we're going
02:45to specify that it's an entity that we want to generate. We're going to give the
02:50entity a name. I'm going to call it AcmeJoinBundle, which is the ID of the
02:56bundle we've been working on, :MailList. Make sure you close that quote.
03:02This will open the entity generator. So we can follow generally the defaults.
03:07We're going to create it with the same shortcut name as we defined. The format
03:11we're going to use to configure, let's go ahead and keep it Annotation, but note
03:15that you could also use YAML or XML or PHP.
03:18So now what we're going to do is we're going to enter all of the fields that we
03:22need to represent our data. You don't need to worry about the ID as it will
03:26auto-generate that for you.
03:28So the first field that I'm going to add is first_name. I can use the default,
03:32which is a string, and the length can be 100 characters. I'm going to go ahead
03:37and generate the same for the last_name, and the email, address, the state_code,
03:49which is also a string. We'll give it 10 characters in case we have other
03:53countries or things we want to include. Zip and postal code like this, string,
04:02username. Oops, I keep doing that. password, bio, interests, and the last one is going
04:16to be the number of tours the user has gone on. We're going to store that just as a string.
04:22When you're done, go ahead and hit Enter again. It will ask us if we want to
04:30to confirm that you want to generate this. Go ahead and click Yes. So it
04:34generated the code, and let's go have a look at what got generated. So we're
04:39going to want to go into our Bundle, and notice there's a directory here called
04:43Entity. If you open it up, you'll see there's a MailList.php and inside of here,
04:49you can see that it's using the Doctrine mapping, and it also has these
04:53notations that define the tables, that define the columns. In this case, it's an
04:56generate an empty repository class, and we don't need to do that. Then it'll ask
04:58ID that's an auto-generated integer. We have the first_name and the last_name, and
05:03they all have the correct data types as we entered them.
05:05Those notations are what can be used by the Doctrine engine to actually generate
05:10your database schema. So let's go ahead and do that. The way we do that is use
05:15the php app/console again and call on doctrine. This time we're going to use
05:23schema and we're going to use update, and make sure to do the force. It's not
05:29always necessary, but this will make sure that if the table already exists, it
05:32will re-create it in the fashion you want it, based on the new version of the entity.
05:38If this should happen to you when you're trying to generate your schema, don't
05:41worry. This is a pretty common event. Basically, what's happening, you're most
05:44likely on Mac and it's looking for a php.ini file that has a setting that tells
05:50it what time zone it's in. Because by default PHP doesn't load an INI file, it's
05:55not there, so when it tries to do something with this daytime object--which it's
05:59doing behind the scenes to generate the schema--it's getting an error.
06:02The way you can fix this, probably the easiest way, is to go ahead and go into
06:07your TextMate and open your php.ini file from MAMP, which is going to be in
06:14Applications/MAMP/bin/php/php5.3.6, and then in the conf directory. Make sure
06:24this has it set, and you can do that by searching for date.timezone. You can see
06:30in this case mine is. It's set to America /Los_Angeles. So go ahead and save it,
06:35but this time Save As. if you use Command+Shift+G, it will open the Go To
06:41folder, and then you can go to the etc folder. Now save it as php.ini.
06:48If you already have a file there, you'll want to make sure to rename it before
06:52you do this. So now that you've corrected that error, go ahead and use the up
06:57arrow. This will bring back the last command you entered into the terminal. Hit
07:01Enter and you can see, "Updating database schema... Database schema updated
07:06successfully!" I'm going to go into MySQL Workbench, which is an application I've
07:13downloaded so that I can see my database. And you can see I have my Symfony
07:17table here. If I open the tables, you can see that I have my MailList table, and
07:24when I open it, the fields match the information that I entered into that
07:28command line. So, implementing the model layer of the Symfony2 application is
07:33generally done using the Doctrine ORM Library.
07:36In this unit, we discussed how to create a database, an entity object, and even
07:41generate the tables all by using a command-line tool. But this is really just a
07:45brief glimpse into all that Doctrine can do. We'll take another peek into
07:49Doctrine when we look at how we can use it to save our data to the database.
Collapse this transcript
Form and actions
00:00Invariably, a php and html application is at some point going to need a form
00:04that's going to can allow the user to enter information and probably save it to
00:08the database. This can be one of the more time-intensive task for developers.
00:12In Symfony2 we are going to use a form builder to accomplish this task. The
00:16first thing we are going to do is we are going to open up our controller and we
00:18are going to change the way that things are working in here. In fact, we can add
00:23our form directly in this indexAction of the controller.
00:27I have already created a form in a separate version of the controller. So let's
00:31go ahead and enter that and walk through what I have here. There are a few
00:34things to note. Note that I'm including the Request object and I am including
00:40the MailList class, which is my entity that I created in the model unit.
00:45The request is now being passed to the indexAction, which will allow us to
00:50process our form when it gets submitted. Inside my indexAction, I am creating an
00:55instance of my MailList entity, and then I'm using the getEntityManager method
01:00from the getDoctrine method, which is going to essentially get an entity manager,
01:05which I can use to then persist my data.
01:08So any changes to that MailList object, when I call a method on my entity
01:13manager, will be saved to the database. Once I've got my data all set up, I then
01:18create a new form and to do that, I create a variable called form on line 20 and
01:24I set it equal to the result of calling the createFormBuilder method, which is
01:28included in the Controller.
01:30Notice I am passing my entity in. That's going to be used to populate the data.
01:35The naming convention is very important. If you look at the first text in each
01:40of these add methods, what these are is these are setting variables that exist
01:45inside of that entity. Another thing to note is I'm using the PHP 5.3 syntax
01:51here so that I can chain my method calls, and it makes for a little shorter coding.
01:55Basically what I'm doing, if you're not familiar with this, is I'm calling the
01:58add method of the form object once I created it. But since I've chained them all
02:03it will just do them all in a row. So for the first few lines, from line 21 to
02:07line 27, I create a number of different text inputs, with the exception of the
02:12email. Notice I specify the type email. That will ensure that it validates that
02:17field to make sure it's a valid email.
02:20On line 22 I add a text area. Then I have a couple of different objects that I
02:24create. On line 30, I want to set the user's interests, which I want to be an
02:30array of check boxes. So I create this as a choice form object, and then I pass
02:34to it an array of the choices and finally, I specify that I want to allow
02:39multiple selection and that I want it to be expanded. If I'd said it not to be
02:43expanded but allow multiple, that would have then made it a multiple select
02:47list. However, since it's expanded, it will make it multiple check boxes.
02:52I also create a radio group. This one is also set up using the choice type;
02:57however, in this example I have set the multiple to false and I have set the
03:01expanded to true. So it's still expanded, and this will make it a radio button.
03:06If expanded was also false, it would have made it a select list. Then the last
03:11thing I do on my form object is I call getForm. This will take all of the fields
03:15I've added and will save it to the form object.
03:18We will look at how I process the request, but there's one more thing that I
03:22need to do to make sure that our form gets rendered appropriately. In the render
03:26call here where I load my template, I want to make sure to pass a variable
03:30called form, and I set it equal to the result of calling the create view method
03:35on my form object. So this will send it into my template.
03:39So now I just need to go into my template and I need to add a form. And you
03:45literally can just add a form tag, set the action equal to, I can use the
03:50special function called path, and this will then result to one of the mappings
03:55inside of Symphony. So this will go to the join path in my application. Because
04:00this is the join path, if you remember, this controller is set up for that path,
04:05this will essentially be a form that submits to itself.
04:08The method is going to be set to post, and you can use a special function to set
04:13the encoding type, called form_enctype, that's available inside of twig. You
04:18pass the form object to it. And I can close the form.
04:23Now the output all the inputs I created, I'll use a special function called
04:27form_widget and to it I will also pass that form object. So this will take the
04:34form that I have already created and will essentially expand it out into inputs
04:38in HTML. And of course I'm going to need a submit button, so input type equals
04:44submit, and I'll give it a value so Join Now.
04:49So once you have got your form set up, the next thing remaining is to test it.
04:54So let's go ahead and go to app_dev/join. When you test this file, you can see
05:01it obviously needs some style applied to it. But it did output all of our check
05:05boxes and our radio buttons.
05:07We have our Submit button and all of our form fields. The way that the request
05:12processing is going to happen is we simply use the request object which got
05:17passed to our controller and we can use the getMethod and say, if the get method
05:24equals POST--in other words, if the request information that's coming in has
05:28been posted to us, which would only happen if we run action page from being
05:32submitted--then we are going to bind to that request to the form.
05:36So it will then look for matches of the form field names and make sure that they
05:41correspond to what we set up in the form, in terms of validation requirements.
05:45There's a special method, is valid, that we will go through and look. And in this
05:49particular example, the only validation we've done is we've set the email equal to an email type.
05:55Once you've done this, any changes have been actually saved to that entity
06:00object. So if you call the flush method of the entity manager, this will then
06:05save the data to our database. And when we are done, you can finally do whatever
06:09you like. In this case, we are going to redirect to, and then we are generating
06:13URL task success, which we haven't set up.
06:15So this is the basic process for building forms. Symfony has this form builder
06:20and it's included for all of your applications. In this one, we built it
06:24directly into our controller, but more often when you get into building your own
06:28applications, you will probably create different classes that are instances of
06:32these forms. You can see using the Doctrine ORM tool that saving the information
06:37to the database is a snap.
06:38All you need to do is persist your entities and the changes will be saved to the
06:41database automatically.
Collapse this transcript
Taking it from here
00:00Symfony 2 is a fairly comprehensive framework. We've seen how Symfony uses a
00:04number of constructs and subtechnologies to manage different aspects, including
00:09YAML, Doctrine and Twig. For some, these are its strengths, shorthand methods to
00:13make development time faster. For others, they add the burden of learning
00:16technologies and additional overhead.
00:18With this version of Symfony, they have design flexibility into it, allowing you
00:22to use these aspects or not. In addition to the basics we've covered, there are
00:26number of things to learn in order to get started working with Symfony.
00:30There are a couple of places to look. The Symfony book can provide a good
00:34jumping point for learning some of the fundamentals, for getting in and learning
00:38more about routing and the controller and handling dynamic routing and Rest
00:42friendly URLs. You can use the templates to create helpers, and there's all the
00:47special tags. There's escaping and there's a number of things you can do for
00:51JavaScript integration. Symfony has a built-in security construct that you can
00:55use for authentication, and it can handle some client management as well.
00:59Just like with other aspects of Symfony, you can use different ways to implement
01:03these technologies. There are varieties of caching mechanisms you can use. These
01:08include HTTP caching and Edge Side includes, and these will be key to making
01:12sure your applications can perform well.
01:15Locales and translations can help with internationalization. Another thing to
01:19look into is the Service Container. The Service Container is a great way to
01:24manage the objects inside your application. It uses a concept called dependency
01:28injection, and it's one of the key selling points of Symfony 2. In addition to
01:33the book, there is the Cookbook and inside the Symfony Cookbook, there are a
01:37number of different examples, broken down into different categories, that can help
01:42you learn different aspects of working with the Symfony application.
01:45The Symfony framework has evolved in version 2 to become a powerful and more
01:49flexible development framework. In these movies, we have introduced the concepts
01:53that are important to understanding Symfony 2, including how to implement
01:56routing and controllers, creating views using templates, and managing data with Doctrine.
02:02There is much more to learn, and I definitely recommend visiting the Symfony Book
02:06and Cookbook at Symfony.com.
Collapse this transcript
6. Introducing CodeIgniter
Intro to CodeIgniter 2.1
00:00CodeIgniter is considered to be one of the lighter and more flexible frameworks
00:04of those currently available for PHP. It has a pretty decent following, good
00:09documentations, and a solid community to work with. PHP is backed by EllisLab.
00:16EllisLab manages a couple of other projects, including MojoMotor publishing
00:20engine, and the Express Engine content management system.
00:23They also offer hosting. CodeIgniter 2 requires the version PHP 5.1.6 or
00:30greater. It has a stricter license compared to the other frameworks we are
00:34looking at. It uses the Open Source License 3.0, which is a copy left
00:39distribution. Basically what it means, if you distribute any changes to the core
00:44files, not just the files that are in your application directory but the core
00:48ones that make it work, you are required to make the source available to the public.
00:52The community behind CodeIgniter is fairly strong, and the documentation is well
00:57written. The structure of CodeIgniter is quite lightweight. It's one of the more
01:03flexible architectures, in that you can easily configure it for hosting, and it
01:07doesn't take much to get up and running with it. There is not a lot of rules
01:11about how specifically you need to use their framework. It provides you a bunch
01:16of classes that you can extend in methods to use and helper classes, but how you
01:20use them is really up to you. It has a good construct for URL mapping and routing requests.
01:27There are number of core library classes included with CodeIgniter that do
01:31everything from helping you with files to CAPTCHA, to UI assistance, and more.
01:37There's a very rudimentary template engine built into CodeIgniter that you can
01:41use if you want to.
01:42Because of its flexibility, it's quite easy to extend CodeIgniter. There are a
01:47number of third-party applications that easily integrate with it. CodeIgniter is
01:51considered a lightweight MVC framework for PHP. It's currently at version 2.1
01:56and is managed by EllisLab. It includes a number of libraries that will give you
02:00a head start in many of the application features you might need. So let's dive
02:05in and see what CodeIgniter, or CI for short, has to offer.
Collapse this transcript
CodeIgniter concepts, advantages, and issues
00:00CodeIgniter is a lightweight glue framework, which means that you can use or not
00:05use different pieces from within it. It features a basic MVC implementation
00:09that's easy to learn and it's loosely structured. It doesn't however include
00:14many of the constructs that some of the other frameworks do to speed up your development time.
00:19In terms of installing, you can download the installation at codeigniter.com.
00:24It's less than a 2 megabyte file for the full framework. You can also use GitHub
00:29to download this if you prefer.
00:30The install includes a user guide that you can reference and you'll have
00:34installed locally. The mainindex.php file allows for custom routing changes in
00:39configuration. In the installation you'll also have a default welcome
00:43controller and view set up for you.
00:46CodeIgniter is object oriented, and the processing is mostly manual. That is to
00:52say, it doesn't have constructs where you complete config files and the
00:56framework does it for you. You're going to end up creating classes and calling
01:00methods and doing typical object-orient programming.
01:03It's a glue framework, meaning you can pick and choose which parts of the
01:06framework you wish to use. You can determine how the URLs are going to be routed
01:11and render it, but you can have dynamic URLs and routing can be very simple and intuitive.
01:16Views are typically done in PHP, or they include a simple template engine to
01:21make it easy for you to output variables and collections. The model layer of
01:25CodeIgniter uses a modified version of Active Record pattern and includes a
01:30special class that you can use for handling data.
01:33CI also includes a number of helpers. These aren't object-oriented, but they're
01:37procedural functions that you can use to do all kinds of tasks, like interacting
01:41with URLs, interacting with files, building forms, or writing cookies.
01:47CodeIgniter is fairly straightforward, which means it's pretty easy for novices
01:51and PHP developers to jump right in and learn it. However, there are not a lot
01:57of rapid application development tools, so you end up coding most things by hand.
02:01In order to do a number of things, it's going to be required that you use
02:05third-party add-ons. This includes authorization and access control lists. One
02:11of the criticisms of CodeIgniter is that it's too loosely structured, which
02:15means that one application can be totally different from another.
02:18However, CodeIgniter is universally agreed to be one of the best performers of
02:23all the PHP frameworks. CodeIgniter is a popular framework that offers a lot of
02:28flexibility, an MVC paradigm, and a significant community behind it.
02:32One of its key advantages is how simple it is to learn and adapt. However, it's
02:36also criticize for lacking some of the more powerful constructs, especially in
02:40terms of rapid application development, that other framework have, and its
02:43flexibility can translate into too loose a structure, so one CodeIgniter
02:47implementation can differ greatly from another.
Collapse this transcript
CodeIgniter classes
00:00CodeIgniter comes with a host of classes that can jumpstart your development and
00:04enable you to do a number of different programmatic tasks. The best way to find
00:08these is to look in the user guide at codeigniter.com.
00:12If you open the table of contents, you can see there are number of different
00:16classes and a number of different documents available for your help. The two
00:20columns on the right contain the key classes that will be usable by you, and they
00:24are broken into three major classes.
00:26Helpers, used to be called plug-ins, these are procedural. You use a special load
00:31helper method to load them, and then you can call on a number of functions.
00:35These can help you with CAPTCHA, They can help you build URLs and manipulate
00:39data, including dates, numbers, and strings. You can work with directories and
00:43files. You can send emails, write URLs. You can even output emoticons using this Smiley Helper.
00:50The driver class allows you to access different types of data, including
00:54database. Yu can work with your cache this way and you can also include
00:58different JavaScript classes using the JavaScript class.
01:01The library classes contain fully functional classes that you can pull in and
01:05use for some of the more heavy crunching within your application. This can include
01:10encryption, interacting with language elements for internationalization, working
01:15with browser detection using the user agent class, XML, zip files, handling
01:20track-backs, image manipulation, and there's even a shopping cart built-in.
01:25As you can see, in addition to the MVC pattern enabling, CodeIgnitor contains
01:30three types of classes that will help your application development: drivers to
01:34load databases and JavaScript libraries like jQuery, helper classes to perform
01:39simple helper tasks, and library classes that contain all kinds of supporting
01:43functionality. The documentation is also a good source for you and one of the
01:47benefits to using CodeIgniter.
Collapse this transcript
CodeIgniter flow and architecture
00:00In this movie we're going to take a deeper look at how CodeIgniter works. We're
00:04going to look at how the MVC architecture is implemented and how we can organize
00:08our code in the CI application before we actually start writing it.
00:12The first thing we want to look at is the directory structure. There are two
00:16main folders you'll see when you download and extract your CodeIgniter
00:19application. The system directory is going to contain all of the main CI
00:24libraries. That's going to include the core classes, the database drivers,
00:28helpers, the language libraries--all of those different class will be located inside of here.
00:34The core of our work is going to be stored in the application directory. We're
00:37going to store our models, views, and controllers. Each of those elements will be
00:41placed inside those directories.
00:43Inside the errors folder you can put any error templates. There's a helpers
00:48folder for your own helper classes. Language allows you to implement your own
00:52internationalization information. Libraries allow you to include your own custom
00:56PHP classes. These might be third-party libraries that you can download. And the
01:01config folder is where we're going to store a number of different core
01:04configuration files.
01:06The model is going to extend the CI model class. The this->db object is a
01:13pseudo query class that implements a modified Active Record pattern, so you can
01:18use this to get data from the database. For example, if I wanted to get
01:23information from the users table, my query might look something like this:
01:27this->db->get and I pass users which is the table, and then I can pass an array
01:33that's going to be some filters that will get applied to the database.
01:37Once you've extended the CI model class, you can create your own class and your
01:41own methods that will call this DB object and use it to output data and to save
01:46data to the database. The view layer is implemented using templates.
01:52Usually these templates are going to be broken up into different parts. You can
01:57do this however you like. Again CI is highly flexible, so if you want to
02:02implement different headers and footers and subelements, you can do that and
02:06pull them in your application where necessary.
02:09There is nothing fancy like inheritance or dynamic content variables that you're
02:13going to use to output in your templates. You can optionally use their included
02:18template engine that comes with CodeIgniter. The controller is responsible for
02:23handling all of the routing.
02:25The basic rule for routing is going to be, whatever the route of your application
02:30is, after that will be the controller; following that will be the method of the
02:34controller and you can pass arguments as many as you like in subsequent
02:39directory-like structures in the URL.
02:41If you want to change this mapping, you can do that in a fairly easy manner. One
02:47of the things that happen inside of the routing is it will essentially post-pend
02:51certain file endings on to some of the files you create. If you want to, you can
02:57change what that post-pending is.
02:58For example, instead of .html, you could refer to .php. The controller class
03:04will always extend the special CI controller that comes with the framework. If
03:09you want to load the model into the class, you simply use this load object and
03:14specify you want to load the model and what the name of your model is. You'll
03:18create your own method to handle the subprocesses: index to handle the homepage,
03:23edit to handle the edit, and however you like from there. There's a data property
03:28that will always get passed to the view,
03:30so anytime you add anything to the data array, it will be available from within
03:35your view template. The way you load views is using the view object and calling
03:40the load and passing the name of the file that you want to load. This is what I
03:44was referring to in the post-pending. In this example it's loading header. That's
03:48automatically going to assume that its header.html, but if you want to change
03:53with that post-pending is, you can do what you like.
03:55This is the basic format of how CodeIgniter routes requests. You can see the
04:00first thing that happens is it goes through the routing. If the item is cached,
04:04it would just send back the cached the view. Otherwise, it will check security
04:09and send it to the application controller. That controller can then interact with
04:13the different classes inside of the CodeIgniter library. It will then send that
04:17information to the view, and that'll end up getting returned to the user.
04:21CodeIgniter uses a fairly straightforward and object-oriented way to implement
04:26their MVC architecture. It all begins with the routing, which will send method
04:30and parameter information to the controller. Both the controller and the model
04:34will extend CI controller and CI model classes respectively.
04:38Finally, your view is written in simple PHP or using CI's template parser. In the
04:44end, you have your partition MVC application, so let's dive in and put this to work.
Collapse this transcript
7. Using CodeIgniter
Downloading and setting up CodeIgniter
00:00You will get to see one of the highlights of CodeIgniter in this unit. It's easy
00:04to get started. It's really just a matter of downloading and extracting it your web server.
00:08The first thing you will need to do is download the latest version at the
00:11homepage of codeigniter.com. If you go to the downloads page, you can find
00:16earlier versions, but it's usually a best idea to start with the latest stable release.
00:20If you use Git, you're also welcome to download it from GitHub. You can find it
00:25at github.com/EllisLab/CodeIgniter. Once you've downloaded the zip file, go
00:31ahead and extract it. I'm going to rename it to something simple, like just
00:35CodeIgniter. You can see inside this directory all you really have are the
00:40application, the system,
00:42the applications where your files are going to go, and the system is where their
00:45files go. The user guide allows you to have a local version of the user guide.
00:50Let's copy the CodeIgniter folder, and let's put it in the root of our MAMP
00:53server, so go ahead and go to mamp/htdocs and paste it in there.
01:00The only configuration you'll need to do--and you only need to do this if you
01:03want to--is available inside of the configuration file. The configuration file
01:09is located in application/config, and it's called config.php. The only thing you
01:15really need to define, if you want to, is the base URL, and this is only important
01:21if you're using a specific domain.
01:23Since we're using local host, it's not something we need to worry about, so we
01:27can go directly to our local instance, which is located at our mamp
01:31URL/CodeIgnitor. You can see it welcomes you to CodeIgnitor. This is actually
01:38being dynamically generated using the welcome controller that's installed by default.
01:42Additionally, you will have a link to the local user guide, should you want to go
01:47and read the documents while you're working. So that's pretty much it. You can
01:49see this is pretty easy and transfers well to hosting environments. The
01:53simplicity of setup is certainly one of strong point of CodeIgniter. The next
01:57step is to start building our CodeIgniter application.
Collapse this transcript
The model
00:00The first thing we're going do is create the model layer of our CodeIgniter
00:03application. We'll configure our database. Then we'll create a CI model class that
00:08can be used to interact with it.
00:09In order to configure your database, we're going to go into the application
00:13directory, into the config subdirectory, and open the database.php file. In here
00:18you can see a number of variables available for you to set that will determine
00:22how CodeIgniter can talk to your database.
00:25Since we're connecting to MySQL, which is the default setup anyways, we're just
00:30going to need to customize the configuration a little bit. We're using MAMP, so
00:33we want to make sure to point to the port 8889, and we'll use that by appending it
00:38after the colon to the localhost. There is no port object. We'll also set the
00:43username and the password.
00:46We created a database in the Zend framework model unit. We're going to use that
00:51same database here. If you haven't created it yet, you probably want to go to
00:55the db.sql file in the exercise files for this course and refer to that movie
01:00and how to create it.
01:01For the database, we can type in php_framework_test, which is the database we
01:07created. Once you save this file, you're now set up so you can talk to the
01:11database. The next step will be to create the actual model class. So we're going
01:15to right-click on the models directory and we're going to create a new file. This
01:20file will be called MailList_model. You can name it what you like, but this is intuitive.
01:26Once you've created this, all you really going to do inside of this file is
01:30create a generic PHP class. We're going to call the class the same thing that
01:34the file is named, and we're going to make sure that it extends the CI_model
01:39class. This will make all the constructs available to us so we can talk to our database.
01:44The main thing we're going to want to happen, no matter what method we call from
01:48inside this model class, is to load the database. So we're going to add a
01:52constructor. Inside of this constructor we're going to use a special load
01:56object. This is used throughout CodeIgniter to load different elements into our
01:59files. In this particular instance we're going to load the database, and this is
02:04going to use all the configuration options to set up the database into our
02:07CodeIgniter application.
02:09Now we can write whatever functions we want to, to do whatever task we want
02:14to have done. I'm going to create one that's going to get the data out of the
02:16database. I put a few dummy users into the database so that we can output and
02:21make sure we're talking to the database correctly.
02:22So we're going to call public function and we're going to call it get_maillist.
02:28And inside this function we're going to create a query object. We're going to
02:32set this query object equal to the result of loading the data from the mailing
02:37list, and we can use a special get format of the DB object that we have in this
02:41model class. So we can say this->db-> get, and then you pass your field name.
02:47The get method also supports an optional ray after that, which can take filters
02:51on specific fields in your database. But this is just going to pull everything
02:55straight out for us. And then we can return the result, the same query, and then
02:59return it as an array, so result_array.
03:03Now to test this, we're going to want to open it up inside of our controller. So
03:08if you notice, I'm also using the load object inside of my controller to load the
03:12view. In the index method, that's where our main application is outputting to the
03:17user, so inside of here is where I also want to load my model. So I can use the
03:22load class to load the model, and you simply specify which model you want to load,
03:28and I'm just going to use the model I created, MailList_model.
03:31This now loads it into the application, and you'll see, it becomes a variable. If
03:36I want to send data to the view, you can use the special data object and set any
03:41properties of it. In this case I'm going to create one called mail_list, and I'm
03:45going to set it equal to the result of calling the function I just created.
03:48So in order to access that class, I use this, and you'll see I now have a special
03:53object called MailList_model, the same name as my model class, and then I can
03:59call its method. Let's go ahead and just use the var dump function to output
04:06the data and see if it's working correctly.
04:08In order to run it, we're going to go to our browser and we're going to go to
04:12localhost:8080/CodeIgniter. This is the CodeIgniter directory that we installed.
04:19So you can see at the top here, it's output a bunch of information, and this is
04:23just the notation that var dump uses to show you what the data is.
04:26So it's an array with a length of three, and it has a number of different
04:30properties in each array, including the values from the database. You can see I
04:34have Joe user, I have Jennifer Smith, and I also have Drew Falkman.
04:39What will we really want of this model class in our application is to be able to
04:43insert data when the user wants to join our mailing list. So let's go back into
04:48our model class and let's create an insert function. So we'll declare another
04:52public function, but this one is going to take an argument, and we can just call it row.
04:57And that's essentially going to be the record that we're going to insert into
05:00the database. And we'll just specify this, and we can use this DB object again,
05:05and it has a method called insert. And in it you specify the table, which is
05:09mailing list, and the data you want to put into the mailing list. This object can
05:16either be an object or an array, but the properties or the array values need to
05:21match the fields in your database.
05:23Assuming that's done so, they will automatically get inserted to the database.
05:27What we're going to do for this function is we're going to return the ID of the
05:29object, in case we need that for any information. So we'll return this->db->insert_id.
05:39So that will return the ID of the last item that was entered into the database,
05:43which would've been the item we just inserted calling the insert function. You
05:47can see already in setting up the model how CI both provides a mechanism for you
05:51to do what you need, but allows for all kinds of flexibility in the
05:55implementation, based on your own coding preferences.
05:57In this unit we created a model class and a couple of methods to return data
06:01from our database and insert a record into our table. Next, we'll get our
06:05controller configured.
Collapse this transcript
The controller
00:00As with the other frameworks, the controller is the core of our application. It
00:04will determine based on the URL how to handle a request and how to generate a
00:08response using the views.
00:09In addition, it's going to be responsible for handling all of our processing, be
00:13that talking to a database, sending emails, uploading a file, or whatever else
00:17you want your application to do.
00:19In CodeIgniter, we will create our own controller class that extends the CI
00:22controller base class. CodeIgniter comes with a default controller, and you can
00:27see on the main Welcome page that will tell you where to find it. It points to
00:31both the view template that's being loaded by the controller and the controller itself.
00:36You can take a look at that controller if you would like. It's located in the
00:40welcome.php, inside of the controllers folder, in the application folder. You can
00:45see, inside of the CI_Controller, we have a function index, and this is the
00:49default function that's going to execute when you go to the main controller page.
00:54We are going to create our own controller. We do that by right-clicking on the
00:58controller file and creating join.php. We are simply going to declare a PHP
01:04class, and we are going to call this join, and we are going to make sure that it
01:08extends the CI_Controller class.
01:10Now there is a few key things that we need to do. The main thing we are going to
01:14need to do is create functions for each of the views that we want to display. I
01:17am going to create a function called index that will be the default display for this controller.
01:23Inside of the index I am simply going to output Hello from CI. In order to load
01:28this particular controller, all I need to do is go to the main CodeIgniter page,
01:33type in index.php, which is the default main front controller for all CodeIgniter
01:38applications that handles the routing, and then type in the padding. So after the
01:43index.php, it's going to look for the join.
01:45If you don't like the index.php and you don't want it to show up in your live
01:49web site, you can use the mod_rewrite in Apache to change how that gets
01:53displayed. So you can see by putting join, it automatically knows that it's
01:58going to look for the join controller, and since nothing is specified after join,
02:02it's going to load the index function.
02:04If you wanted to make the join controller the default controller so that it
02:08used this instead of the welcome controller, you can do this by going into config
02:13and looking in the routes.php folder.
02:16If you scroll down a little way, you will see that on line 41 there's a
02:21default_controller property that's set. It's set to welcome. If you change this
02:25to join, it will now load your controller instead of the default welcome
02:30controller. So if you go back to the web site and type in localhost CodeIgniter,
02:35you'll now get your hello from CI application.
02:38If you want to use variables or control other information that happens inside of
02:43your controller, there are a few things you can do. One is, if you want to make
02:47sure that variables are available to anything inside of your controller, you can
02:52create a constructor. And inside of this constructor, you can add whatever
02:57variables you want and instantiate them and then any of the methods that execute
03:01will have them available.
03:02One thing you are going to want to make sure to do if you do this is call on the
03:06constructor method of the parent class. What this will do is this will call the
03:11construct method of the CI_Controller class and make sure that everything that
03:15needs to be done to set up your controller is going to happen inside of this file.
03:20Following that, you can create and set whatever variables you want. If you want
03:24to create a dynamic routing variable, you create your route. So in this case I
03:29want to create one that will output a user's name.
03:32So I'm going to create a function. Whatever you name this function will be used
03:36to respond to the routing request. If I use the name "name" that's what I will
03:40use in my route in order to make sure this function gets called.
03:44So I am going to create whatever variables I want here and as many as you are,
03:49these will be appended as if they were folders on your routing, and I will show
03:52you how that all works in a second.
03:54In order to create variables for your routing, you simply put arguments into
03:59your function. Then each subsequent folder in your URL request will become that
04:04value. In this case I'm only going to use one variable, called str, where I'm going
04:09to pass a string that I can then just display directly.
04:12Once you've written your argument, you can then use it to output to your view.
04:16You could either write it to the data object that will be used in the view or
04:19you can echo it directly. I am simply going to output it directly to the screen.
04:24Now to run this in your application, you are going to want to load up Firefox.
04:29In order to load the application, you then go to your CodeIgniter folder, you'll
04:33load the main index.php, after which you can specify the route you want to use.
04:38I'm going to go to the join controller, so I specify join. After the controller,
04:44you specify the function you want to call, in this case name. Following the name
04:49of the function, you can put whatever parameters are taken by that function. In
04:54this case, I only take one, which is the string variable.
04:56So I'm going to go ahead and put my name, but you can put whatever you like. And
05:01you can see how whatever you put will get echoed back to you. So that's the
05:05essence of how CodeIgniter works.
05:06The controller in CodeIgniter is a class that's going to extend the
05:09CI_Controller class. Because of how default routing is set up in CI, there is a
05:13simple pattern that gets followed: whatever your base domain is, /index.php,
05:19then your controller name, then your method name, followed by any arguments.
05:23In all these examples we output simple strings, but you can use this to deliver
05:27a full interface using your views.
Collapse this transcript
The view
00:00Creating the view layer in CodeIgniter is a matter of creating templates. You
00:04can do this either in PHP or you can use CodeIgniter's template engine. Once
00:08you're done, you will load them into the controller in order to be displayed.
00:12For the view, we can create PHP inside of this application/views directory. What
00:17we are going to do is we are going to go into our original application, which is
00:22stored in the exercise files, in the START_HTML folder. We are going to open
00:27join.htm into our text editor, and now we can cut stuff out of here and use it
00:32for our header and footer inside of CodeIgniter.
00:34So I'm going to grab everything and including this div with the id of
00:38mainArticle, everything above it, and I am just going to cut it out of this file.
00:43I am going to create a new file in the views directory and I am going to call
00:48this header.php, and I can just paste this data right in here.
00:52I need to make sure to remove any unnecessary information. In this case, the
00:57SpryValidation and form stuff we can go ahead and remove, because we're not using that any longer.
01:02Additionally, I want to make sure that the style sheet always is referenced
01:06correctly, so I'm going to hard code in the CodeIgniter directory. So this will
01:10always look up to the root of our web server and go into CodeIgniter and then
01:14grab the main.css from the join_files directory.
01:17So let's make sure to copy that join_files directory into that place. So it's
01:22also in the START_HTML. We can copy it. We can go into our CodeIgniter
01:27directory, and then we can go into our MAMP root, and we can paste it directly in here.
01:32The next thing we'll need to do is configure our footer, so we can go back into
01:36the main file and look for the closing form tag. This div corresponds to that
01:41main content div, so we want to include that closing div tag and everything else
01:46in the remainder of the file. So you can cut it out, which will leave just the
01:50basics of what was actually making up the original file, the content itself
01:54without the design. And we go back into our footer, which is going to be a new
01:59file called footer.php. You can paste the results of that cut into this file, and
02:05we are going to remove this script block. Everything else should work correctly.
02:10The next thing we are going to need to do is load this into our controller. So
02:14let's go into the join controller and let's go ahead and edit the original index
02:19function. This is the default function that's going to run whenever you go to
02:23just CodeIgniter directory.
02:25The first thing we are going to do is use that load object to load our view, so
02:29we will specify that we are going to load the view by calling the view method of
02:32the load object. And header is going to refer the header PHP, and CodeIgniter
02:37knows to look in that views directory. We will also then specify that we want to
02:41load our footer after our content has been output.
02:44So now let's go back and load this into our application and see if our design is
02:48included. So because we have set this as the DefaultController, we simply need
02:52to go the CodeIgniter directory and it loads up our application. You can see
02:58Hello from CI doesn't necessarily get output in the right place, and we can talk
03:02about how we can do that.
03:03The next thing we'll need to do to make this more dynamic is to use variables.
03:08For example, if I want to change the title to output something different, I can
03:12create variables of this data object. So inside of my controller I have this
03:17object called data. I can create whatever variables I want--in this case, I will
03:21create title--and set them to whatever I wanted to say: Join our community.
03:28I can then use anything in this data object as if they were variables in the
03:32local scope of my view classes. So if I open my header.php file, I can change
03:38out the title and instead use PHP to output this title variable. Notice how I
03:45don't use the data object; I just refer to the name of the variable directly.
03:49Additionally, I have this h1. That's the page ID. Inside of here I can do the
03:54same thing and use that same variable.
03:56Before we are able to use this on the page, we need to make sure to pass it to
04:01the load method. So all you need to do is append it as an argument to this load
04:06function. This will then pass it into that view and make it usable as if it was local data.
04:11Now when we load our CodeIgniter, we can see it says Join our community, which is
04:16the title we entered, both at the title of our browser and in our h1 tag. If you
04:21want to use the built-in template library, instead of using the view, you are
04:26going to use a special function called parser.
04:28The parser object is made to use the template. So if I wanted to create, say, a
04:33different header that used CodeIgniter's template engine, I am going to go ahead
04:38and copy everything that's in the existing header and I am going to create a new
04:43view called theader.php and paste that information.
04:49Because it's not a PHP file anymore, if I want to output data, instead of using
04:54PHP, I simply use the curly braces. The template engine knows that these curly
05:00braces signify a dynamic area and will output whatever's held in that variable.
05:04Let's go back into our controller and let's use the template for a name route.
05:08So, the way we are going to do this is, once again, we are going to use our
05:12special data property, and we are going to set a variable called title. And we
05:16are going to set equal to whatever that string variable is. So whatever the user
05:20passes as the argument is going to end up being that title.
05:24We are then going to need to load in the parser class, and the way we do that is
05:29we call this load, and we use this special library function and specify which of
05:34the library classed that we wish to load, in this case the parser.
05:38This will load it into our application, and now we can refer to this parser. And
05:43the parser class has a special method called parse. that will pass whatever file
05:48you enter here, in this case theader, and note you don't need to use the file
05:53ending. And it will pass any variable that has data that you want to output into
05:58that template. So it works very similar to how we load the view. The difference
06:02is we are calling on the parsing engine to do it.
06:05Additionally, we're going to go ahead and parse the footer. Since there wasn't
06:08anything dynamic--there was no PHP in the footer--we can parse that as well. We
06:13are also going to go ahead and pass the data variable to the footer, even though
06:16we are not using it.
06:19In order to load this value in our browser, we are going to have to type in the
06:23appropriate root, so we are going to go to index.php, the name of our controller,
06:28the name of the function we want to call in the controller, and then any
06:31arguments, in this case one string. And I'll go ahead and put my name in there.
06:35So you can see my name got passed. It got used as the title so it was output in
06:40the title, and it was also output in this h1. You can see it up here in the
06:44header, and basically the reason is because when you have anything that's being
06:48echoed also in this function, it's simply going to place it in the body tag somewhere.
06:52So, all the view layer elements are placed in this views folder. You can use
06:57subdirectories if you'd like. You just change the parser view call to add the
07:01folder. For example, if I created a subdirectory join for all these files, I
07:06would've just prefixed it with join/theader.
07:09You can use the basic template engine that's included with CodeIgniter. It also
07:13adds simple looping abilities, or you can code directly in PHP. The choice is yours.
Collapse this transcript
Forms and making it all work
00:00We only have a couple of more steps to get our subscription application
00:03finalized. We need to build the form and we need to insert that information into
00:07the database when the user submits it.
00:09To do this, we are going to dive into the CodeIgniter helpers, specifically the
00:13Form Helper. We'll also look at how to handle requests in the controller and
00:16call on our data model to do our insert.
00:18First, let's create a new view template that we can use to build our form. I am
00:23going to go into the views directory and I am actually going to create a
00:27subdirectory. I find this is a good way to organize my view files.
00:32I name the directory the same as the controller, join, and then I can create my
00:36files in this directory. So let's go ahead and create a new file, and we can call this form.php.
00:43This is where I am going to create my form, and in order to create it, I'm going
00:47to make sure that I have that form helper loaded. The way that that's going to
00:52work is inside of the controller, join.php in the controllers directory. I am
00:57going to call a special method, this-> load. I'm going to use the same load
01:01object that I used to load views and libraries, and this time I am going to load my Form Helper.
01:07So I am going to use a special method called helper, and then you pass which
01:09helper you want to load, in this case form. I am also going to add a reference
01:16to my form itself, so I'm going to make another load call. This time I am going
01:20to call the view and I am going to specify that I want to load join/form.
01:25So when it loads this view into the controller, it will have all of this form
01:30data available to it, all of these helper functions. Let's go back into our form
01:34template and create our form. I've already pre-written all of this code, so
01:40let's go ahead and paste it in and discuss what I've done.
01:43The first thing I do at the top of my form template is I output any validation
01:47errors that might exist. This way when the user submits, if anything is incorrect
01:52in the validation, it will output the errors in this placeholder. On the first
01:56time this form loads, if there aren't any validation errors, the result of that
02:00function will be nothing, so nothing will get output.
02:02The next thing I'm doing on line 3 is I am calling the form_open method. This is
02:07going to open the form. It'll essentially create the open form tag. The variable
02:12that I pass to it, join/save, is going to be the action, and it uses the same
02:17routing mechanism that we use throughout CodeIgniter.
02:20So it's going to go to the join controller and it's going to call the save
02:23method, so that's going to be something we'll want to make sure to create inside
02:26of our application. Then I'm going to go ahead and create all of the different
02:29form elements that I want to use.
02:32What you do is you create the special arrays and you set values. These values
02:37will be used to generate all of the different inputs. The first one I create on
02:41line 7 is a variable called firstname, and I'm setting the name property to
02:46first_name and the id to the same. Notice that these match the name of the field in the database.
02:52I can then call a number of different form functions, depending on what type of
02:57control I want to create in my form. In this instance I want to create a simple
03:01text input, so I am just passing this variable into the form input function.
03:05That will then use the items of that array to create that input.
03:09Following that, I create a label that's going to match up with that input. The
03:14first argument of the form_label function is the text that you want to see in
03:18the label. The second argument is going to be the id of the field that you want
03:22it to relate to. You can then output any additional HTML depending on how your
03:26layout goes. In this case I am going to put a simple line break in between each
03:30of the form controls.
03:32All of these form controls work in basically the same manner. You can see when I
03:35get down to line 43, I can add other options, in this case size, which will
03:40control the size of the text input. If you want to create other types of form
03:44elements, such as check boxes or radio buttons, you can see what I've done in
03:49line 89. Each radio button has to individually be created. Notice that all of
03:55the names are the same; however, the values differ.
03:58You then create each individual radio button by calling the form_radio function
04:03and passing the values in for that specific radio button. Following each of the
04:08radio buttons, I am also outputting a value that I want to put next to the radio
04:12button. You could do this in another way, but I find it's simple just to use plain text.
04:17I also create my check boxes. Here I'm using this interests array, which is
04:23storing all of the data that can possibly be entered in as interests. Then I
04:27call the form_multiselect and I pass into it interests and also this object interests.
04:34Finally, I add a submit button by calling the form_submit function. You pass the
04:38id and then you can pass whatever you want to be displayed on the button. The
04:42last thing you want to do is make sure to call the form_close method. This will
04:45put the form closing tag into your form.
04:48So now that my form is all ready, I can go ahead and make sure that it's loading
04:52appropriately. So I am going to go back to my browser, I'll open a new window,
04:57and I'll go to localhost:8888/ CodeIgniter, which should load my default index
05:02method of my default controller.
05:05You can see that what I've done here is I've created this form and it's added
05:09all these text inputs based on the information that I entered. I have my radio
05:13buttons and I also have my form multi- select. I have not yet implemented the action.
05:18So if you right-click and look at the source of our form, you'll see that it's
05:23submitting to join/save. I haven't yet created that function, so I don't want to
05:27submit it, because I'll get a 404 error.
05:29Let's go ahead and go back into the controller, and let's create a new save
05:34function. I've already created all of the functionality that I'm going to need
05:40for this to work, so let's go ahead and open up the save function I've already
05:44created, and I am going to grab the code inside of it, copy it, and I'm going to
05:50paste it into my controller.
05:52Let's go through what I've done here. The first thing you can see that I've done
05:56is I've loaded my form_validation class. This is going to allow me to do any
06:00validation inside of my form. I've set the title, which is what I do in all of
06:05the different actions. You can see I've also done it up here in the name, and
06:08it's done in the index as well. That's just part of my view layer.
06:12I use the form_validation object that I created through the library load in
06:17order to set any validation rules I want on my form. You can see in this
06:21instance I am saying that the Email field needs to be trimmed. It's required and
06:26it must be a valid_email address. You can see, each of these options are
06:30separated by a pipe. If I didn't want to trim it, for example, I could remove
06:34that option and it would work just fine.
06:36Following that, I then call the run method of the validation object. This is
06:41going to return TRUE or it's going to return FALSE. If it returns false, then
06:46that means the validation is failed. So in this instance if it returns FALSE, I
06:51am going to go ahead and load the view, my form once again, and my footer again
06:56to make sure that it pulls in the form and it rebuilds it so that the user can
07:00correct their errors.
07:01If it does pass, however, I'm going to go ahead and load the url helper, which
07:06I'm going to use to get my postdata. I then create a postdata object that's an
07:11array with field names that match the field names in my database, and set it to
07:15all of the post values.
07:16You can see that I've called my MailList_model. I want to make sure that I've
07:21included my MailList inside of the controller, so I am going to go ahead and add
07:26a load inside of the constructor, so I'll call this load and then use my load
07:31object to load the model. And the name of my model was MailList_model.
07:36Because I load this in the constructor, I'll now have my model available in any
07:41function in which I'd like to use it. You can see on line 61 I'm using it by
07:46calling the insert method.
07:48We created this in the model unit, and this is going to insert all of these
07:52values into the database. Once I'm done, I go ahead and load the view header,
07:57the view footer, and the special view template join/saved, which I'll want to make sure to create.
08:02So let's go ahead and right-click on the join folder and create saved.php. This
08:09is going to be a simple output that's going to just alert the user that their
08:13information has been saved to the database, "Thank you for subscribing."
08:17So let's make sure to save our controller, and let's go back to our application,
08:24and let's try entering some data. Let's enter an incorrect email address and see
08:29what happens. We go ahead and Join, notice that it says "The Email must contain a
08:35valid email address." So it gives me my validation errors.
08:39One of the things about CodeIgniter is that it will not by default pre-populate
08:43all of the information that you already entered into the form. So if you want to
08:46do that, you want to make sure to set default values equal to those post values.
08:50So let's go back to our original form and let's change this to a valid email
08:56address. When I submit it now, it goes ahead and calls that method and saves the
09:00data into our database and pulls in our saved template to tell us "Thank you for subscribing."
09:06You can see the CodeIgniter has a number of tools to help you build your forms,
09:10handle validation, and handle the form submission. This is really just the
09:14beginning. The form tools allow for more field types; the validation model
09:18allows for a lot more control; and you can always use jQuery and JavaScript for
09:22this processing as well.
Collapse this transcript
Taking it from here
00:00We've seen so far how to plug a basic application into CodeIgniter and use it
00:04to help organize our code and hopefully shorten our development time. One of the
00:09advantages of CodeIgniter being an active open source project is that it comes
00:12with a lot of stuff out of the box: caching, profiling, and a slew of libraries
00:17to jumpstart your application development.
00:19One of the key advantages of CodeIgniter is a large community. In addition to
00:22having fairly decent docs, there's also a wiki page where you can find a lot of
00:27information including FAQs and a number of tutorials, and a decent forum that's
00:32quite active. You can see the last post was about two minutes ago.
00:36I briefly covered routing and URL mapping inside of CodeIgniter, but this is a
00:40key aspect to using CI, and it can be great when you have an ecommerce or a
00:44drilldown type of web site.
00:47I always recommend, when you're getting to know a new environment, to go through
00:50the tutorials in the getting started. In CodeIgniter they have a number of
00:54examples inside of this tutorial in how to create a news application, and it
00:59really shows you how everything works inside of the environment.
01:03Additionally, all of this information will help you determine what classes are
01:06available and how you can do things like manipulate images, use jQuery library,
01:11create captchas, and even create a simple shopping cart application.
01:15If you don't see a feature you need, there is actually uservoice web site for
01:19CodeIgniter. This allows you to submit ideas that you think you'd like to see in
01:23the next version of CodeIgniter.
01:25We've now explored all the basics of the CodeIgniter framework. Its advantages
01:29are that it's easy to get started. It's flexible. However, those same traits can
01:34also make it so that no two CI applications are identical.
01:37CI has a very large and active community. If you're interested in learning more,
01:41the codeigniter.com web site will offer links to the forums, tutorials, the wiki,
01:46and other documentation.
Collapse this transcript
8. Introducing CakePHP
Intro to CakePHP 2.0
00:00CakePHP is another of the full-stack frameworks. Its aims are fast adoption and
00:05development speed, specifically writing as much functionality as possible with
00:09as little code as possible.
00:11Developers who use it claim this to be true and swear they've been saved from
00:15repetitive stress disorder because of it. CakePHP is often criticized for lack
00:19of flexibility beyond the basics, performance, and the usual criticism of
00:23full-stack architectures.
00:25Cake was created in 2006, and it's backed by a non-profit, Cake Foundation. It was
00:31created by a Polish developer who wanted to emulate Ruby on Rails. A key goal is
00:37not to reinvent the wheel every time, which is also shared by Ruby on Rails.
00:41Cake requires PHP 5.2.6. It has a lot of features that I call automagical, which
00:48means if you name things correctly it'll end up building a bunch of code sets
00:53behind it that you don't need to actually write.
00:56Scaffolding and the creating of Create, Read, Update, Delete methodologies is
01:01quite automated. There is a command-line utility called BAKE that you can use to
01:05generate code. There are a number of components and features built-in the CakePHP
01:10that you can use for doing all kinds of tasks.
01:13Cake has recently released 2.0, and with it they've re-architected a lot of their
01:18structure. One of the key re- architectures is the Request and Response
01:23mechanism. Exception handling and error handling has been improved. They've
01:28added a lot more native features, including json_encode, PHP Data Objects, and
01:33SPL. They've improved the internationalization features.
01:37One of the criticisms of CakePHP previously was its performance, and they've done
01:42a lot in this version to improve performance, including adding lazy loading and
01:46intelligent caching. They added a new authorization system for handling
01:51authentication of users.
01:53And finally, they've dropped support for PHP 4, which means there's not a lot of
01:57extra code inside of the code base to check for all these different things,
02:02whether they exist or not. CakePHP is a full-stack framework that aims to
02:06shorten developer time while maintaining flexibility.
02:09With the release of 2.0 in 2011, we're seeing better performance and a slew of
02:14new and improved features. Let's dive in and take a look at CakePHP and how it works.
Collapse this transcript
CakePHP concepts, advantages, and issues
00:00CakePHP is an MVC framework that was originally built to bring some of the
00:04simplicity and time-saving aspects of Ruby on Rails to PHP. It does still share
00:09some similarities, but it's certainly evolved into its own type of framework,
00:13one that has specific conventions and also time-saving benefits.
00:17In order to install CakePHP, you're going to first download it either from the
00:22CakePHP web site or you can use git if you prefer. If you want, you can add a
00:27configuration to set up your URL mapping.
00:30One of the nice features of CakePHP is that it has an installer page to help you
00:35get everything configured and connect to your database. In terms of organizing
00:39your code, CakePHP like the other frameworks we've looked at, is object oriented.
00:44You can set up your routing and controllers quite easily. When you create your
00:49model class, there's really not much you need to write, and it will work with the
00:53database almost instantaneously. One of the aspects of CakePHP is that how you
00:58name your classes and items inside of your application will determine how they're used.
01:03There are a lot of things that happen behind the scenes with CakePHP. This makes
01:08it easy because there's a lot less code to write. However, it can be difficult
01:12when you get into to do more customization and deeper functionality.
01:16You can use helpers throughout CakePHP. For example, we'll look at the forms
01:20helper. There are a lot of rapid application development tools, including
01:25scaffolding to build CRUD applications quickly. If you prefer doing things from
01:30the command line, you can use BAKE, which will shorten development time of writing different classes.
01:36When looking at Cake, one of the key advantages is that it's quick to learn. You
01:40can be up and running writing Cake applications fairly quickly. However, until
01:45you've learned that, there are little mistakes you can make. For example, if you
01:49misname a class it might not map up correctly to your database.
01:53Using a full-stack framework has advantages, but it also has costs. One of the
01:58advantages is that everything is going to be structured in a very specific way.
02:03Additionally, you know where everything is going to be, and there are a lot of
02:07features that can happen behind the scenes. Some of the issues include
02:11performance and the ability to customize when you get into deep functionality.
02:16People that use Cake tend to really like the scaffolding. Scaffolding is the
02:20ability to create forms that will then submit to add and update items in your
02:25database. There is not a lot of information in terms of how CakePHP 2 performs.
02:31The Cake developers say that it performs a lot better than previous versions,
02:35but it's always tricky when looking at benchmarks online.
02:38One of the things to remember is that a full-stack framework like CakePHP is
02:42probably never going to be able to compete head to head with a lightweight
02:46framework like CodeIgniter. There are however tools for caching and lazy loading
02:51that should help for performance in your applications.
02:54Overall CakePHP is a full-stack framework that allows developers to take
02:58advantage of its internal constructs to save on coding time. Some argue, and to a
03:04certain degree benchmarks tend to agree, that this overhead reduces performance
03:08and learning a new set of conventions can take a little time. However, a fairly
03:12large development community is there to help.
Collapse this transcript
CakePHP components
00:00CakePHP divides up their class library by the ways in which the classes are used.
00:05There are a few different groupings of classes. Let's take a look at some of
00:08what's available to you out of the box in the CakePHP core library.
00:12If you go to the CakePHP book, you can see these different libraries are broken
00:16up by category on the left navigation. The first category is general-purpose
00:22classes. These are libraries that are available and reused throughout CakePHP.
00:27It includes collections, some global constants, and functions like
00:31internationalization features, timing, and object loading.
00:35Components are for use inside of your controllers. This includes authentication
00:40and Access Control Lists, cookie session management, emails, the request
00:45handling pagination, and other security constructs. There are a number of
00:50helper classes available to you inside CakePHP. These can be used in your views
00:55and for creating views. You can use these helper classes to do caching, create
00:59forms, write HTML, import JavaScript, parse numbers and text in time, work with
01:06pagination, work with feeds, and also manage session information.
01:11Behaviors are library classes that you can use in your model. There are ways
01:15that you can control your find methodologies using the containable class. You
01:20can use hierarchical data if you want, using the Tree. This also includes classes
01:24for translation and for managing Access Control List behaviors.
01:29And finally, there are the core libraries. These are simple utilities that can be
01:33used throughout your application, including the application library itself, the
01:38caching libraries, cleaning data, handling emails, working with files, using
01:44HTPP socket. There are number of different classes for handling how you name
01:49things, specifically the inflector class, using strings and handling XML.
01:55The internationalization and localization classes are located in here. You also
01:59have logging the router and security classes available to you. And finally,
02:04because of the active CakePHP community, there are a number of third-party
02:08plug-ins available as well.
02:10CakePHP 2.0 includes a number of classes that can be used throughout your
02:14CakePHP applications. These can perform all kinds of tasks, from string parsing
02:19to emailing and authorizing users.
02:21We touched on the basics here, broken down by the different types of your
02:25application. Additionally, you can look to the CakePHP developer base to find
02:30third-party plug-ins that might be helpful to you.
Collapse this transcript
CakePHP flow and architecture
00:00Let's take a look from a 10,000 view in how the CakePHP framework is laid out.
00:05This will help us hit the ground running when we actually get into writing our application.
00:10The first thing you'll notice when you download and install Cake is the
00:13directory structure. Here's how it's basically laid out. Everything in your
00:17application is going to be located in the app directory. This includes
00:21configuration information, The console will contain Bake, but there are some
00:25other console classes available to you as well.
00:28The controller will house your controllers. The view will hold all of your view
00:32templates. Model is where you're going to place all of your data models, and
00:36finally, the web root is where you're going to place any of the assets that you
00:40need inside of your application.
00:42Additionally, you're going to have the library directory, or lib. This is going
00:46to contain the core Cake libraries. The vendors folder is where you're going to
00:51put any third-party tools, if you want to use different template engines or
00:54something like that. The plug-ins is where you can add any CakePHP plug-ins you have.
01:00When you're creating the data model you're simply going to create a file in the
01:05/app/model directory. This class is going to extend the AppModel class. Whatever
01:12you name the class will coordinate how it connects to the database. Specifically
01:17it will determine what the database table is that it connects to, and we'll look
01:21more into this in the movie about the data model.
01:24That's pretty much all you need to do in Cake to create a class. This will
01:28handle both the entity management as well as all the add, update, and delete
01:33information to the database. Of course, if you want to add additional behavior
01:38or add additional functionality, you are welcome to do so, inside of that class.
01:43The View layer is typically written in templates that have a .ctp ending inside
01:49your views folder. If you want, you can use a template engine like Twig or
01:53Smarty, but typically they are written straight in PHP.
01:57There are four different types of view templates you can create. You can create
02:02layouts, and layouts are the skins or wrappers that you wrap around your
02:06application, or parts thereof.
02:09Views work with the controller, and these will be specific actions inside of your
02:14application. For example, one view that we're going to create is the form, and
02:19another view you might create is the Thank You page.
02:22Elements are little subapplications that will show up within your application.
02:27They can be as simple as displaying links, or they can be more complicated, like a forum.
02:32Helpers are functional little bits that can do all kinds of things to help build
02:37forms, add JavaScript functionality, or work with feeds.
02:42The controller handles the core routing and how everything works inside of the
02:46CakePHP application. Every application is going to have an AppController main
02:51class. You can then create your own controllers and methods to customize things how you like.
02:57If you're working with the Create, Read, Update, Delete methodology, you're
03:01going to want to make sure that the name of your controller correlates to the
03:05name of your database table, which will also map to the model name. If you want,
03:09inside of your controller you can add additional models than the one that
03:13matches your controller name, using the uses variable. Templates are going to
03:18load automatically. Again this happens because of the naming conventions.
03:23The controller defaults to have HTML form and session helpers. You can include
03:28different ones if you like or if you don't want to use some of these, you can
03:32specify which ones you want to use. You can generate all of these MVC elements
03:38using the command line if you prefer.
03:40Here's how the Cake environment looks from a diagrammatic perspective. You can
03:45see the client is going to make a request--it's down at the bottom-left by
03:49number one--which is going to be handled by the dispatcher. It will look at the
03:54URL to determine how to route the functionality and what's going on from the
03:58user request. This will then go to the controller, which may or may not use
04:02components or talk to the models.
04:04In the end, it's going to generate a view that's going to include the main
04:09layout, the specific view, and possibly other elements. Again, this can use
04:13helper classes to do it. And finally, it will deliver the response to the client.
04:18CakePHP is a fairly comprehensive MVC framework, and it allows developers to skip
04:23a lot of steps by using naming conventions to connect models to database, models
04:28to controllers, views to controllers, and the like.
04:32Once you get used to the CakePHP environment, this can make getting an
04:35application up and running, well, I hate to say it, but a piece of cake. The next
04:40step is to dive in and start creating some code.
Collapse this transcript
9. Using CakePHP
Downloading and setting up CakePHP
00:00In this movie, we will go over how to download and install the CakePHP framework
00:04in order for you to start development. You can download the latest build at
00:08cakephp.org. The most recent stable build currently is 2.0.5, though they have a
00:142.1 that's in alpha.
00:15If you want to download an earlier version, in the Downloads menu it will give
00:20you options to do that. I downloaded CakePHP as a zip file, and I'm going to
00:26extract it. I am going to rename the directory to just CakePHP, and let's copy
00:33it and place it in our MAMP root directory. So that's Applications/MAMP/htdocs.
00:42If you open up this directory, you can see the folder structure. The app is where
00:48everything that belongs to our application is going to be stored. The library
00:52directory has all of the Cake files inside of it. And additionally, we have
00:57plugins, which is empty, and a vendors, which is also empty. But you could add
01:02third-party vendor tools in here and any other plug-ins used in the plugins directory.
01:07Once you have downloaded CakePHP and installed it in your web root, you can open
01:11it up in your browser. So go ahead and go to localhost:8888 if you're using
01:18MAMP, and then CakePHP, or whatever else you might have named that folder.
01:23You can see, this will open a special page that will tell you crucial information,
01:27in terms of setting up Cake. You can see here, there are a couple of security
01:32warnings I am getting, and you will want to make sure to change these in a
01:36production environment. Though for our purposes in a development environment, it
01:39doesn't really matter. It will tell you that you have the correct version of PHP
01:42or not, and if you can write to the tmp directory, which is necessary in order to use Cake.
01:48Additionally, it gives you information about the cache, and you can see here it
01:52tells us that we do not have a database configuration file. Don't worry about
01:56that for now. We will set that up when we get to creating the model.
02:00Like other framework it uses a main index.php to handle the application. If you
02:04don't want that index.php to appear in all of your routing, you can use the mod
02:09rewrite in Apache, or whatever you need to do in your other web server to make
02:13sure this isn't included.
02:15So that's the basics. CakePHP is now set up, and we are ready to build our
02:19application. So let's get to it.
Collapse this transcript
The model
00:00The first thing we're going to do on our CakePHP application is to set up the
00:03model. We're going to need to make sure that our database is configured
00:06correctly, but once we've done that, it's remarkably easy. We're going to use the
00:11same database that we used in some of the other frameworks. If you haven't
00:15already, you're going to want to run the query that's in the db.sql document,
00:19inside the exercise files folder. You can refer to Zend model movie to see how this is done.
00:25Once you have your database set up, you're going to need to configure the
00:29database. You'll notice that there's a file in there called
00:31database.php.default. Let's go ahead and rename that. We're going to call it
00:38just database.php. The next thing you'll need to do is find the correct place
00:45inside of the document where you can set up the default database for your
00:49application. You can see on line 60 we're declaring this class Database_Config,
00:55and on line 62 we have the default database. So let's go ahead and customize
01:00this to our specific database.
01:02We're going to use a MySQL database, so that's the correct driver. We're also
01:07going to refer to the host, which is localhost. In the case of MAMP we need to
01:11make sure to add an item for port. And we're going to set this to 8889. We're
01:18going to need to set our login to root, and our password is also root. But you'll
01:23want to change this to whatever the login and password are for your database.
01:27Finally, we're going to change the database name to php_frameworks_test, which is
01:34the database we're going to use. Everything else can remain the same.
01:38Once you're done configuring this, make sure that you save it. Ensure that
01:43you've named it as database.php. If you don't rename it and leave it to
01:47database.php.default, it won't be detected by the CakePHP engine.
01:53So let's open our browser and let's go ahead and reload the main page at
01:57localhost CakePHP. If you scroll down, you should see something telling you that
02:03your configuration file is present, which means there's a database.php class,
02:08which we did, and whether or not Cake is able to connect to the database. If
02:13Cake isn't able to connect to the database, you want to log in and make sure that
02:17you've entered everything correctly and make sure that your MAMP server is running.
02:21Once you've connected to the database, creating the model is fairly
02:25straightforward. You're going to go ahead and open the model directory and
02:29create a new model class. So you can right-click on the Model folder and go to
02:34New File. We're going to name that file MailingList.php.
02:40Notice that this corresponds to Mailing_ List, which is the name of our database
02:44table. We then write our class directly in PHP, so we'll write a PHP
02:50declaration, and we'll use the class keyword to declare a class called MailingList.
02:55You want to make sure that this class extends the app model class, which is the
03:01class inside of Cake which will make sure that everything is set up correctly.
03:06The way that you link it to your database is you're going to give the model a
03:10name and it's going to match the name of your class, MailingList. Additionally,
03:18you're going to set the table that you want to use by setting a property called
03:22useTable. This will be referring to the table that this model is going to be mapped to.
03:29The reason why we're adding this useTable is because by default what CakePHP is
03:35going to look for is a pluralization of your mode name. So if you create a
03:40model, for example, called User, it will look for the users table in your
03:44database. In our instance it would actually look for a table in our database
03:49named MailingLists, and it will be okay to have the underscore in it, but it
03:54will be pluralized. So by specifying the useTable property, it makes sure that it
03:58gets the correct table.
03:59So that's it. There you have it. We've now created the model. And we can
04:03interact with this and it will persist our data inside of Cake, and it will also
04:07save it to the database.
Collapse this transcript
The controller
00:00The controller is going to handle requests and responses. It'll collate the view
00:04and coordinate the model, among other things. As with the other frameworks, the
00:09controller is central to the application. Let's take a look at how we can set up
00:12a controller in CakePHP.
00:14In CakePHP it's recommended to create individual controllers around each model.
00:19So in our application we're going to create a controller around the mailing list
00:23model. As such, we're going to create a file called MailingListController, and
00:28it's going to be inside of the controller directory.
00:31So we'll call it MailingListsController, plural. This is going to be a PHP class, so we can add a
00:43class declaration, and we're going to call it MailingListsController.
00:50Make sure that it extends the AppController class. It might be tempting to name
00:59your mailing list controller something else so that you can have a different
01:02URL than mailing lists inside of the routing; however, don't do that. It's not
01:07recommended. If you want to change later how the routing is determined, you can
01:11update this in the routes.php config file.
01:15The next thing we're going to want to do is declare the name of our controller.
01:19We're going to declare a public variable called name and set it equal to
01:24MailingLists. Notice that it's the pluralization of our data model. We're also
01:30going to load our helper classes. By default, if you don't load these, it's going
01:35to include an HTML helper, a form helper, and also a session helper.
01:40If you don't want to include all these, or if you want to include additional, you
01:44can go ahead and define them as the helpers object. So here we're going to
01:48create a new public variable called helpers and set it equal to an array, and
01:54this is going to contain the name of the different helpers, in our case HTML and form.
02:00Since we're not doing anything with the session, there's no need to include the
02:04session helper. If you wanted to add additional models, you can use the uses
02:09property and it would be an array, and you could add additional models that you'd
02:13want to include, just like the helpers class.
02:16By default, because we've name to this as the pluralization of our model, it will
02:21include our model already. The final thing we need to do is to create a new
02:26function to handle the index operation. So we go ahead and declare a public
02:33function Index. This is going to be essentially the default controller for the
02:38main page. So let's go ahead and look what this looks like if we execute it.
02:42So go ahead and go to the URL localhost CakePHP and add on the name of our
02:49controller, without the word controller. So we're going to locate
02:52PHP/MailingLists. You can see that it gives you the error that there's a missing
02:57view. We have to use templates in CakePHP. It's looking for an index.ctp file,
03:04and you can see exactly where it's looking for it. It's looking in the view
03:07directory, in the mailing list subdirectory. So we'll need to create that in
03:12order for this to work.
03:13If you want to pass variables into your view, you can simply use the set method.
03:20Inside of that, you can pass specific variables. In this case, I'm in the past
03:24title_for_layout, and this will include a title, Join our community. This will
03:33pass it into the view automatically.
03:35Since we didn't like the routing having the mailing list at the end, we could
03:39set this up to be the default mapping. In order to change the routing, we go
03:43into the config directory and if you scroll down, you'll see that you get a list
03:48of router objects. By default, there are a couple that are set up. There's one
03:53for the root directory that's going to the page's controller and opening the display action.
03:58In our case we want to change this and instead go to the MailingList Controller.
04:03So to change it, we're going to go ahead and change the controller from pages to
04:08MailingLists, and the action we can change simply to index. We no longer need the
04:16second action. Note that we still have our pages router, which is used for
04:21rendering static pages.
04:23Now when you go to load your application, you can simply load it at the main URL
04:28and it loads the same MailingList controller. So that's the basics of creating a
04:33controller. In more complex applications, you'll add more functions to represent
04:37different actions. There are other methods available for handling requests,
04:42performing redirects, and a number of other tasks.
04:45Keep in mind that for each action, it's automatically going to look for a
04:48corresponding view template to render, so create these or use the render method
04:53to point to a different view template, which brings us to the next thing I'm
04:56going to show you: how to create the View template.
Collapse this transcript
The view and the FormHelper
00:00CakePHP requires that we utilize templates to render our view. In order to get
00:05our site working, we need to use layouts to create the skin, or the main template
00:09that's going use to wrap the design of each action, and we'll need to create the
00:13templates for each of the actions themselves.
00:16In this movie, we'll create a layout for application, we'll create an action
00:19view, and we'll use the form helper to create our form. The first thing we're
00:23going to want to do is implement the layout.
00:26The layout comes from a special file inside of view/layouts, and it's going to be
00:33called default.ctp. Once we create that, we're going to go into our original
00:40source and we're going to use that code to be our wrapper code. So let's go
00:45ahead and open, in our exercise files, in the _START_HTML directory, join.htm.
00:59So let's go ahead and remove the form and everything essentially that's in this
01:04main div content area. This div is where we want to house whatever our action
01:11code is. So those views will always show up inside of this div tag.
01:16Once you've done that, go ahead and select everything, and you can copy it, and
01:24we'll paste it into our default CTP file. Additionally, we want to remove
01:30references to the spry library, which is handling forms in a non-CakePHP way, and
01:37we're going to update all of that. There is a block at the end of the file that
01:40you can remove, a script block. And you can also remove references to the two
01:45style sheets that have the word spry validation in it, and the two JavaScripts.
01:50Additionally, we want to make sure that the join files refer to the correct join
01:55files. The easiest way is to refer to our CakePHP directory, and we can place our
02:01join files directly in there. So let's go ahead and go into our file system and
02:08grab the join files directory.
02:11We can copy it to our clipboard and go into the MAMP root. Once you're here, go in-
02:18to CakePHP and instead of putting it directly here, you're actually going to put
02:23it in app/webroot. This is where all of your assets are going to reside that end
02:28up in your CakePHP application.
02:30So when I refer to it as /CakePHP/join_files, CakePHP knows to look in that
02:37directory to find anything. The next thing we're going to need to do is create a
02:41main view index. If you remember in the controller unit, our controller was
02:46looking for an index.ctp in a subdirectory called MailingLists. This matches the
02:52name of our controller. So let's create that subdirectory and then inside of
02:57that subdirectory, we can create index.ctp.
03:03The name index matches the name of our main function inside of the controller.
03:08You can type anything. We can just write Test for now. And then let's go ahead
03:13and load this up inside of our browser and make sure everything is working correctly.
03:17In the previous movie, we set up the CakePHP default controller to be our
03:22mailing list controller. When I go to localhost CakePHP, it should load that
03:27controller. So go ahead and go to that URL and you should see your design laid out.
03:33Note that the content isn't displayed anywhere. We need to add that into our
03:38main layout. So in our default.ctp file, there're a couple of variables you can
03:44use. One is called title for layout, and you can output it directly in the title.
03:53You don't have to use adjust in the title, however. If you want to use it, in
03:57other elements you can. I'm going to output it also inside of our h1 tag here.
04:03And then finally, you want to output your content. So in between this open and
04:07closing div tag, let's go ahead and output the other special variable,
04:12content_for_layout. If you want to set the title, you'll need to do so in your
04:17controller. In the previous movie on setting up the controller in CakePHP, I used
04:24this set method to set the title_for_layout property to join our community.
04:28So you notice when I go back and load the page, I'll see the title showing up,
04:35both in the title bar and in that main h1 tag, and because I output the content,
04:40I also see the contents of that file.
04:42So the next thing we'll need to do is build our actual form. I've already
04:46created a form in another text file, so let's go ahead and copy it and paste it
04:51into our index.ctp file. And let's take a look at what I've done. This dot form
04:58refers to the form helper that I included inside of my controller. I also have
05:05an HTML helper that I can use for other tasks if I need to.
05:10You can see I refer to the form object inside of this object. This refers to the
05:16form helper in the controller class. In my controller, I use this special
05:21helpers variable to set the form helper and the HTML helper into my controller.
05:27I can now use these in any views that are used in this controller.
05:33The form helper has a number of different methods that perform a number of
05:37different tasks. You can see the create form is going to set up our form. It's
05:42also going to output our main form tag. It's going to be a self-submitting form,
05:47so it's going to submit to the same controller and the same action that we set
05:51up to render the form. I then can add whatever inputs I want.
05:56By default, it's going to take the input name and it's going to use that to save
06:01the data. It's also going to set a label that's going to match the name. So if
06:07you look in this first input, its first name, it's automatically going to
06:11capitalize each word and create a corresponding label. So that works fine for
06:16the first name and last name; however, if I want to do more customization, I
06:20can. I can pass the second argument, that's an array, with any properties that I want to set.
06:26In this case, I'm specifically setting the label to email and I'm setting the
06:31type to email, so that I can do some validation, so the user enters a correct
06:35email format. I'm also adding an address, a state, and an input for ZIP code. Notice
06:43I change the label to ZIP code because I don't want it to say ZIP postal. I add
06:47username, a password. The bio I customized to use a type of text area instead
06:53of a text input. I then have a couple of special controls I'm adding. I'm adding
06:59a radio button, and notice I first create an array of the options.
07:04The value on the left of each item is going to be what the value is that's
07:09stored in the radio button. The value to the right will be what's displayed in
07:13the label. So in my radio function of the form helper I pass in the ID of the
07:20field. I then pass in these options and finally, I can add any additional
07:25options. In this case, I'm adding a separator, which delineates how I want each
07:30of the radio buttons to be separated.
07:32Finally, I set the legend property to false. I also create a select. This is
07:39going to show up as a check box, even though I'm using the select function in
07:43line 43. The reason why is, because you see I've set the multiple property to
07:48check box, this will allow the user to select multiple by showing them in a check box.
07:54My interest options works in the similar way to the radio buttons. The items on
07:58the left are the values that are going to be stored and saved in the database.
08:02The values on the right will be what's displayed to the user. When I'm done
08:06writing my form, I call the end method, and you can optionally pass a variable
08:11which will be what's displayed on the Submit button for the form.
08:15Once you've finished all this, you can save this and return to your controller and
08:20reload it. You should now see the form you created, displayed based on all of
08:25the options you configured using the form helper. We now have our application
08:29laid out basically how we want it. We use the default.ctp to create a default
08:35layout wrapper that's going to design anything used by this controller.
08:39We then create the index.ctp that's going to handle the main controller action,
08:44which includes generating our form. The only thing remaining in our application
08:49is to save the form data to the database when the user submits it.
Collapse this transcript
Making it all work
00:00The last remaining thing to do for our application is to handle the request.
00:04Like other things in CakePHP, this can be done in a fairly intuitive manner. Our
00:08model has a save method that we can use to interact with the database. We now
00:12just need to get the data and use that to save it.
00:15Let's go ahead and open our controller. The first thing we want to do is in our
00:19index action, we want to determine if the user is loading the form for the first
00:23time or if it's been posted. So we will create an if statement and inside, we can
00:29use the special request property of the controller class. It has a function
00:34called is and you can pass to it the value post.
00:40If the form has been submitted in order to get the page, this will resolve as
00:43true, in which case we can assume that the user has just submitted the form. The
00:48first thing we will want to do is get our post information, so we can set a data
00:53property equal to, and then we can use our request object.
01:00In order to save this, we can access the MailingList model. Because the name of
01:04the controller is MailingLists, it will automatically include the MailingList
01:09model, if it exists. So we can refer to this MailingList. That will refer to our
01:17model, and it has a function called save that's pre-built onto there by the
01:23framework. And we will just save our data.
01:26Because the post variables are going to match our MailingLists object, because
01:30the way we set it up, everything is going to work just fine, and when I call
01:33save, it's going to take that data, serialize it, and save it to our database.
01:38So if I go to my form, at localhost CakePHP, I can fill out information and submit
01:49it. You can see, something happened. It at least processed it. Now we don't know
01:55if it was confirmed or if we had any issues; we can only assume that everything worked.
02:02One thing that you are going to notice is an issue is if you fill out multiple
02:06items in your check box, it's going to cause an error. The reason why is because
02:12it's going to set everything up as an array and our database isn't really set to handle an array.
02:17So there are a couple of things we need to do to clean up our processing. The
02:22first thing we are going to do is we are going to look to see if that particular
02:27property is an array, and this gives us an opportunity to see how the data is
02:31formatted coming over.
02:33The way our data is structured is in a special array. So the first thing we can
02:39do is we can use our array to access the interest property and see if it itself
02:44is an array. If it's an array, that means the user has selected more than one
02:49box. So we can go ahead and say is_ array and now we want to refer to that
02:55specific item, and the way we do is we refer to the data object. And inside we
03:00can say MailingList, which matches our data model name. And then we have a
03:05second array inside of that array, which is going to represent each of the fields.
03:11So what I am looking for here is I am looking to see if the interests are an
03:17array; if they are, then what I am going to do is I am simply just going to turn
03:21it into a string so I can save it into my database. So I am going to rewrite
03:25that same property. And I can use the special implode function, which will
03:35essentially take an array and just bring it out into a comma-delimited string.
03:39And I will pass to it data, MailingList, interests. If you want to make it
03:48cleaner you could put it on the second line, maybe add a tab in there. So that
03:52should all be good to go.
03:54The next thing I want to do is instead of just bringing them back to the form, I
03:58want to redirect them to a different page. I can do this by using a special
04:02redirect function. So I will type in this->redirect, and then you specify where
04:09you want to redirect to. In this case, I'm only redirecting to another action
04:14inside of this controller. So all I need to specify is action and I can set that
04:21equal to, and we will just call it thanks.
04:24So that's going to redirect to the action in this controller. So now I need to
04:29just create that action, so public function thanks. And we can go ahead and set
04:35the title just like we did in the main action. So it will be
04:38this->set('title_for_layout,' and we can do the same one. You could even just
04:48copy and paste the other one.
04:49Since it's really the same functionality, we don't need to have that different
04:53of a title. If you remember from the View movie, CakePHP is automatically going
04:58to look for a corresponding template. So it's going to look in the MailingList
05:02directory for a template called thanks.ctp. So let's go ahead and create that
05:08file and inside of this file, we will simply say, "Thanks for signing up."
05:18Let's go back. And make sure you have checked multiple strings. So go ahead and
05:24fill out the form however you like. The one thing to make sure of is that you
05:28have a properly formatted email address, and also make sure to select a few of
05:33these interests so it passes the array and we can make sure that function is
05:36working correctly. So go ahead and click Join Now! and you can see it redirects
05:41us to the thanks action in the URL.
05:45So there is our application in CakePHP. We have seen that Cake hides a lot of
05:48internal processing, allowing us to get to the business of building our
05:52applications without having to write a lot of redundant code. This is really the
05:57strength of CakePHP.
Collapse this transcript
Taking it from here
00:00We built a very basic application using CakePHP and while it gave us a good idea
00:04of how the framework is organized, we really only scratched the surface of what
00:08you can do. In this movie we are going to discuss what else you can do with
00:12CakePHP, and hopefully guide you where to find it.
00:14One of the things we didn't do is use the Bake console application. Using Bake,
00:20you can generate your own code at the command line. You can see there's
00:23different options to set your database configuration, create models, views,
00:27controllers. You can create entire projects, fixtures, and even test cases. It's
00:33fairly straightforward how you specify what to do in the command line.
00:36Every aspect of the MVC paradigm that we've looked at is much more customizable.
00:42Because of all the plug-ins, behaviors, helpers and library classes, you can add
00:47all kinds of functionality to enhance how your model behaves, how your view
00:51renders, and how your controller handles every request and response.
00:55Additionally the Cake request and Cake response classes allow customizable
01:00featuring of how you handle these aspects of your application.
01:03We looked at some of the libraries earlier, but there are ton of classes in here
01:08that can help you to do everything from build Access Control Lists, send emails,
01:12and create security paradigms.
01:14To learn more, definitely check out cakephp.org. There are lot of references
01:20there. They have a lot of movies you can use to watch tutorials. CakePHP also
01:25has a Google group, which can be really helpful if you need questions answered.
01:29Lastly, the Cake foundation is offering some classes, and that could be helpful.
01:34So overall CakePHP is a full-stack framework that includes tons of features for
01:39rapid application development. It has a large core library and an active
01:43community. The next step in learning any framework is just to dive in. You
01:47really don't know till you try it. So take a look at cakephp.org and see where it goes.
Collapse this transcript
Conclusion
Goodbye
00:00In this course we have discussed the PHP MVC frameworks. We have looked at what
00:05frameworks are and how they work and how they can be helpful in your coding. We
00:09looked at the four most popular PHP frameworks.
00:12I highly recommend taking it from here and building your own application.
00:16There really is no better way to get to learn a coding environment than to
00:20actually write something in it. For each of the frameworks the "Taking it from
00:24here" video should give you some good jumping off points, both in terms of what to
00:28learn and where to find it.
00:30So that's it. I've had a lot of fun putting this course together, and I hope you
00:33have enjoyed it as well.
Collapse this transcript


Suggested courses to watch next:

PHP with MySQL Beyond the Basics (10h 27m)
Kevin Skoglund

Installing Apache, MySQL, and PHP (2h 43m)
David Gassner


Dreamweaver with PHP and MySQL (4h 55m)
David Gassner


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,141 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,025 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked