Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Generating a controller with Bake

From: Up and Running with CakePHP

Video: 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.

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.

Show transcript

This video is part of

Image for Up and Running with CakePHP
Up and Running with CakePHP

27 video lessons · 4643 viewers

Jon Peck
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Up and Running with CakePHP.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

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

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

Sign up and receive emails about lynda.com and our online training library:

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

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.