CakeDC Blog

TIPS, INSIGHTS AND THE LATEST FROM THE EXPERTS BEHIND CAKEPHP

Towards Data Integrity: Validations and Behaviors in CakePHP 3.0

 

Validation
Let us consider “validation” in a little more detail to see how it has been implemented and optimized in CakePHP 3.0. In addition to what we discussed in the earlier sections, validation now incorporates two complementary conceptions or areas. These include 1) data type and format validation and 2) Application rules.

1. Data Type and Format Validation

This part of the validation deals structural aspects such as data type, format validation, and basic types. Unlike in previous versions, validation is applied before ORM entities are created. This is a very useful feature that ensures everything is totally in sync and set in a way that preserves data integrity and the overall stability of the entire application. Moreover, it markedly reduces application errors and inconsistencies throughout the system. It is therefore a significant enhancement over previous versions.

2. Application Rules

Application rules are the second component of validation in CakePHP 3.0 implementation. They play a key role in quality control to ensure that all application rules and workflows are operating in an orderly and systematic fashion. This is implemented through buildRules() method in tables. Here is a code example that uses buildRules() method for articles table.

// In src/Model/Table/ArticlesTable.php

namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\ORM\RulesChecker;

class Articles extends Table
{
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn('user_id', 'Users'));
        $rules->add(
            function ($article, $options) {
                return ($article->published && empty($article->reviewer));
            },
            'isReviewed', [
                'errorField' => 'published',
                'message' => 'Articles must be reviewed before publishing.'
            ]
        );
        return $rules;
    }
}

Identifier Quoting

Identifier quoting is another CakePHP feature or process that has changed in CakePHP 3.0. In the new release, quoted identifiers, which were expensive and involved a notoriously error-prone process of parsing SQL snippets has been disabled by default - thereby removing a major source of frustration for developers. The only time you may want to enable identifier quoting is when working with column names or table names with special characters or reserved words. Here is how to enable identifier quoting when configuring a connection.

// In config/app.php

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Driver\Mysql',
        'username' => 'root',
        'password' => 'super_secret',
        'host' => 'localhost',
        'database' => 'cakephp',
        'quoteIdentifiers' => true
    ]
],


Note: Identifiers in QueryExpression objects require manual quoting or IdentifierExpression objects.

 

Updating Behaviors

Let us now turn to behaviors. As with most features that has to do with ORM, the way behaviors are setup and configured has evolved for smooth integration with the new framework. Among other things, behaviors now attach to table instances. Here are some other significant differences in the way behaviors are handled in CakePHP as compared to earlier versions.

1. Each table that uses a behavior will have its own instance. No storing of “name space” setting in a behavior is required.

2. Method signature for mixin, callback, and base class for behaviors have all changed

3. Finder methods can now be added easily by behaviors.

 

The above, in a nutshell, summarizes the main changes and enhancements in the new ORM and CakePHP 3.0 in general. Like all major releases or upgrades, the new release supplants many processes and functions in previous versions while at the same time adding many brand new features.

But as you go through the initial learning curve, please remember that you, the developer, have been the primary driving force behind the changes and enhancements. Your feedback and critiques over the years was the invaluable source that inspired CakePHP team to produce this groundbreaking and cutting-edge release that you are reviewing.

Latest articles

Navigation options for improved usability

Ease of navigation is closely linked to user experience - it can make or break how the user interacts with your website. From being able to find and use your navigation menu, through to the user being able to find the information they are looking for, it is important to ensure your navigation options are cleverly designed and stick to best practices.   If visitors are having difficulty with your navigation options, you are missing an opportunity to either create a conversion, delight your customer or engage a potential client.   When it comes to your website usability, here are some things you can focus on to ensure your site’s navigation is a user-friendly one:   1.     Keep it simple While this one may sound obvious, it is important to ensure that you avoid making your navigation difficult to comprehend. Examples of this include cluttered navigation menus, disorganized sub menus   2.     Keep it predictable While creativity makes your website stand out and is great to catch a user's attention and provoke emotion, it is important not to practise creativity in areas where predictability is preferred by the user or visitor. Such as when creating your navigation menu or the placement thereof.   3.     Keep it consistent It is key to keep the theme and structure of the different pages of your site consistent. Check our CakeDC.com menu and the different pages, each page keeps the overall theme and structure consistent. This is to ensure that your user is able to make sense of the content as quickly as possible when switching between pages.   4.     Have a clear hierarchical structure Every category and clickable sub category should have a clear hierarchical structure and should be visible in your menu. Doing this gives your user a clear view and pathway for them to go to the exact page or content that they are wanting. This point is particularly important for website that have a wide range of products or services.   5.    Make it distinct Navigation options should be clearly visible and easy to find. They should stand out from other graphics, images or backgrounds. This can be done through size, color and font.   6.    Link the logo to the homepage A good practice is to link the homepage to the logo of your company on your website. This logo should be in the same place on every page. Users have a high tendency to click on your logo, with the expectation that it will lead back to the home page as this is a generally predictable behavior across websites and design practices.   7.     Always include a search bar Search bars are necessary for making your website more usable to your visitors. Some visitors only want to find information by using a search bar within your site. Offer your users a way to navigate through your website without having to go through every page or menu option.

Difference between UX and UI

UX and UI are often misused in the tech industry. Understanding the key differences between UX and UI is beneficial, we take a quick look at both. UX, User Experience Design, and UI, User Interface Design, are both crucial to a product, such as a website, and work closely together while remaining vastly different disciplines. UX design tends to be more analytical and technical, while UI is not. A basic example for UX design would refer to how users interact with CakeDC.com, where they find the navigation menus (is this as per industry norm or do they have difficulty navigating around the website to find the information they are looking for, or how to contact CakeDC via our contact form or telephone number). Whereas UI design looks at ensuring brand relevance through the look and feel of the site, keeping color standards as per best practice. UX, User Experience Design UXD or User Experience Design refers to the process of enhancing the experience that a user has with a company, its products or its services. This is done by focusing on increasing the ease of use as well as improving the overall interaction between the user and the product or service. Good user experience design translates to customer satisfaction and loyalty so it's vitally important to ensure good design is put to practice! As a UX designer, you will  need to understand your site’s users and potential users, from creating persona’s to determining user stories and carrying out user testing. A persona could be an example of a customer who is seeking more information by contacting you versus a visitor who would like to learn more by reading your blog. UI, User Interface Design User Interface Design is the look, feel and interactivity of the product, basically referring to the means by which the user and a product (such as a website) interact with each other. The end goal of UI Design is to “achieve structure, analysis and optimization of a customer’s experience with the company and product.” UI Design includes activities that range from user guides and story lines through to UI prototyping and implementation with the development team.
  While there are differences between UX and UI, there are some similarities, let’s look at these:

  • Have a primary objective of improving customer satisfaction such as improving the use of a “contact us” form
  • Focus on the user and his/her interaction with a product/service such as having an easy to navigate menu
  • Can be applied to any product
Here is an example of the planning behind CakeDC.com

Website redesign? Here's a checklist of things you’ll need to consider

Redesigning your website can be a daunting and scary task, however, with the proper preparation and the right development team it can be a breeze! It can be a potentially long and tedious process, with a lot that can go wrong. From just a visual overhaul through to improving branding, user experience and sales, a website redesign can encompass a wide variety of changes that you can benefit from. Whatever your reasoning is for choosing a redesign, it offers you an opportunity to re-evaluate the bigger picture and see where improvements can be made. Here’s a quick checklist of things to look out for when embarking on a website redesign

  1. Strategy - why are you doing a redesign. Pencil down your main points behind the redesign project. What are your goals, ideals, visions. Where do you want the redesign to get you. What are the measurable results that you are hoping to see - importantly, you should also benchmark your current traffic and metrics.
  2. Saving your current assets - Have you made the proper back ups of the important files, media etc so that these remain accessible after the redesign of your website is complete. Go a step further, and take your metrics to work out what the most important assets and pages of your website - such product pages with the most sales or blog posts with the most views or social shares.
  3. Define your target audience - who is your idea visitor? Look at your customer journey and describe your customer.
  4. Have you checked out your competitors? Conduct competitive research - their overall look, problem areas, good ideas that appeal to you and your product/redesign.
  5. Outline your key features - identify what is most important to your website redesign. From shopping carts to news posts, landing pages, social sharing, security updates.
  6. Set your budget - outline what you want and how much you’d like to spend on it. Who - an agency, a freelancer etc - as well as the size or scope of your project, backend applications or additional features that you are looking for.
  7. Create a timeline and schedule your milestones - When do you expect to see things happen.
  8. Have you considered optimization? Don’t forget to make sure that your site is optimized social media and search engines.
  9. Test and revise - before launching!

We Bake with CakePHP