Skip to content

Commit c291d90

Browse files
committed
Update to naming conventions for controller action methods
Refs: wintercms/winter@353b238
1 parent 55007a8 commit c291d90

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

architecture/developer-guide.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,46 @@ Winter follows the [PSR-2 Coding Style Guide](https://www.php-fig.org/psr/psr-2/
1010

1111
Due to historical choices and technical limitations, we have some exceptions to these guidelines in Winter.
1212

13-
#### Controller methods can have a single underscore
13+
#### Controller action and AJAX handler names
1414

15-
The PSR-2 guidelines state that methods must be in **camelCase** format. However, in [Backend controllers](../backend/controllers-ajax.md) in Winter, AJAX handlers can be created using a suffix notation if they are connected to a "main" action. For example:
15+
The PSR-2 guidelines state that methods must be in **camelCase** format. [Backend controllers](../backend/controllers-ajax.md) in Winter deviate from this in two ways, and an exception must be granted for each.
16+
17+
Firstly, methods that are reachable as a URL — *actions* — must be named entirely in **lowercase**. Actions made up of more than one word use **snake_case**, or run the words together:
1618

1719
```php
1820
public function index()
1921
{
20-
// This is the index page (index action)
22+
// This is the index page, at /author/plugin/controller/index
2123
}
2224

23-
public function index_onDoSomething()
25+
public function my_action()
2426
{
25-
// AJAX handler only works on the index action
27+
// Reachable at /author/plugin/controller/my-action
28+
}
29+
30+
public function myaccount()
31+
{
32+
// Reachable at /author/plugin/controller/myaccount
2633
}
34+
```
35+
36+
URL segments containing dashes are normalised to snake_case, so `/my-action` resolves to `my_action()`. A camelCase method such as `myAction()` is not reachable as an action at all.
2737

38+
Secondly, AJAX handlers are named with an `on` prefix in camelCase, and may carry an action prefix separated by a single underscore if they are connected to a "main" action:
39+
40+
```php
2841
public function onDoSomethingElse()
2942
{
3043
// AJAX handler works globally for all actions
3144
}
45+
46+
public function index_onDoSomething()
47+
{
48+
// AJAX handler only works on the index action
49+
}
3250
```
3351

34-
An exception must be granted for this scenario. In other scenarios, underscores should be avoided in method names.
52+
Handler names are deliberately excluded from URL dispatch. As of v1.2.14 only lowercase method names are routable as actions, which prevents an AJAX handler being triggered by a plain link rather than by a proper AJAX request. In other scenarios, underscores should be avoided in method names.
3553

3654
#### Curly braces for condition blocks
3755

backend/controllers-ajax.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Property | Description
5757

5858
## Actions, views and routing
5959

60-
Public controller methods, called **actions** are coupled to **view files** which represent the page corresponding the action. Backend view files use PHP syntax. Example of the **index.php** view file contents, corresponding to the **index** action method:
60+
Public controller methods with lowercase names, called **actions**, are coupled to **view files** which represent the page corresponding the action. Backend view files use PHP syntax. Example of the **index.php** view file contents, corresponding to the **index** action method:
6161

6262
```html
6363
<h1>Hello World</h1>
@@ -75,6 +75,10 @@ The above Controller results in the following:
7575
https://example.com/backend/acme/blog/users/index
7676
```
7777

78+
> **NOTE:** Action methods must be named entirely in **lowercase**. Actions made up of more than one word use **snake_case** (`my_action`), or run the words together (`myaccount`). Dashes in the URL are normalised to snake_case, so `/my-action` resolves to the `my_action()` method. A camelCase method such as `myAction()` is not reachable as an action.
79+
>
80+
> This is what keeps [AJAX handlers](#using-ajax-handlers) — which are named `onDoSomething` or `index_onDoSomething` — from being reachable as URLs, so that they can only be triggered by a proper AJAX request. Prior to v1.2.14, any public controller method was reachable as an action.
81+
7882
## Passing data to views
7983

8084
Use the controller's `$vars` property to pass any data directly to your view:

0 commit comments

Comments
 (0)