Creating Helpers
If a core helper (or one showcased on Cakeforge or the Bakery) doesn’t fit your needs, helpers are easy to create.
Let's say we wanted to create a helper that could be used to output a specifically crafted CSS-styled link you needed many different places in your application. In order to fit your logic in to CakePHP's existing helper structure, you'll need to create a new class in /app/views/helpers. Let's call our helper LinkHelper. The actual PHP class file would look something like this:
Простой текст<?php/* /app/views/helpers/link.php */class LinkHelper extends AppHelper {function makeEdit($title, $url) {// Logic to create specially formatted link goes here...}}?>
There are a few methods included in CakePHP's Helper class you might want to take advantage of:
output(string $string)
Use this function to hand any data back to your view.
Простой текст<?phpfunction makeEdit($title, $url) {// Use the helper's output function to hand formatted// data back to the view:return $this->output("<div class=\"editOuter\"><a href=\"$url\" class=\"edit\">$title</a></div>");}?>


Коментарии:
Добавить коментарий