In this CakePHP tutorial, Justin will teach you how to write custom CakePHP 3 controller methods. You'll learn how to create new actions with matching URLs to enable you to extend and customize your CakePHP 3 applications.
- View Offline
- Now that we understand the basics of what a Controller is and what it is doing, let's actually work in our Controllers and add a new feature to our application. Something that our customers have been asking for is the ability to export their bookmarks, so let's add this feature. First off, since we are working with bookmarks, we're going to add this new feature to the Bookmarks Controller, so open up the Bookmarks Controller, which again is in source, Controller, BookmarksController.php.
We want a new method in our Controller that we'll call the export method, so let's add this method after the index method on line 30. We'll add public function export, so we have a new method. What we need is, to get a collection of bookmarks for exporting. To do this, we want to talk to the table class and run a sequel query to find a group of bookmarks. We've seen a bit of talking to the table classes earlier. That is, the this->Bookmarks that we can see on line 45.
The object, bookmarks, is the table class for our bookmarks database table. CakePHP includes an ORM layer that we can utilize to write sequel queries with eliminating issues like sequel injection, and without a lot of deep knowledge of SQL. To find a collection of bookmarks is pretty easy. We want to write, on line 31, this->Bookmarks->Find('all');, and there. We've just wrote our first sequel query for CakePHP.
This query finds all the bookmarks. Let's sign the output to a variable, bookmarks. Next, let's see what happened. Remember to have start at your Cake server and navigate to /bookmarks/export in your browser. And we see an error page telling us that there is a missing template. This is CakePHP informing us that the template file for this action is not present. So CakePHP doesn't know how to render this page, and shows no view portion of our MVC cycle for this action, so let's add a template now.
You'll note, CakePHP tells you exactly what the error is and how you can correct it by creating a new file, export.ctp, in the template bookmarks directory, so let's do that now. So go back to your code editor. Scroll to the source, Template, Bookmarks, and create a new file here labeled export.ctp. Now if we go back to the browser and refresh the page, we'll see the error went away, but we don't see any bookmarks, but this makes sense, right? Our template isn't actually doing anything, so let's correct the issue.
Go back to the code editor, and edit the export.ctp file you just created to perform a foreach loop over the bookmarks variable, the value portion being the singular bookmark, so that code is open PHP brackets, foreach bookmarks as $bookmark, and remember to close your PHP tags. Inside the foreach loop, we'll use a special CakePHP method called debug, that displays debugging information about a variable, so on line two, we'll read this as debug, and the variable bookmark, and if we go back to the browser and refresh the export page, we see another error.
Okay, these are telling us that the bookmarks variable is undefined. We never told the Controller to set the bookmarks variable to the view layer, so let's do that now. We want to go back to the Bookmarks Controller in our code editor, and we need to set the variable bookmarks, so on line 32, we'll call this set('bookmarks'). This first string, it's a name of the variable that is being set to the view layer, so the view layer will now have access to a variable called bookmarks.
The second parameter is the value of that variable. In this case, we want to set the variable bookmarks here, so this says, pass the bookmarks variable to the view layer and name it bookmarks. Save this. Go back to our browser, and refresh the page one more time, and there we go. We see each individual bookmark from our database. Notice a few things here. These are instances of the bookmark entity, which is a special class that represents a single row from the database.
Also note the created and modified date time values are instances of a Cake frozen time class. For now, we did good work. We made a lot of progress towards creating the ability for our users to export their bookmarks. In creating both a new method in our Controller as well as a view template to display them.
CakePHP is a critical framework for PHP developers. It helps them build complex web applications faster and more efficiently. If you want to use CakePHP 3 (the latest version of the framework) 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 Mac and Windows. He shows how to use the CakePHP shell to build your first basic CakePHP app, and then discusses each application element in depth: controllers, models, views, components, behaviors, helpers, and utilities. At each step, he discusses the relevant new features and enhancements in CakePHP 3, including new components; performance, session management, and ORM improvements; and localization.
In later chapters, the course gets a little more advanced. Watch these tutorials to learn how to send email with CakePHP, 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.
- Installing and configuring CakePHP
- Using the CakePHP shell console
- Creating CakePHP controllers
- Saving data in a CakePHP model
- Finding and deleting data
- Working with entity methods
- Creating CakePHP views
- Using and customizing components to share functionality between controllers
- Creating behaviors
- Formatting data with helpers
- Developing faster with CakePHP utilities: hash, collections, and logging
- Sending CakePHP email
- Creating a custom plugin
- Testing CakePHP applications
- Authorizing users of CakePHP applications