• Trusted by:

Hire CakePHP developers and engineers

Bryce Ott, United States

Member since April 17, 2014
With more than 13 years working as an engineer, architect, director, vice president, and chief technology officer, Bryce brings a deep understanding of enterprise software, management, and technical strategy to any project. His specialties include real-time systems, business intelligence, big data, enterprise web apps, scalability, and open-source software. Click to continue

Francisco Sanchez Clariá, Argentina

Member since November 2, 2012
Francisco is an engineer specializing in hardware-software technology integration. He also has extensive experience in innovative solutions, distributed data manipulation systems, extension of web applications to ease common business tasks, custom UI design, and application usability with a strong focus on sustainability. Click to continue

Adewale Andrade D. Alcantara, Brazil

Member since January 30, 2013
Adewale is a reliable and proficient developer with an interest in developing efficient web applications. He has over four years of experience with a number of programming languages and platforms, and has held positions ranging from team member to team leader. Click to continue

Guilherme Fernando Ferreira, Brazil

Member since January 20, 2014
Guilherme is an Agile developer with a BS in Computer Science. He is a passionate programmer with over 10 years of experience in Ruby, JavaScript, Python, PHP, Java, and more. He has spoken at conferences including Campus Party, Agile Brazil, and Agile Tour. Click to continue

Maksym Petkus, Ukraine

Member since March 28, 2014
Maksym is an advanced developer, an engineer with extensive experience in web-development and systems architecture. He developed complex systems for high-load and high availability, reliable projects like payment processing. He’s an enthusiastic entrepreneur when with team lead experience. Click to continue

Ivan Bernat, Croatia

Member since January 21, 2014
Ivan is a full stack web developer who has shipped over 400 front-end projects, a dozen smaller web apps, and several WordPress themes. Ivan has experience working both as a standalone freelancer for clients from around the globe and as an in-house developer in large teams. Click to continue

Jordan Ambra, United States

Member since June 6, 2013
Jordan is a top-notch architect, developer, sysadmin, and entrepreneur with the passion and experience to help businesses solve complex problems. He is an expert full-stack developer, bringing projects from concept to completion, with a proven track record of delivering powerful, stable, and comprehensive solutions. Click to continue

Michał Tatarynowicz, Poland

Member since July 18, 2014
Michał has been a professional full-time developer for thirteen years. Before that, he worked mostly in advertising companies doing technical, print-related work, and website development. He is very passionate about code quality and how it relates to developer productivity and the company bottom line. Click to continue

Richard Garand, Canada

Member since October 22, 2013
Richard specializes in performance and scalability. He has helped to design, implement, and maintain server systems handling millions of users per month. When you need a 10x or 100x improvement in performance, he is ready to step in. Throughout his work, he aims to teach both managers and other developers exactly what's going on and how they can think about it in a more effective way. His experience primarily covers Linux, MySQL, and PHP. Click to continue

Michael Houghton, Ireland

Member since March 6, 2014
With twelve years of back-end development in PHP combined with a background in business, Michael brings a great balance of technical and business expertise to the table. He has experience with vanilla PHP websites as well as CakePHP, Zend, CodeIgniter, PhalconPHP, and Laravel frameworks. He's also gained project management experience, leading teams and working directly with clients to build projects from start to finish. Click to continue
CakePHP Resources
Trustpilot
Toptal on TV
"Toptal is killing it, it kind of owns this market."
Andrew Ross Sorkin Reviews Toptal
Cnbc 02e66b
"Tech companies simply cannot find the quality employees they want to hire. The solution: Toptal."
Liz Claman´s Review of Toptal
Fox 7ef901
Toptal in the press
“Despite accelerating demand for coders, Toptal prides itself on almost Ivy League-level vetting.”
Selina Wang
“Toptal allows corporations to quickly assemble teams that have the right skills for specific projects.”
Steven Norton
A hiring guide
The Vital Guide to CakePHP Interviewing

Fully mastering CakePHP can take some time, which makes finding true CakePHP experts a real challenge. The questions presented in this guide can be highly effective in evaluating the breadth and depth of a developer's knowledge of the CakePHP framework.

Testimonials
“One of our biggest and most time consuming problems is finding qualified staff. We need to find the correct platform to advertise on, create a proper ad, filter through tons of useless applications to try and find the small gems, interview and test the capabilities of applicants to see if they fit our profile and finally take a risk by hiring. This was the old way of doing things! The new way: describe the job position to our Toptal account manager, interview a few qualified applicants, hire with a risk-free trial! Much faster, a lot easier and with incredible end results.”
Catalin S. Balaci
Owner @ EvoBits Information Technology SRL
“Robert was such a breath of fresh air! I’m used to working with developers who don’t speak good English, don’t understand my needs, and are bad communicators in general. But Robert was not only an EXCELLENT communicator, he knew exactly what I was looking for and made it better than I thought possible. Now I have a website that works better than I thought it could for less than the cost it would have taken other developers, and in half the time. Robert is as good as they come, and a true professional. Anyone thinking of working with him, stop thinking about it and hire him. You won’t regret it.”
Matthew Bennett
CEO @ Winning Social
“I can't say enough about how happy and confident I am in working with Enrique. He brings an enthusiasm to his profession that is very refreshing, constantly surprising us with his depth of knowledge and his capability to not only complete the task, but to surpass our expectations. He presents us with solutions that we would not have thought about ourselves, and sees the problem better than we can explain it.”
Chad Berryman
Senior Technical Lead @ Zoomedia
“I needed help, and fast. I did not want to hire a random coder I met online- I was worried about security, and many other issues. Toptal was there when I needed them- and has been fantastic to work with. Outsourcing is full of terrible stories- Toptal is the story that is the giant exception. I give them my highest recommendation.”
Michael Feder
Founder & CEO @ PrayerSpark
“It has been wonderful to work with Fabian for the past two years. He is an expert in JavaScript development and has built our embeddable widget and video player. He is a quick study and is able to learn how to integrate new technologies, which is so critical for a startup. He even took the extra effort to teach others on the team. He is super responsive and often went above and beyond to help us meet deadlines (including working on weekends). Thank you Fabian. Thank you Toptal for making it so easy to find and work with amazing talent.”
Natasha Mohanty
CTO @ FEM Inc
“We needed a programmer to help push a project over the finish line, and I figured -- best case -- Toptal would find someone who would come in and complete a few tickets. Instead, Sergio blended right in with the team and made significant progress after mere days of ramp-up. He even offered to take his laptop with him on vacation in case we had a customer issue with his code! He surpassed my expectations, and then some.”
Bryan Adams
CTO @ Healthsense
“Toptal helped us when we had a hiring gap we needed to fill. The developer we received was excellent, with a great work ethic and fit in very well with the team. We are very happy with the service and would definitely consider using Toptal again in future should we need additional help. ”
David Gildeh
CEO @ Dataloop.io
“Dario was recently assigned to our company in order to help with PHP and JW Video player refinements. He listens extremely well and offers common sense suggestions that save both time and money. He speeds up the process by asking great questions, and he just delivered our first project on time. His redesigned player offers a very pleasing appearance and delivers functions that are both elegant and intuitive.”
Dennis B. Jacobs
President @ College TV Ticket
“We had a great experience with Toptal. They paired us with the perfect developer for our application and made the process very easy. It was also easy to extend beyond the initial time frame, and we were able to keep the same contractor devoted to the project. We definitely recommend Toptal for finding high quality talent quickly and seamlessly.”
Ryan Morrissey
CTO @ Applied Business Technologies, LLC
“My experience with Toptal has exceeded expectations. I needed to quickly find a front-end developer, and the first person they recommended was a perfect fit. A month later, I needed an iOS developer and they delivered again. I'm incredibly impressed with the quality of the people in Toptal's network and how quickly I was able to find a match for my needs. Greco is an amazing web developer. He has great attention to detail, in such a way that his websites look just like the design mockups we created on Photoshop. Not only that, but his code is very organized, and he proved to be very skilled in React.js. I couldn't have been happier with his contributions to our company. Vadim is a very experienced software engineer, particular in iOS. He was able to help us build our mobile app in record speed, and also contributed to major mobile and architecture decisions. Vadim is a proactive and intelligent engineer and has impressed us from day one.”
Bruno De Martino
Head of Product @ Token
“Toptal was the best thing to happen to my web app. Initially, I hired a team of freelancers from India through another freelancing website—which was a nightmare. In fact, it was so bad that I had to start again from scratch, which is when I found Toptal. The experiences were like night and day. It was so refreshing to get partnered up with a quality developer that I could trust due to Toptal's extensive screening process. I will only use Toptal from now on and highly recommend you do the same.”
Chris Holtzhauer
CEO @ HootRecruit
“We struggled for weeks to find a developer or architect with expertise in a niche API, but to no avail. When we linked up with Toptal, they connected us with the very expert we needed, and a week later we were hard at work making progress! The whole process was very smooth and we ended up with a new feature we couldn't have done without Toptal.”
Thomas Honeyman
COO @ FindMySong
“Our experience with Toptal was very good. We had an open ended development project when we got started and the programmer from Toptal was very helpful, working with us on everything from the top level outline all the way down to development and delivery of functioning code." ”
Michael Milton
VP of Engineering @ SLANTRANGE
Hire a top CakePHP developer now.
Toptal is a marketplace for top CakePHP developers, engineers, programmers, coders, architects, and consultants. Top companies and start-ups choose Toptal freelancers for their mission critical software projects.
Full
profile
Bryce OttUnited States
With more than 13 years working as an engineer, architect, director, vice president, and chief technology officer, Bryce brings a deep understanding of enterprise software, management, and technical strategy to any project. His specialties include real-time systems, business intelligence, big data, enterprise web apps, scalability, and open-source software.
[click to continue…]
CakePHPPHPVisual BasicSQLCSSJavaScriptJavaHTML5AngularJSTwitter BootstrapYii FrameworkZend Framework 2Twitter API
Hire
Full
profile
Francisco Sanchez ClariáArgentina
Francisco is an engineer specializing in hardware-software technology integration. He also has extensive experience in innovative solutions, distributed data manipulation systems, extension of web applications to ease common business tasks, custom UI design, and application usability with a strong focus on sustainability.
[click to continue…]
CakePHPSQLHTML5PHPJavaScriptjQueryAngularUIAdobe FireworksAdobe DreamweaverAdobe FlashMySQL
Hire
Full
profile
Adewale Andrade D. AlcantaraBrazil
Adewale is a reliable and proficient developer with an interest in developing efficient web applications. He has over four years of experience with a number of programming languages and platforms, and has held positions ranging from team member to team leader.
[click to continue…]
CakePHPPHP
Hire
Full
profile
Guilherme Fernando FerreiraBrazil
Guilherme is an Agile developer with a BS in Computer Science. He is a passionate programmer with over 10 years of experience in Ruby, JavaScript, Python, PHP, Java, and more. He has spoken at conferences including Campus Party, Agile Brazil, and Agile Tour.
[click to continue…]
CakePHPPHPJavaScriptRubyRuby on RailsjQueryGit
Hire
Full
profile
Maksym PetkusUkraine
Maksym is an advanced developer, an engineer with extensive experience in web-development and systems architecture. He developed complex systems for high-load and high availability, reliable projects like payment processing. He’s an enthusiastic entrepreneur when with team lead experience.
[click to continue…]
CakePHPSQLCSSPHPJavaScriptGoogle Geocoding APIGoogle MapsMySQL
Hire
Full
profile
Ivan BernatCroatia
Ivan is a full stack web developer who has shipped over 400 front-end projects, a dozen smaller web apps, and several WordPress themes. Ivan has experience working both as a standalone freelancer for clients from around the globe and as an in-house developer in large teams.
[click to continue…]
CakePHPPHPJavaScriptNode.jsReact.jsVue.jsResponsive Web Design (RWD)
Hire
Full
profile
Jordan AmbraUnited States
Jordan is a top-notch architect, developer, sysadmin, and entrepreneur with the passion and experience to help businesses solve complex problems. He is an expert full-stack developer, bringing projects from concept to completion, with a proven track record of delivering powerful, stable, and comprehensive solutions.
[click to continue…]
CakePHPVB.NETPythonSQLJavaScriptPHPC#RubyAdobe FlexBootstrapASP.NETDjangojQuery
Hire
Full
profile
Michał TatarynowiczPoland
Michał has been a professional full-time developer for thirteen years. Before that, he worked mostly in advertising companies doing technical, print-related work, and website development. He is very passionate about code quality and how it relates to developer productivity and the company bottom line.
[click to continue…]
CakePHPPHPWeb App Development
Hire
Full
profile
Richard GarandCanada
Richard specializes in performance and scalability. He has helped to design, implement, and maintain server systems handling millions of users per month. When you need a 10x or 100x improvement in performance, he is ready to step in. Throughout his work, he aims to teach both managers and other developers exactly what's going on and how they can think about it in a more effective way. His experience primarily covers Linux, MySQL, and PHP.
[click to continue…]
CakePHPPHPDrupalMySQL
Hire
Full
profile
Michael HoughtonIreland
With twelve years of back-end development in PHP combined with a background in business, Michael brings a great balance of technical and business expertise to the table. He has experience with vanilla PHP websites as well as CakePHP, Zend, CodeIgniter, PhalconPHP, and Laravel frameworks. He's also gained project management experience, leading teams and working directly with clients to build projects from start to finish.
[click to continue…]
CakePHPHTML5PHPPayPal APIGitAgile Software DevelopmentMySQL
Hire

The Vital Guide to CakePHP Interviewing

CakePHP is an extensive framework whose feature set has continued to grow substantially since its initial release in 2005. As a result, fully mastering its capabilities can take some time, which makes finding true CakePHP experts a real challenge.

Finding them requires a highly-effective recruiting process, as described in our post on Finding and Hiring the Best in the Industry. Such a process can then be augmented with questions – such as those presented herein – to identify those candidates who have truly mastered CakePHP.

Q: How do CakePHP conventions and Object-Relational Mapping (ORM) help streamline queries? Also discuss any potential pitfalls.

CakePHP’s Object-relational mapping (ORM) benefits greatly from CakePHP conventions. By setting out the database schema to Cake’s standards, you can quickly connect tables together through Cake’s powerful ORM. You rarely need to write an SQL statement, as CakePHP handles things like table joins, hasMany, and even hasAndBelongsToMany relationships with ease.

Leveraging CakePHP’s ContainableBehavior, through your model associations you can specify which database tables and fields to select from an SQL query. This can go several tables deep, and through the ORM it is easy to rapidly construct highly complex SQL statements. It helps you search and filter data in a clean and consistent way and can also help increase the speed and overall performance of your application. (It works by temporarily or permanently altering the associations of your models, using the supplied containments to generate a corresponding series of bindModel and unbindModel calls.)

Overall, Cake’s ORM really does help streamline development and, if used correctly, is an amazing tool for building complex queries quickly. It is nonetheless vital that developers take the time to fully understand the ORM and to ensure that their queries are are properly optimized (as is true in any language).

The challenge with the ORM is that it makes using SQL so simple that, if a developer isn’t careful, he or she can write inefficient SQL queries without meaning to. These problems tend to surface after a system has been deployed, as databases grow and badly written queries become increasingly slow.

Q: What are CakePHP Helpers? List the 10 types of Helpers that are available and, using the FormHelper as an example, describe how Helpers can be used to speed up development.

CakePHP Helpers are component-like classes for the presentation layer of your application. They contain logic that can be shared by many views, elements, or layouts.

The 10 types of helpers available in CakePHP are:

  1. CacheHelper
  2. FormHelper
  3. HtmlHelper
  4. JsHelper
  5. NumberHelper
  6. Paginator
  7. RSS
  8. SessionHelper
  9. TextHelper
  10. TimeHelper

By encapsulating commonly used functionality in reusable form, CakePHP Helpers help speed up development. A great example is the FormHelper, which creates your form input fields based on your database table schema set up. For example, a TINY INT field will automatically be mapped to a checkbox, while a TEXT field will automatically be mapped to a text area.

By ensuring that the FormHelper names match the same names as in the database table, the form will automatically be created.

Using your data validation rules, CakePHP will automatically display error messages next to the form input if the data validation fails. All that is required is for the developer to match the form input to the fields in the database. With a little bit of help from the controller, the data will then automatically be saved to the database once the validation has passed successfully.

Q: What are Components and what are the benefits of using them? Provide an example of how you would access a component via a controller.

Components are logical modules that are shared between controllers. CakePHP comes with its own set of core components for common tasks and you can also create your own components. Creating and using components can help keep controller code clean and facilitates code reuse across and between projects. For example, you might want to create a custom “shopping cart” component for use across multiple controllers in an e-commerce application.

Each component you include in a controller is exposed as a property on that controller. For example, if you included the SessionComponent and the CookieComponent in your controller, you could access them as follows:

class PostsController extends AppController {
    public $components = array('Session', 'Cookie');

    public function delete() {
        if ($this->Post->delete($this->request->data('Post.id')) {
            $this->Session->setFlash('Post deleted.');
            return $this->redirect(array('action' => 'index'));
        }
    }

Q: What are Behaviors and what are their advantages? List the 4 Behaviors supported “out of the box” in CakePHP.

In much the same way that a Component extends a Controller, a Behavior extends a Model. Behaviors enable you to separate and reuse logic that performs a type of behavior, and to do so without requiring inheritance.

As an example, consider a model that provides access to a database table which stores structural information about a tree. Removing, adding, and migrating nodes in the tree is not as simple as deleting, inserting, and editing rows in the table. Rather than creating those tree-manipulation methods on a per model basis (for every model that needs that functionality), we could simply attach the TreeBehavior to our model.

The following 4 Behaviors are provided “out of the box” in CakePHP:

  1. AclBehavior: provides a way to seamlessly integrate a model with your ACL (Access Control List) system
  2. ContainableBehavior: streamlines search and filter operations
  3. TranslateBehavior: for internationalization
  4. TreeBehavior: facilitates accessing and manipulating hierarchical data in database tables

You can also create your own behaviors. Behaviors can be a great way to keep a clean code base and keep your code out of your controller. A good example is the open sourceImageUploadBehavior which allows for a very simple image or file uploading. By specifying a few rules in your model file, files will be automatically validated and uploaded, with no extra code required in your Controller file. An added plus of that particular behavior is that it uses PHP Thumb to automatically resize any image uploads.

Q: How would you handle nested data in CakePHP, such as a category tree?

Creating a category structure which goes unlimited levels deep is a good example of where the CakePHP’s TreeBehavior can come in handy.

Employing the TreeBehavior is trivially simple and is done as follows in your model file:

public $actsAs = 'Tree';

The categories table might then look something like this:

CREATE TABLE categories (
    id INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    parent_id INTEGER(10) DEFAULT NULL,
    lft INTEGER(10) DEFAULT NULL,
    rght INTEGER(10) DEFAULT NULL,
    name VARCHAR(255) DEFAULT '',
    PRIMARY KEY  (id)
);

By setting the parent_id field when you save the data, the lft and rght fields will automatically be populated. The lft and rght fields follow an MPTT (Modified Preorder Tree Traversal) structure.

All of the TreeBehavior’s methods are then available for use. Examples include:

  • generateTreeList() - returns a hierarchical array of values (e.g., for use with HTML select boxes, etc.)
  • children($parentId) - returns a list of the children of the specified $parentId
  • getPath($id) - returns an array of the nodes to traverse hierarchically within the tree to reach the specified $id

Q: How do you perform data validation in CakePHP?

CakePHP simplifies data validation, enabling you to specify in your model file the data validation rules for each of your database tables that are universal for that model. This adheres to the DRY (Don’t Repeat Yourself) principle by enabling you to just specify the rules once and then have them apply across the entire model.

The supported validation rules are plentiful in CakePHP. Here are a few good examples:

public $validate = array(
	'phone' => array(
		'rule' => 'phone',
		'message' => 'A valid phone number is required.'
	),
	'email' => array(
		'rule' => 'email',
		'message' => 'A valid email address is required.'
	),
	'password' => array(
		'rule'    => array('minLength', '8'),
		'message' => 'Minimum 8 characters long'
	),
	'dob' => array(
		'rule'    => 'date',
		'message' => 'Enter a valid date',
		'allowEmpty' => true
	)
);

Note in particular the allowEmpty key in the 'dob' array which allows the field to be empty. Based on these validation rules, the dob field can be left blank, but if a date is entered, it will be checked to confirm that it is a valid date value.

Q: Provide some examples of folder and file manipulation in CakePHP.

Often challenging with standard PHP alone, the Folder & File Utilities are useful if you need to create, upload, or manipulate folders or files.

Here are some key examples:

The Folder::copy method simplifies copying a file from one location to another:

// Copy folder1 and all its contents into folder2
$folder1 = new Folder('/path/to/folder1');
$folder1->copy('/path/to/folder2');

The Folder::copy method supports additional options as well. Specifically:

$folder = new Folder('/path/to/folder');
$folder->copy(array(
    'to' => '/path/to/new/folder',
    'from' => '/path/to/copy/from',
    'mode' => 0755,
    'skip' => array('skip-me.php', '.git'),
    'scheme' => Folder::SKIP  // Skip directories/files that already exist
));

Or to create new folders, simply use the Folder::create method:

$folder = new Folder();
if ($folder->create('foo' . DS . 'bar' . DS . 'baz' . DS . 'shoe' . DS . 'horn')) {
    // Successfully created the nested folders
}

The Folder::find method is particularly useful as it enables you to dynamically find files within a directory:

// Find all .png in your app/webroot/img/ folder and sort the results
$dir = new Folder(WWW_ROOT . 'img');
$files = $dir->find('.*\.png', true);

Q: What are some advantages of the “Fat Model, Skinny Controller” approach? Provide an example of how you would use it in CakePHP.

“Fat Model, Skinny Controller” - often advocated by Ruby on Rails developers - is an approach within the Model/View/Controller (MVC) architectural paradigm whereby logic should predominantly exist within the model. This relegates the “skinny” controller to its intended role as a controlling interface between the view and model.

Consider, for example, performing a simple CRUD (create, read, update and delete) operation, such as adding posts to a blog. The default add method might be as follows:

public function add() {
    if ($this->request->is('post')) {
        $this->Post->create();
        if ($this->Post->save($this->request->data)) {
            $this->Session->setFlash(__('Your post has been saved.'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Unable to add your post.'));
    }
}

This controller action is fine for a simple add, but what would happen if you wanted to do things such as send an email to the admin when a post was added, or update another model association when a post was added? This is additional logic, but this logic shouldn’t go in the controller file.

Instead we would write a method for this in our Post.php model, perhaps something like this:

public function addPost($data = array(), $emailAdmin = true) {
    $this->create();
    $this->save($data);

    // update any other tables

    if ($emailAdmin) {
    	// send the email to the admin user
    }
 
    // if all is successful
    return true;
}

This would then only require a small change to the controller action as follows:

public function add() {
    if ($this->request->is('post')) {
        if ($this->Post->addPost($this->request->data)) {
            $this->Session->setFlash(__('Your post has been saved.'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Unable to add your post.'));
    }
}

As you can see, the new action is actually one less line, because the $this->Post->create() has been moved to the model file, helping achieve clean and concise code.

Q: How does CakePHP handle authentication and user login? Provide a code example.

CakePHP has a built in authentication component (AuthComponent) that makes setting up a user registration and login system very straightforward. By setting up a users table with a username or email field and a password field, a programmer can quickly incorporate authentication into their application. CakePHP also handles password encryption, providing several different classes for encryption including B crypt and digest authentication. CakePHP also has some advanced methods for doing things such as logging users in automatically.

Here’s how you can easily take advantage of these capabilities in your code:

Your database table might look something like this:

CREATE TABLE users (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50),
    password VARCHAR(255),
    role VARCHAR(20),
    created DATETIME DEFAULT NULL,
    modified DATETIME DEFAULT NULL
);

In your AppController.php you would then setup the component:

// app/Controller/AppController.php
class AppController extends Controller {

    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array(
                'controller' => 'posts',
                'action' => 'index'
            ),
            'logoutRedirect' => array(
                'controller' => 'pages',
                'action' => 'display',
                'home'
            ),
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'Blowfish'
                )
            )
        )
    );

In your User.php model file, you would set the password encryption code in the beforeSave callback:

// app/Model/User.php

App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

// ...

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $passwordHasher = new BlowfishPasswordHasher();
        $this->data[$this->alias]['password'] = $passwordHasher->hash(
            $this->data[$this->alias]['password']
        );
    }
    return true;
}

Then, in your UsersController.php, you can set the login action:

// app/Controller/UsersController.php

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}

Note how simple it is. The code simply looks for a post, and then calls the Auth->login component method which logs the user in.

The view login file might then look something like this:

//app/View/Users/login.ctp

<div class="users form">
<?php echo $this->Form->create('User'); ?>
    <fieldset>
        <legend>
            <?php echo __('Please enter your username and password'); ?>
        </legend>
        <?php echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>

Just set the form with the username and password fields, and CakePHP handles the rest.

Finally, for the logout script, in your UsersController the logout action could look something like this:

public function logout() {
    return $this->redirect($this->Auth->logout());
}

Q: Provide an example of how you would use CakePHP’s callbacks.

CakePHP callbacks enable you to manipulate or check data before a model operation. Examples include before validation, before save, after save, before delete, after delete, and after find.

For example, consider a case where you want to manipulate a date so that it is displayed differently than the way it is saved in the database. (Perhaps you are working with an older database that saved the date in a time() format or in a non standard database date format.)

To accomplish this, these callbacks could go in your model file:

public function afterFind($results, $primary = false) {
    foreach ($results as $key => $val) {
        if (isset($val['Event']['begindate'])) {
            $results[$key]['Event']['begindate'] = $this->dateFormatAfterFind(
                $val['Event']['begindate']
            );
        }
    }
    return $results;
}

public function dateFormatAfterFind($dateString) {
    return date('d-m-Y', strtotime($dateString));
}

The afterFind callback will take the data returned from a find query, and change the format of the date. In this example, we set the date to be in the d-m-Y format – perhaps preparing the data for going into a date picker or something similar. This code will be called before the data is returned to the controller, so it allows us to manipulate data before we receive it in our controller.

We would also need to have a callback before we save the data, to revert the date format back to that used in the database:

public function beforeSave($options = array()) {
    if (!empty($this->data['Event']['begindate'])) {
        $this->data['Event']['begindate'] = $this->dateFormatBeforeSave(
            $this->data['Event']['begindate']
        );
    }
	// Be sure to return true, or your save is going to fail!
    return true;
}

public function dateFormatBeforeSave($dateString) {
    return date('Y-m-d', strtotime($dateString));
}

Q: What are Virtual Fields in CakePHP? How and why would you use them. Provide an example.

Virtual Fields allow you to create arbitrary SQL expressions and assign them as fields in a model. These fields cannot be saved, but will be treated like other model fields for read operations. They will be indexed under the model’s key alongside other model fields.

As a simple example, consider a model that contains first_name and last_name fields. You might find that you often want to use the user’s full name. In this case, in your model file you can add:

public $virtualFields = array(
    'full_name' => 'CONCAT(User.first_name, " ", User.last_name)'
);

This will add a new field called full_name. When doing a find query, the data would then show as follows:

Array
(
    [0] => Array
        (
            [User] => Array
                (
                    [id] => 1
                    [fist_name] => John
                    [last_name] => Smith
                    [full_name] => John Smith
                )
        )
)

Another good use of virtual fields is when you need to count data. Let’s take the example when an article has comments. Often we need to count how many comments an article has. This is simple to do with virtual fields. For example, in your model file, you could add the following:

public $virtualFields = array(
	'num_comments' => 'SELECT COUNT(id) FROM comments WHERE article_id = Article.id'
);

This will add num_comments at the end of any Article find query, e.g:

Array
(
    [0] => Array
        (
            [Article] => Array
                (
                    [id] => 1
                    [title] => Test Article
                    [description] => Test Description
                    [num_comments] => 2
                )
        )
)

Virtual fields do not come without any penalty though. Their downside is performance. This should be kept in mind when creating virtual fields, as the more complex your virtual field, the more impact on query performance the virtual field will have.

Wrap-up

The questions presented in this guide can be highly effective in evaluating the breadth and depth of a developer’s knowledge of the CakePHP framework. It is important to bear in mind, though, that these questions are intended merely as a guide. Not every “A” candidate worth hiring will be able to properly answer them all, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains as much of an art as it does a science.

Hire CakePHP developers now
Alvaro 1506e7

My team is going to personally help you find the best candidate to join your team.

Alvaro Oliveira
VP of Talent Operations