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

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

        //Sets the cache name
        $cache = sprintf('category_%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->PostsCategories->getCacheName()
        ));

        //If the data are not available from the cache
        if (empty($posts) || empty($paging)) {
            $query = $this->PostsCategories->Posts->find('active')
                ->find('forIndex')
                ->where([sprintf('%s.slug', $this->PostsCategories->getAlias()) => $slug]);

            is_true_or_fail(!$query->isEmpty(), I18N_NOT_FOUND, RecordNotFoundException::class);

            $posts = $this->paginate($query);

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

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

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

        //Sets the cache name
        $cache = sprintf('category_%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->PostsCategories->getCacheName()
        ));

        //If the data are not available from the cache
        if (empty($posts) || empty($paging)) {
            $query = $this->PostsCategories->Posts->find('active')
                ->find('forIndex')
                ->where([sprintf('%s.slug', $this->PostsCategories->getAlias()) => $slug]);

            is_true_or_fail(!$query->isEmpty(), I18N_NOT_FOUND, RecordNotFoundException::class);

            $posts = $this->paginate($query);

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

        $this->set('category', $posts->extract('category')->first());
        $this->set(compact('posts'));
    }
public function view ( $ slug = null ) { //The category can be passed as query string, from a widget if ( $ this -> request -> getQuery ( 'q' ) ) { return $ this -> redirect ( [ $ this -> request -> getQuery ( 'q' ) ] ) ; } $ page = $ this -> request -> getQuery ( 'page' , 1 ) ; //Sets the cache name $ cache = sprintf ( 'category_%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 -> PostsCategories -> getCacheName ( ) ) ) ; //If the data are not available from the cache if ( empty ( $ posts ) || empty ( $ paging ) ) { $ query = $ this -> PostsCategories -> Posts -> find ( 'active' ) -> find ( 'forIndex' ) -> where ( [ sprintf ( '%s.slug' , $ this -> PostsCategories -> getAlias ( ) ) => $ slug ] ) ; is_true_or_fail ( ! $ query -> isEmpty ( ) , I18N_NOT_FOUND , RecordNotFoundException :: class ) ; $ posts = $ this -> paginate ( $ query ) ; //Writes on cache Cache :: writeMany ( [ $ cache => $ posts , sprintf ( '%s_paging' , $ cache ) => $ this -> request -> getParam ( 'paging' ) , ] , $ this -> PostsCategories -> getCacheName ( ) ) ; //Else, sets the paging parameter } else { $ this -> request = $ this -> request -> withParam ( 'paging' , $ paging ) ; } $ this -> set ( 'category' , $ posts -> extract ( 'category' ) -> first ( ) ) ; $ this -> set ( compact ( 'posts' ) ) ; }
Lists posts for a category
@param string $slug Category slug
@return \Cake\Network\Response|null|void
@throws RecordNotFoundException
Lists posts for a category