patternphpMinor
CakePHP static pages (without entity) but with dynamic content
Viewed 0 times
withoutwithbutcakephpentitydynamiccontentpagesstatic
Problem
I have a few pages that is not connected with entities (index page, terms of use page, email page).
Detailed description: In CakePHP, when you want to have a static page (such as index for example), you should use the
1st: To do this, I create a
2nd: I edited the
Question: How can I improve (do it right) the two points referred to above? Any other point to improve ?
CustomStaticPagesController controller code:
```
Auth->user('id');
$username = $this->Auth->user('username');
$this->loadModel('Categories');
$categories = $this->Categories->getAllCategories();
$this->loadModel('SubCategories');
$subCategories = $this->SubCategories->getAllSubCategories();
$subCategoriesName = $this->SubCategories->listAllSubCategories();
$this->loadModel('UserTypes');
$userTypes = $this->UserTypes->listSubCategories();
$this->loadModel('Banners');
$fullBanners = $this->Banners->full();
$smallBanners = $this->Banners->small();
$this->loadModel('Products');
$productsBestSeller = $this->Products->getProductTrendByColumn('sold', 0);
$productsNewer = $this->Products->getProductTrendByColumn('created', 0);
$productsMostPopular = $this->Products->getProductTrendByColumn('visited', 0);
$this->loadModel('Offers');
$offers = $this->Offers->offersRecursive();
$this->loadModel('News');
$news = $this->News->getRecentNews();
$this->set(compact('userId', 'username', 'use
Detailed description: In CakePHP, when you want to have a static page (such as index for example), you should use the
PagesController (not connected to any entity). However, my static pages have dynamic content, as the index page (the navbar is dynamic: it has the name of the user when logged in, and special buttons).1st: To do this, I create a
CustomStaticPagesController controller (which is not connected to any entity), in which I created the methods (index, email, termos-de-servico).2nd: I edited the
routes.php references to the actions without the standard localhost/controller/action now this localhost/action.Question: How can I improve (do it right) the two points referred to above? Any other point to improve ?
CustomStaticPagesController controller code:
```
Auth->user('id');
$username = $this->Auth->user('username');
$this->loadModel('Categories');
$categories = $this->Categories->getAllCategories();
$this->loadModel('SubCategories');
$subCategories = $this->SubCategories->getAllSubCategories();
$subCategoriesName = $this->SubCategories->listAllSubCategories();
$this->loadModel('UserTypes');
$userTypes = $this->UserTypes->listSubCategories();
$this->loadModel('Banners');
$fullBanners = $this->Banners->full();
$smallBanners = $this->Banners->small();
$this->loadModel('Products');
$productsBestSeller = $this->Products->getProductTrendByColumn('sold', 0);
$productsNewer = $this->Products->getProductTrendByColumn('created', 0);
$productsMostPopular = $this->Products->getProductTrendByColumn('visited', 0);
$this->loadModel('Offers');
$offers = $this->Offers->offersRecursive();
$this->loadModel('News');
$news = $this->News->getRecentNews();
$this->set(compact('userId', 'username', 'use
Solution
I'd prefer to place the loadModel() calls in the initialize() function, like so:
That way you get no redundancy and you have them at the same spot.
Easier to maintain.
There are different ways to include your models in the controller, a different way would be to call
I'm mainly using TableRegistry when im creating custom classes.
public function initialize() {
parent::initialize();
$this->loadModel('Model');
}That way you get no redundancy and you have them at the same spot.
Easier to maintain.
There are different ways to include your models in the controller, a different way would be to call
Cake\ORM\TableRegistry('Table');I'm mainly using TableRegistry when im creating custom classes.
Code Snippets
public function initialize() {
parent::initialize();
$this->loadModel('Model');
}Context
StackExchange Code Review Q#114286, answer score: 2
Revisions (0)
No revisions yet.