Create and use a custom component

Keyboard Shortcuts

Custom components are a good way to reduce duplicated code in multiple CakePHP controllers. In this training video, Justin Yost shows you how to create and use a custom CakePHP component to perform a standard summation calculation that can be called from each of the components that need it.

- Like we've discussed earlier,…components are used to reduce…duplicated code in our controller.…Whereas the place where we have some…duplicated code in our controller…that we can reduce.…Well, in our app, Controller, UserAccountsController,…if you look at lines 9 through 12,…those lines are very similar.…In fact, they're almost identical,…except for a few minor changes.…Let's reduce this duplication of code…by creating a custom component…called the MathComponent to do this math for us.…We're going to start by needing to create…a new file in our component directory.…

That's in app, Controller, Component, New File,…and we're going to call this file MathComponent.php.…We need to start by first creating our open PHP tags.…Next we need to have an app use this line…to pull in the core component class.…The core component class in CakePHP…is found in the controller path.…So, following the pattern that we've seen earlier,…it's going to be App: : uses Component, Controller…Next, we need to actually define our new class…that we're going to be creating.…

Resume Transcript Auto-Scroll

Author

Released

12/4/2015

CakePHP is a registered trademark of Cake Software Foundation, Inc.. Cake Software Foundation, Inc. is not affiliated with Lynda.com, nor does it endorse, sponsor, or otherwise support Lynda.com services.

CakePHP is a critical framework for PHP developers. It helps them build complex web applications faster and more efficiently than coding by hand. If you want to use CakePHP in your own development workflow, this is the place to start. Justin Yost provides an overview of the underlying MVC pattern in CakePHP, and the installation and configuration process for the framework. He shows how to use CakePHP shells and the console to build your first basic CakePHP app, and then discusses each application element in depth: controllers, models, views, components, behaviors, helpers, and utilities.

In later chapters the course gets a little more advanced. Watch these videos to learn how to write a custom shell, extend CakePHP with plugins, and write unit tests to identify and eliminate bugs in your code. Justin also shows how to add security to your CakePHP apps with a basic user authentication system.

NOTE: While CakePHP 2 is a slightly older version of the framework, it's still an actively developed tool that is useful for maintaining and updating web applications. Many of the conventions and problem-solving approaches in this course are also applicable to CakePHP 3.

Topics include:

Comparing CakePHP 2 and CakePHP 3

Installing and configuring CakePHP

Using different CakePHP shells

Creating CakePHP controllers

Saving data in a CakePHP model

Creating views

Using components to share functionality between controllers

Using behaviors

Formatting data with helpers

Developing faster with CakePHP utilities

Writing a custom CakePHP shell

Creating a custom plugin

Testing CakePHP applications

Authorizing users of CakePHP applications

Skill Level Intermediate

4h 55m

Duration

42,303

Views

Show MoreShow Less

Q: I can't open the exercise files. What's this .7Z extension?

A: The .7Z extension is for a 7-Zip file. 7-Zip is an open-source file compression standard that is similar to a ZIP file, but it has a much better compression rate in certain situations. For the exercise files in this course, using a standard ZIP file would have added around 150MB to the download size, so we opted for a more efficient format.

To extract the .7Z file, you'll need some free software. If you visit 7-zip.org, you can find free, open-source software for Windows. For Mac users, please see kekaosx.com to download Keka, a free application that can do the same. For those with software security policies in place, Keka is also available on the Mac App Store for $1.99.

Skills covered in this course

Course Retiring Soon

Learning CakePHP 2 will be retired from the lynda.com library on March 30th, 2018. Training videos and exercise files will no longer be available, but the course will still appear in your course history and certificates of completion. For updated training, check out Learning CakePHP 3 in the lynda.com Online Training Library.

Continue Assessment

You started this assessment previously and didn't complete it. You can pick up where you left off, or start over.

Start My Free Month

Start your free month on LinkedIn Learning, which now features 100% of Lynda.com courses. Develop in-demand skills with access to thousands of expert-led courses on business, tech and creative topics.