Installation
composer require dywee/news-bundle
Register the bundle in config/bundles.php:
return [
// ...
Dywee\NewsBundle\DyweeNewsBundle::class => ['all' => true],
];
Routing
Add to config/routes.yaml:
dywee_news:
resource: "@DyweeNewsBundle/Controller/"
type: annotation
prefix: /news
First Use Case
DyweeCoreBundle is installed) or manually create a News entity via:
$news = new \Dywee\NewsBundle\Entity\News();
$news->setTitle('My News');
$news->setContent('Content here...');
$news->setPublishedAt(new \DateTime());
$em->persist($news);
$em->flush();
$news = $em->getRepository(\Dywee\NewsBundle\Entity\News::class)
->findBy([], ['publishedAt' => 'DESC']);
Admin Integration
DyweeCoreBundle, leverage its CRUD admin generator for News entities. Configure in config/admin.yml:
dywee_core:
admin:
news:
entity: Dywee\NewsBundle\Entity\News
menu: News
Frontend Display
dywee_news namespace in templates:
{% for news in dywee_news_list %}
<h2>{{ news.title }}</h2>
<p>{{ news.content|truncate(200) }}</p>
{% endfor %}
$newsService = $this->get('dywee_news.service.news');
$latestNews = $newsService->getLatest(5);
Custom Queries
$publishedNews = $em->createQuery(
'SELECT n FROM DyweeNewsBundle:News n WHERE n.publishedAt <= :now ORDER BY n.publishedAt DESC'
)->setParameter('now', new \DateTime())
->getResult();
Event Listeners
NewsEvents (if defined) for post-save logic:
// config/services.yaml
Dywee\NewsBundle\EventListener\NewsListener:
tags:
- { name: kernel.event_listener, event: dywee.news.post_save, method: onNewsSaved }
Missing DyweeCoreBundle
DyweeCoreBundle for admin features. Without it, you’ll need to manually handle CRUD operations or disable admin routes in routing.yml.Entity Configuration
News entity may lack annotations (e.g., @ORM\Table). Verify your Entity/News.php includes:
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="Dywee\NewsBundle\Repository\NewsRepository")
* @ORM\Table(name="news")
*/
Routing Conflicts
prefix: / in routing may clash with other bundles. Use a unique prefix (e.g., /news) and clear cache:
php bin/console cache:clear
Translation Support
News entity fields are marked translatable (e.g., title, content) and configure liip/translate-bundle separately.php bin/console debug:router | grep dywee_news
php bin/console doctrine:schema:validate
NewsService is registered:
php bin/console debug:container dywee_news.service.news
Custom Fields
News entity:
/**
* @ORM\Column(type="string", nullable=true)
*/
private $customField;
Validation
News entity:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\NotBlank
*/
private $title;
API Integration
NewsOutput DTO or serializer group:
# config/serializer/Entity.News.yaml
Dywee\NewsBundle\Entity\News:
attributes:
publishedAt:
groups: [api]
How can I help you explore Laravel packages today?