Installation
composer require aimin/blog-bundle
Add to config/bundles.php:
return [
// ...
Amin\BlogBundle\AminBlogBundle::class => ['all' => true],
];
Publish Assets
php bin/console assets:install
Database Migrations Run migrations to create tables:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case Create a blog post via CLI:
php bin/console amin:blog:create --title="First Post" --content="Hello World"
Post Management
Use the amin:blog:create, amin:blog:list, and amin:blog:delete commands for CRUD operations.
Example:
php bin/console amin:blog:list --limit=5
Frontend Integration Render posts in templates via Twig:
{% for post in amin_blog_posts() %}
<h2>{{ post.title }}</h2>
<p>{{ post.content|truncate(200) }}</p>
{% endfor %}
API Endpoints Leverage Symfony’s routing to expose posts as JSON:
# config/routes.yaml
amin_blog_posts:
path: /api/posts
controller: Amin\BlogBundle\Controller\PostController::index
Custom Fields
Extend the Post entity by overriding the bundle’s configuration:
# config/packages/amin_blog.yaml
amin_blog:
post:
fields:
- { name: 'custom_field', type: 'string' }
Event Listeners
Subscribe to post events (e.g., PostCreatedEvent) in services.yaml:
services:
App\EventListener\BlogListener:
tags:
- { name: kernel.event_listener, event: amin.blog.post.created, method: onPostCreated }
Symfony Version Mismatch The bundle requires Symfony 4.4.x. Ensure your project matches this version or fork the bundle for compatibility.
Missing Assets If CSS/JS fails to load, run:
php bin/console assets:install --symlink
Doctrine Configuration
Verify doctrine.yaml includes the bundle’s entity mappings:
orm:
mappings:
AminBlogBundle: ~
Command Errors
Use --verbose for commands:
php bin/console amin:blog:create --verbose
Entity Overrides
If extending the Post entity, clear the cache:
php bin/console cache:clear
Custom Controllers
Override the default PostController by defining a new route with higher priority.
Validation Rules
Add custom validation to the Post entity:
use Symfony\Component\Validator\Constraints as Assert;
class Post {
/**
* @Assert\Length(min=10)
*/
private $content;
}
How can I help you explore Laravel packages today?