In these days I thought on how to work better with AppHelpers in CakePHP. I thought to use the AppHelper to make links and other html elements consistent depending by the context i need, for example, for users I have the method
$this->AppUser->profile($data, $options, $attributes);
this method returns a link styled for the users, with a specific css classes, maybe something line this:
<a class="user female" href="http://url/profiles/username">Username</a>
My problem is the data is structured differently by the situation, in some case I have an array like this:
$data['User']['id']
$data['User']['username']
$data['Profile']['user_id']
$data['Profile']['sex']
$data['Profile']['other']
And in some other cases, with different queries and different entities I have this:
$data['User']['id']
$data['User']['username']
$data['User']['Profile']['user_id']
$data['User']['Profile']['sex']
$data['User']['Profile']['other']
So I would like to understand if I missing something in the data hierarchy because it should be always structured in the same way?
And so should I to send data to the Helper always structured in the same way?
Should I let the helper parse the data depending by the situation, so with conditions to find where the data is?
That’s pretty common, and is a result of finding related items multiple levels deep. I usually have a helper method on the Helper that normalizes the data.
I would always send the data to the helper as-is, and then restructure it as needed within the helper. It would look something like this:
Now your functions can always expect the Profile data on the same level as the User key. This function isn’t perfect and isn’t recursive, but should give you a good start.