The role of a controller is that of administering the logic for a special section of your application. In general controllers are utilized in administering the logic for only one model. To take as an example if you were to make up a website that directs a rental-library, you could use a BooksController and a RentalController to administer both your rentals and your books.
Note that in Cake Controller names are only plural.
The controllers of your application are classes that extends the Cake ApplicationController class, which also extends a core Controller class. Controllers may comprise many kinds of actions: functions that are utilized in your web application to show off data in views.
ApplicationController class can be described in /app/appController.php and it may include methods that are distributed among controllers. It itself extends the Controller class which is a standardized in Cake Library.
A controller has an action as it’s single functionality. The Dispatcher directs it automatically if any incoming page request details in routes configuration. Going back to our example with the rental-library, our BooksController is supposed to include the view(), rent(), and search() actions.
The controller should be situated in /app/controllers/bookscontroller.php and include:
PHP:
-
class BooksController extends AppController{
-
function view($id)
-
{
-
//logic here...
-
}
-
function rent($customer_id, $video_id)
-
{
-
//logic here...
-
}
-
function search($query)
-
{
-
//logic here...
-
}
-
}
You should be capable to access these actions using the example URLs:
http://www.yourwebsite.com/books/view/Tom-Sawyer
http://www.yourwebsite.com/books/rent/12332/B-213ed
http://www.yourwebsite.com/books/search/Mark+Twain
The data displayed on this pages can be easily formated from the views area, for example for the rent() action we will have: /app/views/books/rent/index.thtml and so on.
Recent Comments