You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Due to historical choices and technical limitations, we have some exceptions to these guidelines in Winter.
12
12
13
-
#### Controller methods can have a single underscore
13
+
#### Controller action and AJAX handler names
14
14
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:
16
18
17
19
```php
18
20
public function index()
19
21
{
20
-
// This is the index page (index action)
22
+
// This is the index page, at /author/plugin/controller/index
21
23
}
22
24
23
-
public function index_onDoSomething()
25
+
public function my_action()
24
26
{
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
26
33
}
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.
27
37
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
28
41
public function onDoSomethingElse()
29
42
{
30
43
// AJAX handler works globally for all actions
31
44
}
45
+
46
+
public function index_onDoSomething()
47
+
{
48
+
// AJAX handler only works on the index action
49
+
}
32
50
```
33
51
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.
Copy file name to clipboardExpand all lines: backend/controllers-ajax.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,7 @@ Property | Description
57
57
58
58
## Actions, views and routing
59
59
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:
61
61
62
62
```html
63
63
<h1>Hello World</h1>
@@ -75,6 +75,10 @@ The above Controller results in the following:
75
75
https://example.com/backend/acme/blog/users/index
76
76
```
77
77
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
+
78
82
## Passing data to views
79
83
80
84
Use the controller's `$vars` property to pass any data directly to your view:
0 commit comments