Services

Roadiz is built upon Pimple dependency injection container. Thanks to this architecture, all Core and Backoffice services are available from any controller in your themes.

$this->get('nameOfService');
  • Doctrine entity manager: $this->get('em')
  • Twig rendering environment: $this->get('twig.environment')
  • Translator: $this->get('translator')
  • Url matcher: $this->get('urlMatcher')
  • Url generator: $this->get('urlGenerator')
  • Authorization checker: $this->get('securityAuthorizationChecker')
  • User token storage: $this->get('securityTokenStorage')
  • Firewall: $this->get('firewall')
  • Assets packages: $this->get('assetPackages')
  • Email manager: $this->get('emailManager')
  • Contact form manager: $this->get('contactFormManager')
  • Accessing global settings ParameterBag: $this->get('settingsBag')
  • Accessing global node-types ParameterBag: $this->get('nodeTypesBag')
  • Getting an entity handler: $this->get('factory.handler')->getHandler($myEntity)

Entity APIs

All these services are Doctrine repository wrappers meant to ease querying entities inside your themes and according to AuthorizationChecker. This will implicitely check if nodes or node-sources are published when you request them without bothering to insert the right criteria in your findBy calls.

Each of these implements AbstractApi methods getBy and getOneBy

Using Solr API

Solr is a really powerful tool to search over your node database with a clever plain-text search engine and the ability to highlight your criteria in the search results. Before going further, make sure that a Solr server is available and that it is well configured in your config.yml. You can use the bin/roadiz solr:check command to verify and then bin/roadiz solr:reindex command to force synchronizing your node database with Solr index.

You can use the solr.search.nodeSource service and its two methods to get node-sources from a search query.

Simple search results

$this->get('solr.search.nodeSource')->search() method will return a SolrSearchResults traversable object listing NodesSources:

$criteria = [];
/** @var SolrSearchResults $results */
$results = $this->get('solr.search.nodeSource')
                ->search(
                    $request->get('q'), # Use ?q query parameter to search with
                    $criteria,          # a simple criteria array to filter search results
                    10,                 # result count
                    true                # Search in tags too
                    10000               # Proximity (optional, default: 10000)
                    1                   # Page (optional, default: 1)
                );

foreach ($results as $nodeSource) {
    # NodesSources object
    echo $nodeSource->getTitle();
}

Search results with highlighting

$this->get('solr.search.nodeSource')->searchWithHighlight() method will return a SolrSearchResults traversable object with arrays with a simple structure: nodeSource for the NodesSources object and highlighting for the html data with highlighted text wrapped in span.solr-highlight html tag.

$criteria = [];
/** @var SolrSearchResults $results */
$results = $this->get('solr.search.nodeSource')
                ->searchWithHighlight(
                    $request->get('q'), # Use ?q query parameter to search with
                    $criteria,          # a simple criteria array to filter search results
                    10,                 # result count
                    true                # Search in tags too
                    10000               # Proximity (optional, default: 10000)
                    1                   # Page (optional, default: 1)
                );

foreach ($results as $result) {
    # NodesSources object
    $nodeSource = $result['nodeSource'];
    # String object (HTML)
    $highlight = $result['highlighting'];
}

Count search results

$this->get('solr.search.nodeSource')->search() and $this->get('solr.search.nodeSource')->searchWithHighlight()``methods will return a ``SolrSearchResults traversable object. You can use SolrSearchResults::getResultCount() to get results count.

$criteria = [];
/** @var SolrSearchResults $results */
$results = $this->get('solr.search.nodeSource')
                ->search(
                    $request->get('q'), # Use ?q query parameter to search with
                    $criteria,          # a simple criteria array to filter search results
                    10,                 # result count
                    true                # Search in tags too
                    10000               # Proximity (optional, default: 10000)
                    1                   # Page (optional, default: 1)
                );

$pageCount = ceil($results->getResultCount()/$this->getItemPerPage());
$itemPerPage = $this->getItemPerPage();

Search criteria

Solr will search in NodesSources index by default, but it will not filter by translation, type or visibility. You can add your own search criteria using similar filter names as nodeSourceApi.

  • visible: boolean
  • translation: a Translation object
  • _locale: string
  • tags: a Tag object or Tag array
  • nodeType: a NodeType object
  • status: defaults to Node::PUBLISHED
  • publishedAt: \DateTime or array like EntityRepository::findBy method.
$criteria = [
    'visible' => true,
    'translation' => $translation,
    // Returns only published nodes-sources
    'publishedAt' => ['<=', new \DateTime()],
    'nodeType' => [
        $this->get('nodeTypesBag')->get('Page'),
        // …
    ],
];