A CakePHP Controller is used to manage the logic for objects in a CakePHP application. In this tutorial, you will learn about the AppController and how controllers gather data from the request, call the needed model object, and pass the data back to the view layer for rendering.
- Let's re-examine our MVC diagram for where our controllers sit. Controllers, recall, are the main glue for an MVC application. Controllers get passed data from the request, call the model as needed, and then pass data back to the view layer for eventually rendering, typically as HTML. But what specifically does a controller do? A controller's job is to manage all the details and logic for dealing with some object. In the cakePHP world, this means we are dealing with a model object, and the database table that it corresponds to.
In our example application, the Users Controller deals with the logic related to the user model, and the user's database table. However, controller doesn't have to be necessarily backed by a model. We can manage the logic for anything in particular. So if we wanted a User Accounts Controller to manage the conceptual idea of a User Accounts, even though we don't have a User Accounts Database Table, we can create a controller to do just that. The first step in working with controllers in cakePHP is learning about the AppController.
To do this, we want to open up the AppController in our project's files. In your code editor, open up the project's Exercise Files. In there, you'll see the Directory App, and the Subdirectory Controller. And in there, you'll see the file AppController.php. Open this now. The AppController is a parent class to all of our other controllers. All of our other controllers will inherit from here, so we do things here that will also occur in all of our other controllers.
Let's start at the top of the file on line two. Our controller stars with an App::uses line. App is a static php class, for requiring a class and being able to use it inside of the file. If you have used the PHP methods Require and Include, it's baiscally a smarter, cakier version of that. App::uses follows the pattern of you to find the name of the class to include, and then the location of the class. In this case, we are including the controller class, which is found in the controller path.
So an App::uses will search in all the places that it expects a controller to exist and load the corresponding controller class. In this case, this is a class file that exists inside of the cake PHP core, so we'll load that particular class. Continuing on down to line four, we write out that the class name is AppController, and it extends the controller class. Pretty standard PHP5 object-oriented programming here. Line 11 is where we start with our custom code. Here, we have an array property, components.
Components is a type of cakePHP class we haven't discussed yet. Components essentially are helper classes for our controllers. Here in the AppController, we are basically saying, "Load this component and make it available "in all of our sub-cakePHP classes." Line 20 is a similar bit of code, this time loading helpers for use in our view layer. Helpers are, get this, helper classes for our view layer. Line 31 again follows this pattern, in this case loading any models that this controller will need.
By default, the controller will load the primary model the controller relates to. For instance, the User's Controller loads the User's Model and so forth. That's all that we'll deal with for our AppController for this particular section.
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