Installation via Composer (if not symlinked):
composer require awaresoft/sonata-news-bundle
Note: Follow the README instructions for symlinking if modifying locally.
Enable the Bundle in config/bundles.php:
return [
// ...
Awaresoft\SonataNewsBundle\SonataNewsBundle::class => ['all' => true],
];
Run Migrations (if using Doctrine):
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
Basic Routing in config/routes.yaml:
sonata_news:
resource: "@SonataNewsBundle/Resources/config/routing/all.xml"
prefix: /news
First Use Case:
{% for news in sonata_news_list %}
<h2>{{ news.title }}</h2>
<p>{{ news.content|truncate(200) }}</p>
{% endfor %}
Admin Integration:
SonataAdminBundle to manage news entities:
// src/Admin/NewsAdmin.php
use Awaresoft\SonataNewsBundle\Admin\NewsAdmin as BaseNewsAdmin;
class NewsAdmin extends BaseNewsAdmin {
protected function configureListFields(ListMapper $listMapper) {
$listMapper->add('title');
$listMapper->add('createdAt');
}
}
Custom News Types:
News entity:
// src/Entity/News.php
use Awaresoft\SonataNewsBundle\Entity\News as BaseNews;
class News extends BaseNews {
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $customField;
}
API Endpoints:
// src/Controller/NewsController.php
use Awaresoft\SonataNewsBundle\Entity\NewsRepository;
class NewsController extends AbstractController {
public function list(NewsRepository $repo): JsonResponse {
return $this->json($repo->findAll());
}
}
Frontend Display:
{{ sonata_news_list({
'filter': 'published',
'sort': 'createdAt',
'direction': 'desc'
}) }}
Event Listeners:
news.publish):
// src/EventListener/NewsListener.php
use Awaresoft\SonataNewsBundle\Event\NewsEvent;
class NewsListener {
public function onNewsPublish(NewsEvent $event) {
// Send notification, log, etc.
}
}
Symfony 2.x Dependency:
Doctrine ORM Assumptions:
createdAt and updatedAt fields exist on the News entity. Override the entity if missing:
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
Admin Bundle Dependency:
SonataAdminBundle for CRUD. If not using SonataAdmin, manually implement controllers/repositories.Routing Conflicts:
all.xml routes may clash with existing routes. Override in routing.yml:
sonata_news_list:
path: /custom-news
defaults: { _controller: sonata_news.controller.list }
Translation Quirks:
{{ 'News'|trans }}
Enable Debug Mode:
APP_DEBUG=1 php bin/console cache:clear
Check Event Dispatching:
services.yaml:
services:
App\EventListener\NewsListener:
tags:
- { name: kernel.event_listener, event: sonata.news.publish, method: onNewsPublish }
Database Schema:
php bin/console doctrine:schema:update --dump-sql
Custom Repositories:
NewsRepository for complex queries:
class CustomNewsRepository extends NewsRepository {
public function findByCategory(string $category) {
return $this->createQueryBuilder('n')
->where('n.category = :category')
->setParameter('category', $category)
->getQuery()
->getResult();
}
}
Form Overrides:
// src/Form/NewsType.php
use Awaresoft\SonataNewsBundle\Form\Type\NewsType as BaseNewsType;
class NewsType extends BaseNewsType {
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder->add('customField');
}
}
Twig Global Variables:
// src/Twig/AppExtension.php
class AppExtension extends \Twig\Extension\AbstractExtension {
public function getGlobals() {
return [
'custom_news_config' => ['max_items' => 10],
];
}
}
Asset Management:
public/bundles/sonatanews/ and modifying.Security:
// src/Security/Voter/NewsVoter.php
use Awaresoft\SonataNewsBundle\Entity\NewsInterface;
class NewsVoter extends AbstractVoter {
protected function supports(string $attribute, $subject): bool {
return $subject instanceof NewsInterface;
}
}
How can I help you explore Laravel packages today?