In this CakePHP tutorial, you will learn how to use the CakePHP Console and bake shell to rapidly build a basic CRUD (Create, Read, Update and Delete) application. The bake shell allows you to quickly build an application without needing to know a lot of PHP beforehand.
- In this section, we'll use the CakePHP console and the Bake Show to rapidly build a basic CRUD or Create, Read, Update and Delete application. Bake allows us to get a CakePHP application in place without needing to know a lot about CakePHP ahead of time or even knowing a lot PHP in general. First though, we need to make a few changes to the base CakePHP project. If you open the Exercise Files, there is a snippets.txt file that we need to copy the contents of and replace our app controller appcontroller.php with.
So open up the snippets.txt file, copy all of that. Close it, open up App, Controller, App Controller and copy and paste the code into here. Before we can bake, we need to have a few properties and some methods set up in our App Controller. We'll come back around to each of these specific properties and the methods later on in the course. Now that we have updated our app controller, let's read the Help information for Bake. We want to navigate to the terminal and in our Projects Root folder, enter in the command "lib/Cake/Console/cake bake --help." The Bake script tells us that it will bake pieces of a project given the options that we passed to it.
So let's take Bake one piece at a time. Bake has several sub commands that we can call, including "all, project, plugin, db_config, model, "view, controller, fixture and test." In this case, since we're wanting to bake a full MVC stack for each of our database tables, we're going to use "Bake all." So we're going to run "cake bake all" on each of our tables. So let's type out "lib/Cake/Console/cake bake all." We should see a list of our database tables.
Let's choose the first one, category. So enter "1" and hit "enter." You may see a message stating that "PHPUnit is not installed. "Do you still want to bake unit test files anyways?" In this case, we want to answer "yes." So enter in "y" and "enter," "y" and "enter" again, and we'll see a message saying that Bake All was complete, and if you scroll back through the terminal you'll see that Bake is outputting that it baked all these different view files and controllers and test files and everything else for us.
Now we need to just repeat the cycle of baking a full MVC stack for each of our other tables. So if we hit the up arrow in our terminal, we'll get the last command run. So we want the last command run, hitting "enter" and here we want to bake our next table, the product table, so we'll enter "2" and "enter." Answer "yes" to baking the unit test files and again, it Baked All for us. We're going to repeat this process again, hit the up arrow, and "enter" and this time we don't need to bake a full MVC stack for our two joined tables, ProductsSale and ProductsTag.
At these two tables aren't really needed for us to have a full MVC stack. All that they do is just create joins between products and sales, and products and tags. So next we're going to bake the Sale table, so enter "5" and "enter" and "yes" and "yes" again and repeat this again. This time, baking the tag table so enter "6" and "enter," "yes" and "yes" and we want to repeat this again, this time baking the UserAddress table, which is number seven, and "yes" and "yes" again, and finally, we need to bake our last table, the User table, so we'll enter "8" and hit "enter," "yes" again and "yes" again.
So now we've baked a basic MVC stack for the project. Let's take a look at what it looks like. We'll start with the user's view. First, we need to start our web server to serve our files. Since PHP 5.4, PHP has included a basic built-in web server to serve up PHP files. We can use this with the CakePHP shell feature to serve up our PHP application. To start this, we want to run "lib/Cake/Console/cake server " -p 8080." This will start our web application and we can view it by navigating to local host port 8080 in a browser.
If we open up our web browser and go to "local host port 8080" we should see the CakePHP homepage. This page details some information about our application, basically ensuring we have our application set up correctly. And if we go to the user's end point by going to "local host port 8080\users" we see an index page for our users, including some buttons along the side for navigating to other portions of our applications, and even adding a new user.
Let's try out adding a new user now. Click on the "new user" button. Here we see a basic form for adding a new user. Let's add in a new user. Let's give them an email address of "email@example.com," a password of "random," first name of "Justin" and a last name of "Smith" and we'll submit this form. Notice, we get a nice success message, we get our new user, and we even have the created and modified date time value set for this user that we just created.
We have links to both view and edit our user as well. Let's edit this user and change the last name from "Smith" to "Yost" so our first name is going to be "Justin" and our last name is going to be "Yost" and if we save it, we get a success message, we see that our new last name for the user is "Yost" and we see that our modified date time value has changed. All the other tables in our application have this sort of minimum tooling built up around them through Bake. We are going to explore the other Baked views, however, if you want to explore this now, feel free to click around this Baked application and see what CakePHP has done already for us through the Bake Show.
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.
- 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