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 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:
<?php
namespace App\Controller;
use App\AppClasses\FormatFormValues\FormatContactForm;
use Cake\Event\Event;
use Cake\Network\Email\Email;
class CustomStaticPagesController extends AppController
{
public function index()
{
$userId = $this->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', 'userTypes', 'smallBanners',
'fullBanners', 'offers', 'news', 'categories', 'subCategories',
'subCategoriesName', 'productsBestSeller', 'productsNewer',
'productsMostPopular'));
}
public function perguntasFrequentes()
{
$userId = $this->Auth->user('id');
$username = $this->Auth->user('username');
$this->loadModel('UserTypes');
$userTypes = $this->UserTypes->listSubCategories();
$this->set(compact('userId', 'username', 'userTypes'));
}
public function termosDeServico()
{
$userId = $this->Auth->user('id');
$username = $this->Auth->user('username');
$this->loadModel('UserTypes');
$userTypes= $this->UserTypes->listSubCategories();
$this->set(compact('userId', 'username', 'userTypes'));
}
public function politicasDePrivacidade()
{
$userId = $this->Auth->user('id');
$username = $this->Auth->user('username');
$this->loadModel('UserTypes');
$userTypes = $this->UserTypes->listSubCategories();
$this->set(compact('userId', 'username', 'userTypes'));
}
public function email()
{
if ($this->request->is('get'))
{
$userId = $this->Auth->user('id');
$username = $this->Auth->user('username');
$this->loadModel('UserTypes');
$userTypes = $this->UserTypes->listSubCategories();
$this->set(compact('userId', 'username', 'userTypes'));
}else if($this->request->is('post'))
{
Email::configTransport('gmail', [
'host' => 'smtp.gmail.com',
'port' => 587,
'username' => 'xxxxxxxxxxxxx',
'password' => 'xxxxxxxxxxxxx',
'className' => 'Smtp',
'tls' => true
]);
$email = new Email();
$email->transport('gmail');
$email->from(['[email protected]' => 'Store Site'])
->to('[email protected]')
->emailFormat('html')
->subject(
FormatContactForm::getSubject(
$this->request->data['subject'],
['suffix' => ' | Store Site']
)
)
->send(
FormatContactForm::getMessage(
$this->request->data,
['uppercaseLabel' => true]
)
);
return $this->redirect(['controller' => 'CustomStaticPages', 'action' => 'index']);
}
}
public function beforeFilter(Event $event)
{
$this->Auth->allow(['index', 'perguntasFrequentes', 'email', 'politicasDePrivacidade', 'termosDeServico']);
}
}
routes.php code
Router::scope('/', function ($routes) {
$routes->connect('/', ['controller' => 'CustomStaticPages', 'action' => 'index']);
$routes->connect('/pages/*', ['controller' => 'CustomStaticPages', 'action' => 'index']);
$routes->connect('/termos-de-servico', ['controller' => 'CustomStaticPages', 'action' => 'termosDeServico']);
$routes->connect('/politicas-de-privacidade', ['controller' => 'CustomStaticPages', 'action' => 'politicasDePrivacidade']);
$routes->connect('/perguntas-frequentes', ['controller' => 'CustomStaticPages', 'action' => 'perguntasFrequentes']);
$routes->connect('/email', ['controller' => 'CustomStaticPages', 'action' => 'email']);
$routes->fallbacks('DashedRoute');
});
-
\$\begingroup\$ Every question on Code Review needs at least a language tag, which is, in this case, php. \$\endgroup\$200_success– 200_success2015年12月18日 01:24:18 +00:00Commented Dec 18, 2015 at 1:24
2 Answers 2
I'd prefer to place the loadModel() calls in the initialize() function, like so:
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.
For Index page, Navigations and other widgets use Cells http://book.cakephp.org/3.0/en/views/cells.html
-
1\$\begingroup\$ You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (why are Cells an advantage?) so that the author can learn from your thought process. \$\endgroup\$SuperBiasedMan– SuperBiasedMan2015年12月17日 15:13:04 +00:00Commented Dec 17, 2015 at 15:13