Installation:
composer require carlescliment/light-news-bundle:dev-master
Update app/AppKernel.php to include:
new BladeTester\LightNewsBundle\BladeTesterLightNewsBundle(),
Add routing in app/config/routing.yml:
blade_tester_light_news_bundle:
resource: "@BladeTesterLightNewsBundle/Resources/config/routing.yml"
prefix: /news
First Use Case:
BladeTesterLightNewsBundle:
namespace App\NewsBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AppNewsBundle extends Bundle {
public function getParent() { return 'BladeTesterLightNewsBundle'; }
}
AppKernel.php.Define a News Entity:
Extend BladeTester\LightNewsBundle\Entity\News and add a unique id field:
namespace App\NewsBundle\Entity;
use BladeTester\LightNewsBundle\Entity\News as BaseNews;
class News extends BaseNews {
// Add custom fields (e.g., title, content, publishedAt)
/**
* @ORM\Column(type="string")
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $content;
}
Run Migrations:
php bin/console doctrine:schema:update --force
Access the Admin Interface:
Navigate to /news/admin to manage news entries.
Entity Customization:
BaseNews to add fields (e.g., slug, author, featuredImage).@ORM\Column, @ORM\ManyToOne) or YAML/XML for mapping./**
* @ORM\ManyToOne(targetEntity="App\User\Entity\User")
*/
private $author;
Repository Integration:
BladeTester\LightNewsBundle\Repository\NewsRepository) in your bundle’s Resources/config/doctrine/:
App\NewsBundle\Entity\News:
type: entity
repository: App\NewsBundle\Repository\NewsRepository
findPublished()):
public function findPublished() {
return $this->createQueryBuilder('n')
->where('n.publishedAt <= :now')
->setParameter('now', new \DateTime())
->orderBy('n.publishedAt', 'DESC')
->getQuery()
->getResult();
}
Routing and Controllers:
Resources/config/routing.yml:
news_list:
path: /list
defaults: { _controller: AppNewsBundle:News:index }
NewsController) in src/App/NewsBundle/Controller/ to add logic (e.g., filtering, pagination).Twig Integration:
public function indexAction() {
$news = $this->getDoctrine()->getRepository('AppNewsBundle:News')->findAll();
return $this->render('AppNewsBundle:News:index.html.twig', ['news' => $news]);
}
{{ news|light_news_format_date }}) if provided.Form Handling:
BladeTester\LightNewsBundle\Form\NewsType) in src/App/NewsBundle/Form/NewsType.php:
namespace App\NewsBundle\Form;
use Symfony\Component\Form\AbstractType;
use BladeTester\LightNewsBundle\Form\NewsType as BaseNewsType;
class NewsType extends BaseNewsType {
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
$builder->add('featuredImage', FileType::class);
}
}
Event Listeners:
news.pre_publish) in src/App/NewsBundle/EventListener/:
namespace App\NewsBundle\EventListener;
use BladeTester\LightNewsBundle\Event\NewsEvent;
class NewsListener {
public function onPrePublish(NewsEvent $event) {
$news = $event->getNews();
$news->setSlug($this->generateSlug($news->getTitle()));
}
}
services.yml:
services:
app.news.listener:
class: App\NewsBundle\EventListener\NewsListener
tags:
- { name: kernel.event_listener, event: news.pre_publish, method: onPrePublish }
API Endpoints:
public function apiListAction() {
$news = $this->getDoctrine()->getRepository('AppNewsBundle:News')->findAll();
return $this->json($news, 200, [], ['groups' => ['api']]);
}
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @Groups({"api"})
*/
private $title;
Bundle Inheritance:
getParent() in your custom bundle will break functionality. Always verify:
public function getParent() { return 'BladeTesterLightNewsBundle'; }
Entity Mapping:
id field is mandatory. Omitting it or misconfiguring it will cause Doctrine errors.BladeTester\LightNewsBundle\Entity\News (not just BaseNews directly).Routing Conflicts:
/news) may clash with existing routes. Test with:
php bin/console debug:router | grep news
Form Overrides:
NewsType, ensure you call parent::buildForm() to retain default fields (e.g., title, content).php bin/console cache:clear
Event System:
news.pre_publish are not documented. Inspect the bundle’s EventDispatcher for available events:
$dispatcher = $this->get('event_dispatcher');
$events = $dispatcher->getListeners();
Symfony 2.1 Legacy:
php bin/console debug:container --parameters | grep symfony_version
Doctrine Configuration:
Resources/config/doctrine/ is properly namespaced and loaded. Place it in:
src/App/NewsBundle/Resources/config/doctrine/
Check Bundle Loading:
php bin/console debug:container | grep light_news
Entity Validation:
php bin/console doctrine:schema:validate
Event Debugging:
var/log/dev.log:
use Psr\Log\LoggerInterface;
class NewsListener {
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function onPrePublish(NewsEvent $event) {
$this->logger->info('News pre-publish event triggered', ['news' => $event->getNews()->getTitle()]);
}
}
Route Debugging:
php bin/console router:debug
Custom Admin Templates:
src/App/NewsBundle/Resources/views/ (e.g., News/index.html.twig).Validation Groups:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\NotBlank(groups={"registration"})
*/
private $title;
Asset Management:
Resources/public/ files (e.g., CSS/JS for the admin panel).Security:
How can I help you explore Laravel packages today?