
CakePHP 2 Application Cookbook
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
Book DescriptionIf you are a CakePHP developer looking to ease the burden of development, then this book is for you. As a headfirst dive into the framework, this collection of recipes will help you get the most out of CakePHP, and get your applications baked in no time. Even if you're not familiar with the framework, we'll take you from basic CRUD building to useful solutions that will aid in getting the job done quickly and efficiently.What you will learn
Who this book is for
All prices
More details
Other editions
Additional editions

Person
Content
Lightning Intro
Advanced Routing
HTTP Negotiation
API Strategies
Using Authentication
Model Layer
Implementing Search
Events System
Creating Shells
View Templates
Unit Tests
Migrations
Listing and viewing records
To begin, we'll need a way to view the products available and also allow the option to select and view any one of those products.
In this recipe, we'll create a listing of products as well as a page where we can view the details of a single product.
Getting ready
To go through this recipe, we'll first need a table of data to work with. So, create a table named products using the following SQL statement:
We'll then need some sample data to test with, so now run this SQL statement to insert some products:
INSERT INTO products (id, name, details, available, created, modified) VALUES ('535c460a-f230-4565-8378-7cae01314e03', 'Cake', 'Yummy and sweet', 1, NOW(), NOW()), ('535c4638-c708-4171-985a-743901314e03', 'Cookie', 'Browsers love cookies', 1, NOW(), NOW()), ('535c49d9-917c-4eab-854f-743801314e03', 'Helper', 'Helping you all the way', 1, NOW(), NOW());Before we begin, we'll also need to create ProductsController. To do so, create a file named ProductsController.php in app/Controller/ and add the following content:
Now, create a directory named Products/ in app/View/. Then, in this directory, create one file named index.ctp and another named view.ctp.
How to do it...
Perform the following steps:
- Define the pagination settings to sort the products by adding the following property to the
ProductsControllerclass: public $paginate = array( 'limit' => 10 ); - Add the following
index()method in theProductsControllerclass: public function index() { $this->Product->recursive = -1; $this->set('products', $this->paginate()); } - Introduce the following content in the
index.ctpfile that we created: <h2><?php echo __('Products'); ?></h2> <table> <tr> <th><?php echo $this->Paginator->sort('id'); ?></th> <th><?php echo $this->Paginator->sort('name'); ?></th> <th><?php echo $this->Paginator->sort('created'); ?></th> </tr> <?php foreach ($products as $product): ?> <tr> <td><?php echo $product['Product']['id']; ?></td> <td> <?php echo $this->Html->link($product['Product']['name'], array('controller' => 'products', 'action' => 'view', $product['Product']['id'])); ?> </td> <td><?php echo $this->Time->nice($product['Product']['created']); ?></td> </tr> <?php endforeach; ?> </table> <div> <?php echo $this->Paginator->counter(array('format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}'))); ?> </div> <div> <?php echo $this->Paginator->prev(__('< previous'), array(), null, array('class' => 'prev disabled')); echo $this->Paginator->numbers(array('separator' => '')); echo $this->Paginator->next(__('next >'), array(), null, array('class' => 'next disabled')); ?> </div> - Returning to the
ProductsControllerclass, add the followingview()method to it: public function view($id) { if (!($product = $this->Product->findById($id))) { throw new NotFoundException(__('Product not found')); } $this->set(compact('product')); } - Introduce the following content in the
view.ctpfile: <h2><?php echo h($product['Product']['name']); ?></h2><?php echo h($product['Product']['details']); ?>
<dl> <dt><?php echo __('Available'); ?></dt> <dd><?php echo __((bool)$product['Product']['available'] ? 'Yes' : 'No'); ?></dd> <dt><?php echo __('Created'); ?></dt> <dd><?php echo $this->Time->nice($product['Product']['created']); ?></dd> <dt><?php echo __('Modified'); ?></dt> <dd><?php echo $this->Time->nice($product['Product']['modified']); ?></dd> </dl> - Now, navigating to
/productsin your web browser will display a listing of the products, as shown in the following screenshot: - Clicking on one of the product names in the listing will redirect you to a detailed view of the product, as shown in the following screenshot:
How it works...
We started by defining the pagination setting in our ProductsController class, which defines how the results are treated when returning them via the Paginator component (previously defined in the $components property of the controller). Pagination is a powerful feature of CakePHP, which extends well beyond simply defining the number of results or sort order.
We then added an index() method to our ProductsController class, which returns the listing of products. You'll first notice that we accessed a $Product property on the controller. This is the model that we are acting against to read from our table in the database. We didn't create a file or class for this model, as we're taking full advantage of the framework's ability to determine the aspects of our application through convention. Here, as our controller is called ProductsController (in plural), it automatically assumes a Product (in singular) model. Then, in turn, this Product model assumes a products table in our database. This alone is a prime example of how CakePHP can speed up development by making use of these conventions.
You'll also notice that in our ProductsController::index() method, we set the $recursive property of the Product model to -1. This is to tell our model that we're not interested in resolving any associations on it. Associations are other models that are related to this one. This is another powerful aspect of CakePHP. It allows you to determine how models are related to each other, allowing the framework to dynamically generate those links so that you can return results with the relations already mapped out for you. We then called the paginate() method to handle the resolving of the results via the Paginator component.
Tip
It's common practice to set the $recursive property of all models to -1 by default. This saves heavy queries where associations are resolved to return the related models, when it may not be necessary for the query at hand. This can be done via the AppModel class, which all models extend, or via an intermediate class that you may be using in your application.
We had also defined a view($id) method, which is used to resolve a single product and display its details. First, you probably noticed that our method receives an $id argument. By default, CakePHP treats the arguments in methods for actions as parts of the URL. So, if we have a product with an ID of 123, the URL would be /products/view/123. In this case, as our argument doesn't have a default value, in its absence from the URL, the framework would return an error page, which states that an argument was required. You will also notice that our IDs in the products table aren't sequential numbers in this case. This is because we defined our id field as VARCHAR(36). When doing this, CakePHP will use a Universally Unique Identifier (UUID) instead of an auto_increment value.
Tip
To use a UUID instead of a sequential ID, you can use either CHAR(36) or BINARY(36). Here, we used VARCHAR(36), but note that it can be less performant than BINARY(36) due to collation.
The use of UUID versus a sequential ID is usually preferred due to obfuscation, where it's harder to guess a string of 36 characters, but also more importantly, if you use database partitioning, replication, or any other means of distributing or clustering your data.
We then used the findById() method on the Product model to return a product by it's ID (the one passed to the action). This method is actually a magic method. Just as you can return a record by its ID, by changing the method to findByAvailable(). For example, you would be able to get all records that have the given value for the available field in the table. These methods are very useful to easily perform queries on the associated table without having to define the methods in...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: PDF
Copy-Protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (only limited: Kindle).
The file format PDF always displays a book page identically on any hardware. This makes PDF suitable for complex layouts such as those used in textbooks and reference books (images, tables, columns, footnotes). Unfortunately, on the small screens of e-readers or smartphones, PDFs are rather annoying, requiring too much scrolling.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our eBook Help page.