@@ -96,62 +96,74 @@ Public methods on behaviors can be called as normal methods:
9696$articles->getBehavior('Sluggable')->slug($value);
9797```
9898
99- Public methods defined on behaviors are also added as 'mixin' methods on the
100- table object they are attached to. If you attach two behaviors that provide the
101- same methods an exception will be raised. If a behavior provides the same method
102- as a table class, the behavior method will not be callable from the table.
103- Behavior mixin methods will receive the exact same arguments that are provided
104- to the table. For example, if our SluggableBehavior defined the following
105- method:
99+ Public methods defined on behaviors can also be exposed as 'mixin' methods on
100+ the table object they are attached to, so they can be called directly on the
101+ table. For example, if our SluggableBehavior defined the following method:
106102
107103``` php
108104public function slug(string $value): string
109105{
110- return Text::slug($value, $this->_config[ 'replacement'] );
106+ return Text::slug($value, $this->getConfig( 'replacement') );
111107}
112108```
113109
114- It could be invoked using:
110+ ::: info Changed in version 6.0
111+ Exposing behavior methods on the table instance is opt-in. In 5.x all public
112+ behavior methods were proxied by default. In 6.0 nothing is proxied until you
113+ set the ` implementedMethods ` configuration key when attaching the behavior.
114+ :::
115+
116+ To proxy all of a behavior's public methods, set ` implementedMethods ` to
117+ ` true ` when adding the behavior:
118+
119+ ``` php
120+ // In a table's initialize() method.
121+ $this->addBehavior('Sluggable', [
122+ 'implementedMethods' => true,
123+ ]);
124+ ```
125+
126+ The method could then be invoked using:
115127
116128``` php
117129$slug = $articles->slug('My article');
118130```
119131
120- ::: info Deprecated in version 5.3.0
121- Calling behavior methods as mixin methods is deprecated
122- :::
132+ If you attach two behaviors that provide the same method an
133+ exception will be raised. If a behavior provides the same method as a table
134+ class, the behavior method will not be callable from the table.
123135
124136#### Limiting or Renaming Exposed Mixin Methods
125137
126- When creating behaviors, there may be situations where you don't want to expose
127- public methods as mixin methods. In these cases you can use the
128- ` implementedMethods ` configuration key to rename or exclude mixin methods. For
129- example if we wanted to prefix our slug() method we could do the following:
138+ When creating behaviors, there may be situations where you don't want to
139+ expose all public methods as mixin methods. Instead of setting
140+ ` implementedMethods ` to ` true ` , you can provide an array that maps the mixin
141+ method name to the behavior method it proxies. Providing an array both
142+ enables proxying and decides which methods are exposed. For example if we
143+ wanted to prefix our slug() method we could do the following when adding the
144+ behavior to a table:
130145
131146``` php
132- protected array $_defaultConfig = [
147+ // In a table's initialize() method.
148+ $this->addBehavior('Sluggable', [
133149 'implementedMethods' => [
134150 'superSlug' => 'slug',
135151 ]
136- ];
152+ ]) ;
137153```
138154
139155Applying this configuration will make ` slug() ` not callable, however it will
140156add a ` superSlug() ` mixin method to the table. Notably if our behavior
141157implemented other public methods they would ** not** be available as mixin
142158methods with the above configuration.
143159
144- Since the exposed methods are decided by configuration you can also
145- rename/remove mixin methods when adding a behavior to a table. For example:
160+ The array can also be a numerically indexed list of method names, in which
161+ case each method is proxied under its own name, so
162+ ` 'implementedMethods' => ['slug'] ` is equivalent to
163+ ` 'implementedMethods' => ['slug' => 'slug'] ` .
146164
147- ``` php
148- // In a table's initialize() method.
149- $this->addBehavior('Sluggable', [
150- 'implementedMethods' => [
151- 'superSlug' => 'slug',
152- ]
153- ]);
154- ```
165+ Method names are matched case-insensitively, and setting
166+ ` implementedMethods ` to an empty array disables mixin methods entirely.
155167
156168### Defining Event Listeners
157169
@@ -173,7 +185,7 @@ use Cake\Utility\Text;
173185
174186class SluggableBehavior extends Behavior
175187{
176- protected array $_defaultConfig = [
188+ protected array $defaultConfig = [
177189 'field' => 'title',
178190 'slug' => 'slug',
179191 'replacement' => '-',
@@ -247,7 +259,7 @@ or exclude finder methods. For example if we wanted to rename our `find(slug)`
247259method we could do the following:
248260
249261``` php
250- protected array $_defaultConfig = [
262+ protected array $defaultConfig = [
251263 'implementedFinders' => [
252264 'slugged' => 'findSlug',
253265 ]
@@ -317,6 +329,10 @@ $table->behaviors()->has('CounterCache');
317329// Get a loaded behavior
318330// Remember to omit plugin prefixes
319331$table->behaviors()->get('CounterCache');
332+
333+ // Check if any loaded behavior exposes a mixin method.
334+ // Only methods enabled through the `implementedMethods` config match.
335+ $table->behaviors()->hasMethod('slug');
320336```
321337
322338### Re-configuring Loaded Behaviors
0 commit comments