I’m playing with CakePHP. I’m looking for a framework for a new web project of mine. Frankly, I’d like CakePHP to be it. There is much to love about CakePHP. It has a very wide range of features and broad support. Unfortunately, I’m finding CakePHP to require much more coding effort than I had anticipated.
The user support has made up for my tarnished expectations. And, as it has been pointed out to me, my current experiences are based on “out-of-the-box” CakePHP. Perhaps plug-ins will help.
A lot of developers love Cake! As such, I can only assume that I am missing something. For this reason, I will press on; hoping that I will find the plugins or that something will just click and suddenly make sense. But right now, it’s a journey.
The Sample Case
I’m working through the User Authentication Tutorial. In this example, I’ve created a very basic users table with an admittedly simple model. Here it is in its entirety:
class User extends AppModel {
public $name = 'User';
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
)
);
}
Nitpick #1 – You Have OOP, Use it!
I understand the simplicity of using an array for a type of configuration. But I would have much preferred an object oriented approach here… And unfortunately, this isn’t the first time I’ve had this complaint.
This could have been much more powerful, extensible – and even required much less code, had we done something as simple as used an array of Constraint objects! Simple things should be simple – and that’s the beauty of OOP.
Yes! I’m nitpicking. (Uh, that’s the point of this post.) But how common is it to have a required field? Do I really need to code the “The {field label} field is required” message every time? A better approach would be:
class User extends AppModel {
public $name = 'User';
public $validate = array(
'username' => array(new RequiredConstraint(‘username’)),
'password' => array(new RequiredConstraint(‘password’)),
'role' => array(new EnumerationConstraint(‘role’, array('admin', 'author')))
);
} ?>
I’d probably even refactor the $validate array and ultimately change AppModel to use a constructor that created a list of Column or Field objects that, in turn had a list of constraints… But I digress…
Nitpick #2 – You Have a Model, Use It!
Back to the model… Without knowing anything about CakePHP, it’s pretty easy to see that the role column is (essentially) an enumeration. It’s value must be either admin or author. I love it when code is self describing!
So, since the model tells me this; why must I express it again in the view? Looking at a snippet of the add.ctp, this is what I find:
<!-- app/View/Users/add.ctp -->
<?php
echo $this->Form->input('role', array(
'options' => array('admin' => 'Admin', 'author' => 'Author')
));
?>
Again, simple things should be simple! Yes. We certainly need the ability to override the list of presented options. Maybe we don’t want all the options available in a particular view. But the view options are commonly the same.
This is also an example where we should be saving much more than development time and initial effort. How many views are going to eventually have this input field? How many are going to need to be changed every time I update the list of options? How often am I going to fat-finger one of the options? How easy is that bug going to be to find?
Parting Disclaimer
I am not a CakePHP hater! In fact, I totally expect to grow to love CakePHP. I am a complete neophyte in the CakePHP world and expect to find the genius in the design far more frequently than I find warts to criticize. I have not even scratched the surface of CakePHP and its many plug-ins and may find simple solutions already exist.
CakePHP is the way! At least, I still believe it is. I have much to learn on this new journey…