public function view($slug = null)
    {
        //Data can be passed as query string, from a widget
        if ($this->request->getQuery('q')) {
            return $this->redirect([$this->request->getQuery('q')]);
        }

        $slug = Text::slug($slug, ['replacement' => ' ']);

        $tag = $this->PostsTags->Tags->findActiveByTag($slug)
            ->cache((sprintf('tag_%s', md5($slug))), $this->PostsTags->getCacheName())
            ->firstOrFail();

        $page = $this->request->getQuery('page', 1);

        //Sets the cache name
        $cache = sprintf('tag_%s_limit_%s_page_%s', md5($slug), $this->paginate['limit'], $page);

        //Tries to get data from the cache
        list($posts, $paging) = array_values(Cache::readMany(
            [$cache, sprintf('%s_paging', $cache)],
            $this->PostsTags->getCacheName()
        ));

        //If the data are not available from the cache
        if (empty($posts) || empty($paging)) {
            $query = $this->PostsTags->Posts->find('active')
                ->find('forIndex')
                ->matching($this->PostsTags->Tags->getAlias(), function (Query $q) use ($slug) {
                    return $q->where(['tag' => $slug]);
                });
            $posts = $this->paginate($query);

            //Writes on cache
            Cache::writeMany([
                $cache => $posts,
                sprintf('%s_paging', $cache) => $this->request->getParam('paging'),
            ], $this->PostsTags->getCacheName());
        //Else, sets the paging parameter
        } else {
            $this->request = $this->request->withParam('paging', $paging);
        }

        $this->set(compact('posts', 'tag'));
    }
public function view($slug = null)
    {
        //Data can be passed as query string, from a widget
        if ($this->request->getQuery('q')) {
            return $this->redirect([$this->request->getQuery('q')]);
        }

        $slug = Text::slug($slug, ['replacement' => ' ']);

        $tag = $this->PostsTags->Tags->findActiveByTag($slug)
            ->cache((sprintf('tag_%s', md5($slug))), $this->PostsTags->getCacheName())
            ->firstOrFail();

        $page = $this->request->getQuery('page', 1);

        //Sets the cache name
        $cache = sprintf('tag_%s_limit_%s_page_%s', md5($slug), $this->paginate['limit'], $page);

        //Tries to get data from the cache
        list($posts, $paging) = array_values(Cache::readMany(
            [$cache, sprintf('%s_paging', $cache)],
            $this->PostsTags->getCacheName()
        ));

        //If the data are not available from the cache
        if (empty($posts) || empty($paging)) {
            $query = $this->PostsTags->Posts->find('active')
                ->find('forIndex')
                ->matching($this->PostsTags->Tags->getAlias(), function (Query $q) use ($slug) {
                    return $q->where(['tag' => $slug]);
                });
            $posts = $this->paginate($query);

            //Writes on cache
            Cache::writeMany([
                $cache => $posts,
                sprintf('%s_paging', $cache) => $this->request->getParam('paging'),
            ], $this->PostsTags->getCacheName());
        //Else, sets the paging parameter
        } else {
            $this->request = $this->request->withParam('paging', $paging);
        }

        $this->set(compact('posts', 'tag'));
    }
public function view ( $ slug = null ) { //Data can be passed as query string, from a widget if ( $ this -> request -> getQuery ( 'q' ) ) { return $ this -> redirect ( [ $ this -> request -> getQuery ( 'q' ) ] ) ; } $ slug = Text :: slug ( $ slug , [ 'replacement' => ' ' ] ) ; $ tag = $ this -> PostsTags -> Tags -> findActiveByTag ( $ slug ) -> cache ( ( sprintf ( 'tag_%s' , md5 ( $ slug ) ) ) , $ this -> PostsTags -> getCacheName ( ) ) -> firstOrFail ( ) ; $ page = $ this -> request -> getQuery ( 'page' , 1 ) ; //Sets the cache name $ cache = sprintf ( 'tag_%s_limit_%s_page_%s' , md5 ( $ slug ) , $ this -> paginate [ 'limit' ] , $ page ) ; //Tries to get data from the cache list ( $ posts , $ paging ) = array_values ( Cache :: readMany ( [ $ cache , sprintf ( '%s_paging' , $ cache ) ] , $ this -> PostsTags -> getCacheName ( ) ) ) ; //If the data are not available from the cache if ( empty ( $ posts ) || empty ( $ paging ) ) { $ query = $ this -> PostsTags -> Posts -> find ( 'active' ) -> find ( 'forIndex' ) -> matching ( $ this -> PostsTags -> Tags -> getAlias ( ) , function ( Query $ q ) use ( $ slug ) { return $ q -> where ( [ 'tag' => $ slug ] ) ; } ) ; $ posts = $ this -> paginate ( $ query ) ; //Writes on cache Cache :: writeMany ( [ $ cache => $ posts , sprintf ( '%s_paging' , $ cache ) => $ this -> request -> getParam ( 'paging' ) , ] , $ this -> PostsTags -> getCacheName ( ) ) ; //Else, sets the paging parameter } else { $ this -> request = $ this -> request -> withParam ( 'paging' , $ paging ) ; } $ this -> set ( compact ( 'posts' , 'tag' ) ) ; }
Lists posts for a tag
@param string $slug Tag slug
@return \Cake\Network\Response|null|void
Lists posts for a tag