E-Book, Englisch, 346 Seiten
Watts CakePHP 2 Application Cookbook
3. Auflage 2025
ISBN: 978-1-78216-009-0
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)
Over 60 useful recipes for rapid application development with the CakePHP framework.
E-Book, Englisch, 346 Seiten
ISBN: 978-1-78216-009-0
Verlag: De Gruyter
Format: PDF
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)
If 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.
Autoren/Hrsg.
Weitere Infos & Material
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 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:
Before we begin, we'll also need to create . To do so, create a file named in and add the following content:
Now, create a directory named in . Then, in this directory, create one file named and another named .
How to do it...
Perform the following steps:
- Define the pagination settings to sort the products by adding the following property to the class: public $paginate = array( 'limit' => 10 );
- Add the following method in the class: public function index() { $this->Product->recursive = -1; $this->set('products', $this->paginate()); }
- Introduce the following content in the file that we created:
Paginator->sort('id'); ?> Paginator->sort('name'); ?> Paginator->sort('created'); ?> Html->link($product['Product']['name'], array('controller' => 'products', 'action' => 'view', $product['Product']['id'])); ?> Time->nice($product['Product']['created']); ?> Paginator->counter(array('format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}'))); ?>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')); ?> - Returning to the class, add the following 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 file:
- Time->nice($product['Product']['created']); ?>
- Time->nice($product['Product']['modified']); ?>
- Now, navigating to in 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 class, which defines how the results are treated when returning them via the component (previously defined in the 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 method to our class, which returns the listing of products. You'll first notice that we accessed a 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 (in plural), it automatically assumes a (in singular) model. Then, in turn, this model assumes a 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 method, we set the property of the model to . 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 method to handle the resolving of the results via the component.
Tip
It's common practice to set the property of all models to 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 class, which all models extend, or via an intermediate class that you may be using in your application.
We had also defined a method, which is used to resolve a single product and display its details. First, you probably noticed that our method receives an 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 . 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 table aren't sequential numbers in this case. This is because we defined our field as . When doing this, CakePHP will use a Universally Unique Identifier (UUID) instead of an value.
Tip
To use a UUID instead of a sequential ID, you can use either or . Here, we used , but note that it can be less performant than 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 method on the 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 . For example, you would be able to get all records that have the given value for the field in the table. These methods are very useful to easily perform queries on the associated table without having to define the methods in...




