Installation:
composer require cm2-tech/swagger-bundle:^1.0
Symfony Flex auto-enables the bundle. If not, add to config/bundles.php:
CM2\SwaggerBundle\CM2SwaggerBundle::class => ['all' => true],
Configure .env:
SWAGGER_TOKEN=your_secure_token_here
(Critical: Protects /_swagger endpoint.)
Update services.yaml:
services:
CM2\SwaggerBundle\Controller\SwaggerController:
arguments:
$sources:
- '%kernel.project_dir%/src/Controller'
- '%kernel.project_dir%/src/Entity'
tags: ['controller.service_arguments']
First Use Case:
Access /_swagger?token=your_secure_token_here to generate swagger.json from annotated controllers/entities.
zircote/swagger-php annotations (e.g., @OA\Info, @OA\Tag, @OA\PathItem).$sources match your project structure./_profiler) for annotation parsing errors.use OpenApi\Annotations as OA;
/**
path="/api/users",
@OA\Response(response="200", description="List of users")
Generate & Serve:
/_swagger?token=... to auto-generate swagger.json.# config/packages/swagger_ui.yaml (if using symfony/swagger-ui-bundle)
swagger_ui:
urls:
- { url: '/_swagger', name: 'My API' }
Extend with Entities:
/**
* @OA\Schema(
* schema="User",
* @OA\Property(property="name", type="string")
* )
*/
#[ORM\Entity]
class User { ... }
swagger.json in production (e.g., via Symfony’s HTTP cache or a reverse proxy).
# config/packages/framework.yaml
framework:
http_cache:
cache_control:
rules:
- path: ^/_swagger
max_age: 3600
ParameterBag to fetch SWAGGER_TOKEN securely:
$token = $this->getParameter('swagger_token');
composer require nelmio/api-doc-bundle
Token Mismatch:
403 Forbidden if SWAGGER_TOKEN in .env doesn’t match the request..env and URL params (?_swagger?token=...).Annotation Parsing Failures:
swagger.json or errors in Symfony logs.zircote/swagger-php annotations are correctly imported.src/Controller and src/Entity paths in services.yaml.php bin/console debug:container CM2\SwaggerBundle\Controller\SwaggerController to validate service config.PHP Version:
PHP 8.1 required (or higher). Use:
php -v # Verify version
composer require php:^8.1
SwaggerController to inspect parsed annotations:
public function index(Swagger $swagger, string $token): Response
{
file_put_contents(
'var/log/swagger_annotations.log',
print_r($swagger->toArray(), true)
);
// ...
}
_profiler to validate parsed metadata.Custom Sources:
Extend $sources in services.yaml to include:
$sources:
- '%kernel.project_dir%/src/DTO'
- '%kernel.project_dir%/config/swagger'
Post-Processing:
Override SwaggerController to modify the OpenAPI object before JSON serialization:
use CM2\SwaggerBundle\Controller\SwaggerController as BaseController;
class CustomSwaggerController extends BaseController
{
public function index(Swagger $swagger, string $token): Response
{
$swagger->info->title = 'Custom API Title';
return parent::index($swagger, $token);
}
}
Update services.yaml to use your custom controller.
Security:
/_swagger to specific IPs or roles:
# config/packages/security.yaml
access_control:
- { path: ^/_swagger, roles: ROLE_SWAGGER }
Voter for dynamic token validation.SWAGGER_TOKEN in .env matches the case in the request (e.g., ?token=AbC123 vs .env value).$sources (e.g., */src/**) to prevent unintended file scanning. Stick to explicit paths.How can I help you explore Laravel packages today?