TheCadien / SuluNewsBundle

Simple News Bundle for Sulu 2 CMS
MIT License
14 stars 12 forks source link

Suggested way to use news in a custom controller #30

Open FlorianBoe opened 1 year ago

FlorianBoe commented 1 year ago

Hi there!

I have created my own controller for a news overview page and would like to display the latest news paginated here. What is the intended way to access news in the controller?

I have tried the following variant recommended in the SULU documentation to pass additional data to the template, unfortunately without success.

final class NewsController extends DefaultController
{
    protected function getAttributes($attributes, StructureInterface $structure = null, $preview = false)
    {
        $attributes = parent::getAttributes($attributes, $structure, $preview);
        $attributes['news_list'] = $this->container->get('sulu_news.repository')->getPublishedNews();

        return $attributes;
    }

    public static function getSubscribedServices(): array
    {
        $subscribedServices = parent::getSubscribedServices();
        $subscribedServices['sulu_news.repository'] = 'sulu_news.repository';

        return $subscribedServices;
    }
}

Leads to the following error message:

"App\Controller\Website\NewsController::getSubscribedServices()" must return valid PHP types for service "App\Controller\Website\NewsController" key "sulu_news.repository", "sulu_news.repository" returned.

I have tried to access the repository directly

final class NewsController extends DefaultController
{
    public function __construct(
        private readonly NewsRepository $newsRepository,
    )
    {}

    protected function getAttributes($attributes, StructureInterface $structure = null, $preview = false)
    {
        $attributes = parent::getAttributes($attributes, $structure, $preview);
        $attributes['news_list'] = $this->newsRepository->getPublishedNews();

        return $attributes;
    }
}

But get the following error message

Cannot autowire service "App\Controller\Website\NewsController": argument "$newsRepository" of method "__construct()" references class "TheCadien\Bundle\SuluNewsBundle\Repository\NewsRepository" but no such service exists. You should maybe alias this class to one of these existing services: "sulu_news.repository", "sulu.repository.news".

Because I am a bit under time pressure in the current project, I have added the following alias in services.xml

<service id="TheCadien\Bundle\SuluNewsBundle\Repository\NewsRepository" alias="sulu_news.repository" />

Would it make sense to introduce an alias for the repository or is there a better way with SULU that I'm overlooking here?

TheCadien commented 1 year ago

This guide is primarily intended for creating your own controllers in the PHPCR context. That is, within the webspace bundle. You will not need this for the most part within the NewsBundle . This is based on a Doctrine entity. Please take a look at the controller of the NewsBundle and copy and adapt it as you need it.

FlorianBoe commented 1 year ago

I looked at your controller, I don't think it answers my question. I'm not trying to customize the view of a single news item, I'm trying to access the repository to create a list view.

porl commented 1 year ago

I am also trying to work out the best way to approach getting news posts to the front end.

I'd like, for example, to be able to do something similar to the following:

In Symfony itself this is pretty straightforward using a Controller extending AbstractController but the Sulu documents don't seem to show much info on standalone controllers that don't have a connection to a specific page template.

Is there perhaps an example project somewhere that has the news integrated into the front end? You mention looking at the controller from the bundle, but what is required to get the autowired services connected as @FlorianBoe mentions?