Skip to content

Commit 7af5f1b

Browse files
committed
Use RouteArgument
1 parent f9415d5 commit 7af5f1b

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

guide/en/start/hello.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,30 @@ namespace App\Controller\Echo;
3131
use Psr\Http\Message\ResponseFactoryInterface;
3232
use Psr\Http\Message\ResponseInterface;
3333
use Yiisoft\Html\Html;
34-
use Yiisoft\Router\CurrentRoute;
34+
use Yiisoft\Router\HydratorAttribute\RouteArgument;
3535

3636
final readonly class Action
3737
{
3838
public function __construct(
3939
private ResponseFactoryInterface $responseFactory,
4040
) {}
4141

42-
public function __invoke(CurrentRoute $currentRoute): ResponseInterface
42+
#[RouteArgument('message')]
43+
public function __invoke(string $message = 'Hello!'): ResponseInterface
4344
{
44-
$message = $currentRoute->getArgument('message', 'Hello!');
45-
4645
$response = $this->responseFactory->createResponse();
4746
$response->getBody()->write('The message is: ' . Html::encode($message));
4847
return $response;
4948
}
5049
}
5150
```
5251

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`,
5554
the action assigns the value "Goodbye" to the `$message` variable.
5655

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.
5958

6059
## Configuring router
6160

@@ -82,7 +81,10 @@ return [
8281
];
8382
```
8483

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.
8688

8789
You also give a `echo/say` name to this route to be able to generate URLs pointing to it.
8890

@@ -97,8 +99,7 @@ If you omit the `message` parameter in the URL, the page displays "The message i
9799
## Creating a View Template <span id="creating-view-template"></span>
98100

99101
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.
102103

103104
For the "Hello" task, create a `src/Controller/Echo/template.php` template that prints the `message` parameter received
104105
from the action method:
@@ -129,7 +130,7 @@ declare(strict_types=1);
129130
namespace App\Controller\Echo;
130131

131132
use Psr\Http\Message\ResponseInterface;
132-
use Yiisoft\Router\CurrentRoute;
133+
use Yiisoft\Router\HydratorAttribute\RouteArgument;
133134
use Yiisoft\Yii\View\Renderer\ViewRenderer;
134135

135136
final readonly class Action
@@ -138,18 +139,17 @@ final readonly class Action
138139
private ViewRenderer $viewRenderer,
139140
) {}
140141

141-
public function __invoke(CurrentRoute $currentRoute): ResponseInterface
142+
#[RouteArgument('message')]
143+
public function __invoke(string $message = 'Hello!'): ResponseInterface
142144
{
143-
$message = $currentRoute->getArgument('message', 'Hello!');
144-
145145
return $this->viewRenderer->render(__DIR__ . '/template', [
146146
'message' => $message,
147147
]);
148148
}
149149
}
150150
```
151151

152-
Now open your browser and check it again. You should see similar text but with a layout applied.
152+
Now open your browser and check it again. You should see the similar text but with a layout applied.
153153

154154
Also, you've separated the part about how it works and part of how it's presented. In the larger applications,
155155
it helps a lot to deal with complexity.

0 commit comments

Comments
 (0)