Controllers: Programming Application Logic / Page 2 | WebReference

Controllers: Programming Application Logic / Page 2


[previous] [next]

Controllers: Programming Application Logic [con't]

Action, Parameters, and Views

In CakePHP, actions are public methods of controllers that represent URLs. A general Cake URL contains suffixes like /controller_name/action_name and from this pattern Cake automatically maps the URL with a controller's action. Again, every such controller action can automatically call a view file that contains the display logic for that particular action. The appropriate view file is determined from the controller and action names. As an example, if the index() action of the BooksController is requested, the view file in /app/views/books/index.ctp will be rendered. We very often need to supply processed data to those view files from controllers, so that we can present the data in a suitable format to the user.

Interacting with View

CakePHP determines the appropriate view file for a controller's action by its naming convention. Controller can also supply processed data to those view files. To do that we can use the controller method set(). In chapter 3, we saw some uses of this set() method. In this section, we will learn some more on how we can interact with view files from controllers.

Time for Action: Passing Variables to a View

  1. Change the index() action of the BooksController (/app/controllers/ books_controller.php).
  2. Change view file index.ctp (/app/views/books/index.ctp) with the following code:
  3. Now enter the following URL in your browser: http://localhost/applogic/books/.

What Just Happened?

In the index() action, we first used the set()method to set a view variable named page_heading.

The first parameter of set()specifies the view variable's name and the second parameter defines its value. In the view file, in the first line, we simply printed out the $page_heading variable that displays the text Packt Book Store (that was set in the controller).

In the index() action, we then created an associative array named $book.

And then passed this array to the view files using the set() method like this:

As we can see, the set() method can take a single parameter as well. We can create an associative array (as we created the $book array) and pass that array to the set() method. It will automatically set all these key=>value pairs of the associative array respectively, as view variables and their values. This method can be pretty handy if we want to assign a set of variables to the view quickly. One thing to be noted, in this case, all the underscored array keys will become CamelCased view variables. Like, in our case, the book_title and release_date keys set in the controller became $bookTitle and $releaseDate variables in the correspondent view. Inside the view file, we then printed out all those variables set through the associative array $book.

Lastly, in the controller action, we defined a controller attribute named $pageTitle.

$pageTitle is a special attribute that sets the title of the rendered page. Now, if we visit this page it would look something like the following figure.

Actions and Parameters

In chapter 3, we already learned how parameters can be passed to a controller action by adding suffixes to the URL. A typical Cake URL looks like this: http://yourhost/controller/[/action][/parameters]. The elements in the URL that are appended to the hostname and separated by / are known as request parameters. We will now see more closely how these request parameters are handled by controller actions.

Time for Action: Understanding Actions and Parameters

  1. Change the index() action of the BooksController like the following:
  2. Now visit the following links and see what shows up in the browser:
  3. What Just Happened?

    We first recreated the BooksController's action index().The index() action can take a parameter named $id:

    That means, if someone requests the URL http://localhost/applogic/books/index/1, the $id parameter of the index() action will be set to 1. The default value of the parameter $id is 0. So, if no request parameter is provided through URL (like http://localhost/applogic/books/index/), the parameter $id will have the value 0.

    We also defined an array named $booksinside the index() action. The $books array has two elements containing information about two different books.

    Now, we want to show the appropriate book information depending on the request parameter. That is if 0 is supplied as the request parameter, we will show information of the 0th book. We can get the request parameter's value through $id. We just passed the $idth element of the $books array to the view file:

    The view file (/app/views/books/index.ctp) that we created in the previous Time for Action will work for this one too, without any change. Now, if someone visits the URL http://localhost/applogic/books/index/0, the index() action will show the information of the first book. If 1 is supplied as parameter $id, the second book's information will be displayed.

    But what if the parameter supplied has got a value that is unacceptable parameter— like a non integer, or an integer that is less than 0 or greater than 1 (the highest index of the $books array). In those cases, our code would just throw errors. To handle these exceptional cases, we added an if condition in the action. If any of those exception happens, we made $id = 0, so that we can bypass the errors gracefully.

    Now if we go to the URL http://localhost/applogic/books/index/xyz, it would just show the information of the first book.

    Following the same technique, we can have more than one parameter for a particular action. In the next Time for Action, we will see an example of such a controller action.

    Cover: WordPress for Business Bloggers

    This chapter is an excerpt from the book, CakePHP Application Development by Ahsanul Bari, Anupom Syam, published by Packt Publishing Ltd, July 2008, ISBN 1847193897, Copyright 2008 Packt Publishing Ltd

    [previous] [next]