Skip to content

Commit b97a35b

Browse files
committed
Adds the ability to declare 'authenticated' in controller doc block
1 parent 655e40f commit b97a35b

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

docs/documenting.md

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ They will be included in the generated documentation text and example requests.
9494

9595
**Result:**
9696

97-
![](body_params_1.png)
97+
![](./../body_params_1.png)
9898

99-
![](body_params_2.png)
99+
![](./../body_params_2.png)
100100

101101
### Example parameters
102102
For each parameter in your request, this package will generate a random value to be used in the example requests. If you'd like to specify an example value, you can do so by adding `Example: your-example` to the end of your description. For instance:
@@ -114,11 +114,11 @@ For each parameter in your request, this package will generate a random value to
114114

115115
You can also exclude a particular parameter from the generated examples (for all languages) by annotating it with `No-example`. For instance:
116116
```php
117-
/**
118-
* @queryParam location_id required The id of the location. Example: 1
119-
* @queryParam user_id required The id of the user. No-example
120-
* @queryParam page required The page number. Example: 4
121-
*/
117+
/**
118+
* @queryParam location_id required The id of the location. Example: 1
119+
* @queryParam user_id required The id of the user. No-example
120+
* @queryParam page required The page number. Example: 4
121+
*/
122122
```
123123
Outputs:
124124
```bash
@@ -151,6 +151,41 @@ public function createPost(MyRequest $request)
151151
## Indicating authentication status
152152
You can use the `@authenticated` annotation on a method to indicate if the endpoint is authenticated. A "Requires authentication" badge will be added to that route in the generated documentation.
153153

154+
Just like `@group` annotation, you can also specify an `@authenticated` on a single method to override the authenticated status defined at the controller level.
155+
156+
```php
157+
/**
158+
* @authenticated
159+
*
160+
* APIs for managing users
161+
*/
162+
class UserController extends Controller
163+
{
164+
165+
/**
166+
* Create a user
167+
*
168+
* [Insert optional longer description of the API endpoint here.]
169+
*
170+
*/
171+
public function createUser()
172+
{
173+
174+
}
175+
176+
/**
177+
* @group Account management
178+
*
179+
*/
180+
public function changePassword()
181+
{
182+
183+
}
184+
}
185+
```
186+
187+
Now all the methods under this controller will have "Requires authentication" badge enabled.
188+
154189
## Providing an example response
155190
You can provide an example response for a route. This will be displayed in the examples section. There are several ways of doing this.
156191

src/Extracting/Strategies/Metadata/GetFromDocBlocks.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ public function __invoke(Route $route, ReflectionClass $controller, ReflectionMe
1717
$docBlocks = RouteDocBlocker::getDocBlocksFromRoute($route);
1818
/** @var DocBlock $methodDocBlock */
1919
$methodDocBlock = $docBlocks['method'];
20+
$classDocBlock = $docBlocks['class'];
2021

21-
list($routeGroupName, $routeGroupDescription, $routeTitle) = $this->getRouteGroupDescriptionAndTitle($methodDocBlock, $docBlocks['class']);
22+
list($routeGroupName, $routeGroupDescription, $routeTitle) = $this->getRouteGroupDescriptionAndTitle($methodDocBlock, $classDocBlock);
2223

2324
return [
2425
'groupName' => $routeGroupName,
2526
'groupDescription' => $routeGroupDescription,
2627
'title' => $routeTitle ?: $methodDocBlock->getShortDescription(),
2728
'description' => $methodDocBlock->getLongDescription()->getContents(),
28-
'authenticated' => $this->getAuthStatusFromDocBlock($methodDocBlock->getTags()),
29+
'authenticated' => $this->getAuthStatusFromDocBlock($classDocBlock->getTags())?:$this->getAuthStatusFromDocBlock($methodDocBlock->getTags()),
2930
];
3031
}
3132

@@ -48,7 +49,7 @@ protected function getAuthStatusFromDocBlock(array $tags)
4849
* @param DocBlock $methodDocBlock
4950
* @param DocBlock $controllerDocBlock
5051
*
51-
* @return array The route group name, the group description, ad the route title
52+
* @return array The route group name, the group description, and the route title
5253
*/
5354
protected function getRouteGroupDescriptionAndTitle(DocBlock $methodDocBlock, DocBlock $controllerDocBlock)
5455
{

0 commit comments

Comments
 (0)