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