How to create and use a Behavior in CakePHP3?

Please, how to a create and use a simple behavior in CakePHP3.
I searched a lot through google, but I have not found it yet.

1 Like

Have you read the manual?

No. In the manual I don’t understand. I create and use a component, a plugin, a helper, a element, but behavior not.

Specifically what part are you struggling with? It tells you where to put the file, how to name the class and file, what class to extend, etc., and gives code examples for all of it.

I create the behavior with my metodo, but I dont know yours specifics methots to use in it. I load in table but dont know how to call the behavior in a table or in a controller.

In manual, Cake 2 behavior have methods but to Cake 3 I dont see and dont undertand this methos. Are optionals, requireds?

Behavior methods are loaded directly onto the model. So you don’t explicitly call the behavior. You just call the method in the behavior like this:

ArticlesTable.php

public function test()
{
    # This method is defined in the behavior.
    $this->myMethod();
}

MyBehavior.php

# This method is available directly on all models which implement the behavior.
public function myMethod()
{
    return true;
}

Is this what you are struggling with?

1 Like

Works fine.
Thank you, very mutch.