问题
I have a few pages that is not conected 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
(is 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 him 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@gmail.com' => 'Store Site'])
->to('email@outlook.com')
->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');
});
来源:https://stackoverflow.com/questions/34337525/cakephp-static-pages-without-entity-but-with-dynamic-content