Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggested way to use news in a custom controller #30

Open
FlorianBoe opened this issue Nov 21, 2022 · 3 comments
Open

Suggested way to use news in a custom controller #30

FlorianBoe opened this issue Nov 21, 2022 · 3 comments
Labels
question Further information is requested

Comments

@FlorianBoe
Copy link

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
Copy link
Owner

TheCadien commented Nov 21, 2022

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
Copy link
Author

FlorianBoe commented Nov 23, 2022

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
Copy link
Contributor

porl commented Dec 21, 2022

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:

  • example.com/news shows an index of all news posts
  • example.com/news/1 shows an individual post page

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?

@TheCadien TheCadien added the question Further information is requested label Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants