Newscoop\Services\ArticleService::articleResolver PHP Метод

articleResolver() публичный Метод

Resolve article from provided data
public articleResolver ( Request $request ) : Article
$request Symfony\Component\HttpFoundation\Request Request object
Результат Newscoop\Entity\Article $article Article entity object
    public function articleResolver(Request $request)
    {
        // get the article information from the URL
        // explode the information and use it to fetch the article
        $uriExplode = explode('/', $request->server->get('REQUEST_URI'));
        // Articles are allways under newscoop_zendbridge_bridge_index_3 route
        if ($request->attributes->get('_route') != 'newscoop_zendbridge_bridge_index_3') {
            return;
        }
        // if key 4 does not exist, it's probably not an article
        if (array_key_exists(4, $uriExplode) && $uriExplode[4] !== '') {
            $articleInfo['id'] = $uriExplode[4];
            $articleInfo['lang'] = $uriExplode[1];
            $articleInfo['section'] = $uriExplode[3];
            $query = $this->em->createQuery('SELECT a, p, i, s FROM Newscoop\\Entity\\Article a LEFT JOIN a.packages p LEFT JOIN a.issue i LEFT JOIN a.section s LEFT JOIN a.language l WHERE a.number = :number AND l.code = :code');
            $article = $query->setParameters(array('number' => $articleInfo['id'], 'code' => $articleInfo['lang']))->getArrayResult();
            if (!empty($article)) {
                // fill the article meta data
                $this->articleMetadata['id'] = $article[0]['number'];
                $this->articleMetadata['name'] = $article[0]['name'];
                $this->articleMetadata['issue'] = $article[0]['issue']['name'];
                $this->articleMetadata['issue_id'] = $article[0]['issueId'];
                $this->articleMetadata['section'] = $article[0]['section']['name'];
                $this->articleMetadata['section_id'] = $article[0]['sectionId'];
                $this->articleMetadata['language_code'] = $articleInfo['lang'];
                $this->articleMetadata['language_id'] = $article[0]['IdLanguage'];
                // add the meta data to the request
                $request->attributes->set('_newscoop_article_metadata', $this->articleMetadata);
                return true;
            } else {
                return;
            }
        } else {
            return;
        }
    }

Usage Example

Пример #1
0
 public function onRequest(GetResponseEvent $event)
 {
     $request = $event->getRequest();
     $pos = strpos($request->getRequestUri(), '/admin');
     if ($pos === false) {
         $this->articleService->articleResolver($request);
     }
 }