Generating a controller with Bake
Video: Generating a controller with BakeI'm going to use the console and the Cake utility to generate the controllers for my models. Cake, bake, controller. Similar to model generation, bake will scan the database for tables to generate controllers for. Let's start with publications, so type 2. The next question will be for interactive controller generation. Yes, I'd like to see all the questions. Would I like to use dynamic scaffolding? What's that? Dynamic scaffolding means that CakePHP will generate code on the fly based on the current contents of the database, rather than writing it to files.
Viewers: in countries Watching now:
Speed up your development with CakePHP, the popular open-source PHP framework. In this course, author Jon Peck builds a magazine cataloging system while explaining how to work with the Model-View-Controller (MVC) development pattern.
The course demonstrates how to install and configure CakePHP, describing the layout and components. Then, leveraging the Bake console, Jon shows how to generate and customize code, and explores form validation, database persistence, and even image uploads. By developing a complete, functional application, you'll have the foundation you need to build your own applications with CakePHP.
- What is CakePHP?
- Installing and configuring CakePHP
- Generating a model with Bake
- Linking models together
- Generating a controller
- Saving a model from the controller
- Displaying a model with a view
- Debugging a failed save
- Using helpers to generate HTML
- Uploading files and displaying images
Generating a controller with Bake
I'm going to use the console and the Cake utility to generate the controllers for my models. Cake, bake, controller. Similar to model generation, bake will scan the database for tables to generate controllers for. Let's start with publications, so type 2. The next question will be for interactive controller generation. Yes, I'd like to see all the questions. Would I like to use dynamic scaffolding? What's that? Dynamic scaffolding means that CakePHP will generate code on the fly based on the current contents of the database, rather than writing it to files.
This is much slower, but gives a lot of flexibility during active development. I'm going to say no, let's write the methods to the controller. Next is the basic class methods. Index, which generates a list, add, view and edit. Yes, please. This is massively time saving, and we'll be looking at these in depth. Admin routing basically sets up a section of the site where a privileged user can administer content. As user management is out of the scope of this course, I'll just say no. Helpers which I'll cover in greater depth in a moment contain presentational logic that can be used to build views.
Two are mentioned here. HTML helper and form helper, and are built into Cake's core, and generate tags and HTML forms. No additional helpers are needed, so I'll just say no. Components are packages of common controller logic, such as security, sessions, authentication, and more. I'll cover components later on in detail, but here's a gotcha. The very next question will ask if I want to use session flash messages, and this requires the session component. If I don't do this, the application will crash.
So I will say yes, and then specify Session with a capital S. Would I like to use session flash messages? They're used for one time notifications for feedback, like publications saved and so forth. This is used for, for communicating with users. So say yes to add this helper, which depends on the session component. Saying yes to this will not automatically add the session component. Finally, Cake will ask if I want to create the controller for publications. Yes, and the file is written to the app in the controller's sub directory.
Again, I'm not generating tests, so say no. While we're at it, let's create the controller for issues as well. One for issues, yes for interactive, no to dynamic scaffolding, yes to basic class methods, no to admin routing, no to additional helpers. Yes, and we're going to need session again. Yes to session flash messages. And this looks okay, so write it. No tests, and we're done.
And let's see what we've created. Switch over to the IDE, and navigate to AppController. I'm going to open publications controller first. The first functional line is app uses app controller, controller. Similar to model, this describes what package the AppController is in to cake. The class definition contains the name of the controller, publications controller and extends AppController. If we take a quick look at AppController, if I go to Navigate, go to Declaration.
It just extends controller from CakePHP core with the addition of the debug kit toolbar which we put in earlier. Close the AppController. Back in publications, there's only one property, components, which is an array containing two strings with the names of the components that will be available to the controller paginator end session. Let's look at the actions generated by bake. The first index takes no parameters. The first line is to the publications model which is available through this. It sets the recursive attribute which tells Cake how to perform fetches.
Zero tells Cake to fetch group data and anything within its domain. The next line is a call to the set method, which is used to specify what data to send to the view as an associative array. The first parameter is the key to be used, which is publications. The model, plural. The second line is a call to the paginator component telling it to do the data lookup. I'll dive into components later on. So for now, let's close, publicationscontroller.php. Let's see what we created in the browser.
By default CakePHP will route request by the first part of the path, mapping the controller name and the second part mapping to the action. The first action that we looked at was index, which is also the default action. So to see it, let's just navigate to Sandbox and go to Publications. I'm getting errors about a missing view and instruction is telling me to create the view. Obviously there's still a missing piece here. Even though we don't have any records yet, let's get the index to a state where it isn't crashing when we try to look at a list of the publications by creating a view.
There are currently no FAQs about Up and Running with CakePHP.