Static CakePHP pages (no object), but with dynamic content

I have several pages that are not related to objects (index page, use page, email page).

Detailed description: In CakePHP, if you want to have a static page (for example, an index), you should use PagesController (not connected to any entity). However, my static pages have dynamic content as an index page (the navigation bar is dynamic (it has a login name and special buttons).

1st: For this, I create a CustomStaticPagesController controller (which is not connected to any entity), in it I created methods (index, email address, termos-de-servico).

2nd: I edited the route.php links to actions without the standard localhost / controller / action, now this is localhost / action.

Question: How can I improve (correctly) the two points mentioned above? Any other improvements?

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']); } } 

route.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'); }); 
+1
source share

Source: https://habr.com/ru/post/1341595/


All Articles