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
$response->getBody()->write('The message is: ' . Html::encode($message));
48
47
return $response;
49
48
}
50
49
}
51
50
```
52
51
53
-
In your example, the `__invoke` method receives the `$currentRoute` parameter that you can use to get
54
-
a message, whose value defaults to `"Hello"`. If the request is made to `/say/Goodbye`,
52
+
In your example, the `__invoke` method receives the `$message` parameter that with the help of `RouteArgument` attribute
53
+
gets the message from URL. The value defaults to `"Hello!"`. If the request is made to `/say/Goodbye`,
55
54
the action assigns the value "Goodbye" to the `$message` variable.
56
55
57
-
The application passes the response through the [middleware stack](../structure/middleware.md) to the emitter that outputs the response
58
-
to the end user.
56
+
The application passes the response through the [middleware stack](../structure/middleware.md) to the emitter that
57
+
outputs the response to the end user.
59
58
60
59
## Configuring router
61
60
@@ -82,7 +81,10 @@ return [
82
81
];
83
82
```
84
83
85
-
In the above, you map the `/say[/{message}]` pattern to `\App\Controller\Echo\Action`. For a request, the router creates an instance and calls the `__invoke()` method. The `{message}` part of the pattern writes anything specified in this place to the `message` request attribute. `[]` marks this part of the pattern as optional.
84
+
In the above, you map the `/say[/{message}]` pattern to `\App\Controller\Echo\Action`.
85
+
For a request, the router creates an instance and calls the `__invoke()` method.
86
+
The `{message}` part of the pattern writes anything specified in this place to the `message` request attribute.
87
+
`[]` marks this part of the pattern as optional.
86
88
87
89
You also give a `echo/say` name to this route to be able to generate URLs pointing to it.
88
90
@@ -97,8 +99,7 @@ If you omit the `message` parameter in the URL, the page displays "The message i
97
99
## Creating a View Template <spanid="creating-view-template"></span>
98
100
99
101
Usually, the task is more complicated than printing out "hello world" and involves rendering some complex
100
-
HTML. For this task, it's handy to use [views templates](../structure/view.md). They're scripts you
101
-
write to generate a response's body.
102
+
HTML. For this task, it's handy to use view templates. They're scripts you write to generate a response's body.
102
103
103
104
For the "Hello" task, create a `src/Controller/Echo/template.php` template that prints the `message` parameter received
104
105
from the action method:
@@ -129,7 +130,7 @@ declare(strict_types=1);
129
130
namespace App\Controller\Echo;
130
131
131
132
use Psr\Http\Message\ResponseInterface;
132
-
use Yiisoft\Router\CurrentRoute;
133
+
use Yiisoft\Router\HydratorAttribute\RouteArgument;
133
134
use Yiisoft\Yii\View\Renderer\ViewRenderer;
134
135
135
136
final readonly class Action
@@ -138,18 +139,17 @@ final readonly class Action
138
139
private ViewRenderer $viewRenderer,
139
140
) {}
140
141
141
-
public function __invoke(CurrentRoute $currentRoute): ResponseInterface
142
+
#[RouteArgument('message')]
143
+
public function __invoke(string $message = 'Hello!'): ResponseInterface
0 commit comments