0

I would like to ask about optimal approach to creation of reusable content blocks in CakePHP 1.3.

Under reusable content blocks I mean partial views used to build up the page. E.g. in an eshop application the minicart summary can be considered as reusable content block - it is displayed on each page header.

In CakePHP 1.3 there are two posibilities (and both have serious disadvantages):

  • Creation of element /app/plugins/myeshop/elements/minicart.ctp and use $this->element('minicart) to load this reusable content e.g. in header. The disadvantage is that this beaks business of Cart entity into many places. Minicart is only some representation of Cart entity whose views are treated by controller /app/plugins/myeshop/controllers/carts_controller.php. So why to put it out of controller and keep it in element? Elements are good to keep some general reusable contents e.g. header, footer, interactive_map, ... something which is not related to application business objects/entities.

  • Creation of method CartsController::minicart() with corresponding view and use $this->requestAction('/myeshop/carts/minicart') to load this reusable content e.g. in header. The advantage is that now all business and views of Cart entity is treated by CartsController. There are no side-logic and side-views hidden in elements. The disadvantage of this approach is apparent - use of requestAction() costs a lot of time.

At this point I must say that I totally agree that requestAction() must be used very carefully. To use it to call some procedural/busines logic of controller is bad application design. Such kind of logic should not be placed in controller but in model. Still, IMHO, it is legitimate to call controller action to get a partial/reusable content (view) and keep the entity business on one place.

Does the CakePHP have some optimal solution for this?

3 Answers 3

0

I highly suggest going the route of elements/requestActions.

Here's a great article by CakePHP Master Mark Story: "How using requestAction increased performance on my site"

We've built many large, highly-traffic sites using CakePHP and use requestActions all over, and our sites load extremely fast.

1
  • thanks for answer. It seems that use of elements is inevitable, though only to cache the results of requestAction().
    – Mojo
    Mar 12, 2013 at 7:55
0

If possible I would create it as an element. I assume your controller will calculate the contents of the mini cart and the view will display it?

If so its fine to use an element. You certainly dont want additional HTTP requests as you state.

However, the element should only display the content, it should perform any business logic. You need to incldue this in a Controller or Component and set the necessary elements for the view.

If you only need it on certain pages, you can create a new layout.ctp file that includes the mini cart and then use this layout on those pages.

0

If this is just about displaying the content of your cart on every page I recommend this solution: Write the cart into session, if the user is not logged in and logs in sync it with the database. If a user comes back and logs in restore the session from the saved cart.

In your AppController::beforeFilter() read and set the session data to the view or if it is just read, read it using the session helper in your minicart element. You won't have additional requestAction() calls by this or any additional db queries.

I've implemented my cart like this, you can take a look at it here. https://github.com/burzum/cart a working example app is also available https://github.com/burzum/CartSampleApp

1
  • thanks :), but eshop cart was only an example of reusable content block. I have asked how to create reusable content blocks in CakePHP. Another examples of reusable contents/views: related products, top 10 products, news, recent posts, ...
    – Mojo
    Mar 12, 2013 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.