0

I would like to build an app (e-shop) where every block (partial) can be rendered separately (to serve these blocks with pAJAX), but at the same time, i want to get the whole page if it is requested directly. Let me explain it by example.

e-shop catalog page consits of the following blocks (partials):

  1. products
  2. news
  3. footer
  4. cart

e-shop index page consits of the following blocks (partials):

  1. article
  2. news
  3. footer
  4. cart

e-shop product page consits of the following blocks (partials):

  1. product
  2. news
  3. footer
  4. cart

So if i request "/catalog/robots" i get a page with all for blocks rendered, but when i request "/block/cart" i would like to get only the contents on cart partial.

How to design Controllers (and Views) properly, so i do not have to fetch cart products again and again in every ProductsController, ProductController, IndexController (for example)? Can i do something like:

class IndexController extends \Phalcon\Mvc\Controller {

  use CartController;
  use NewController;
  ....

}

How can i design everything to work as i plan it to work?

Oleg Smith
  • 11
  • 1

1 Answers1

0

I would probably have a bunch of very thin controllers that extends a more substantial base controller

class ShopBaseController extends \Phalcon\Mvc\Controller {

    public function getNews( $limit=10 )
    {
        $news = \App\News::find(
            'query stuff here'
           ,'limit'=>$limit
        );

        $this->view->setVar('news', $news); 
    }

    public function getProducts( ... ){ ... }

    public function getCart( ... ){ ... }

    public function getSpecials( ... ){ ... }

}

class IndexController extends ShopBaseController {

    public function indexAction(){
        $this->getNews(10);
        $this->getProducts(array(
            // product params
        ));
        $this->getCart();
        $this->getSpecials();
    }

}

class CatalogController extends ShopBaseController {

    public function indexAction(){
        $this->getNews(5);
        $this->getProducts(array(
            // product params
        ));
        $this->getCart();
        $this->getSpecials();
    }
}

And then the views

<div>
    <?=$this->partial('blocks/news', array( 'news'=>$news ))?>
</div>

etc

CodeMonkey
  • 3,271
  • 25
  • 50
  • I don't want to be "grumpy point grabber guy" but it looks like you're new here, so if something is great and helped you it is customary to accept the answer and/or mark it up, thanks :) – CodeMonkey Nov 18 '13 at 10:03