Свежайшие Пирожки от CakePHP по-русски

Полнейшее руководство CakePHP 1.2 на русском языке, горячие новости и полезные статьи

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:

Простой текст
  1. <?php
  2. /* /app/views/helpers/link.php */
  3. class LinkHelper extends AppHelper {
  4. function makeEdit($title, $url) {
  5. // Logic to create specially formatted link goes here...
  6. }
  7. }
  8. ?>

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.

Простой текст
  1. <?php
  2. function makeEdit($title, $url) {
  3. // Use the helper's output function to hand formatted
  4. // data back to the view:
  5. return $this->output(
  6. "<div class=\"editOuter\">
  7. <a href=\"$url\" class=\"edit\">$title</a>
  8. </div>"
  9. );
  10. }
  11. ?>