title: CakePHP static pages (without entity) but with dynamic content
body: <p>I have a few pages that is not conected with entities (index page, terms of use page, email page).</p>
  
  <p><strong>Detailed description:</strong> In CakePHP when you want to have a static page (such as index for example) you should use the <code>PagesController</code> (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).</p>
  
  <p><strong>1st:</strong> 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).</p>
  
  <p><strong>2nd:</strong> I edited the routes.php references to the actions without the standard localhost/controller/action now this localhost/action.</p>
  
  <p><strong>Question:</strong> How can I improve (do it right) the two points referred to above? Any other point to improve ?</p>
  
  <p><strong>CustomStaticPagesController controller code:</strong></p>
  
  <pre><code>&lt;?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-&gt;Auth-&gt;user('id');
          $username = $this-&gt;Auth-&gt;user('username');
  
          $this-&gt;loadModel('Categories');
          $categories = $this-&gt;Categories-&gt;getAllCategories();
  
          $this-&gt;loadModel('SubCategories');
          $subCategories = $this-&gt;SubCategories-&gt;getAllSubCategories();
          $subCategoriesName = $this-&gt;SubCategories-&gt;listAllSubCategories();
  
          $this-&gt;loadModel('UserTypes');
          $userTypes = $this-&gt;UserTypes-&gt;listSubCategories();
  
          $this-&gt;loadModel('Banners');
          $fullBanners = $this-&gt;Banners-&gt;full();
          $smallBanners = $this-&gt;Banners-&gt;small();
  
          $this-&gt;loadModel('Products');
          $productsBestSeller = $this-&gt;Products-&gt;getProductTrendByColumn('sold', 0);
          $productsNewer = $this-&gt;Products-&gt;getProductTrendByColumn('created', 0);
          $productsMostPopular = $this-&gt;Products-&gt;getProductTrendByColumn('visited', 0);
  
          $this-&gt;loadModel('Offers');
          $offers = $this-&gt;Offers-&gt;offersRecursive();
  
          $this-&gt;loadModel('News');
          $news = $this-&gt;News-&gt;getRecentNews();
  
          $this-&gt;set(compact('userId', 'username', 'userTypes', 'smallBanners',
              'fullBanners', 'offers', 'news', 'categories', 'subCategories',
              'subCategoriesName', 'productsBestSeller', 'productsNewer',
              'productsMostPopular'));
      }
  
      public function perguntasFrequentes()
      {
          $userId = $this-&gt;Auth-&gt;user('id');
          $username = $this-&gt;Auth-&gt;user('username');
  
          $this-&gt;loadModel('UserTypes');
          $userTypes = $this-&gt;UserTypes-&gt;listSubCategories();
  
          $this-&gt;set(compact('userId', 'username', 'userTypes'));
      }
  
      public function termosDeServico()
      {
          $userId = $this-&gt;Auth-&gt;user('id');
          $username = $this-&gt;Auth-&gt;user('username');
  
          $this-&gt;loadModel('UserTypes');
          $userTypes= $this-&gt;UserTypes-&gt;listSubCategories();
  
          $this-&gt;set(compact('userId', 'username', 'userTypes'));
      }
  
      public function politicasDePrivacidade()
      {
          $userId = $this-&gt;Auth-&gt;user('id');
          $username = $this-&gt;Auth-&gt;user('username');
  
          $this-&gt;loadModel('UserTypes');
          $userTypes = $this-&gt;UserTypes-&gt;listSubCategories();
  
          $this-&gt;set(compact('userId', 'username', 'userTypes'));
      }
  
      public function email()
      {
          if ($this-&gt;request-&gt;is('get'))
          {
              $userId = $this-&gt;Auth-&gt;user('id');
              $username = $this-&gt;Auth-&gt;user('username');
  
              $this-&gt;loadModel('UserTypes');
              $userTypes = $this-&gt;UserTypes-&gt;listSubCategories();
  
              $this-&gt;set(compact('userId', 'username', 'userTypes'));
  
          }else if($this-&gt;request-&gt;is('post'))
          {
              Email::configTransport('gmail', [
                  'host' =&gt; 'smtp.gmail.com',
                  'port' =&gt; 587,
                  'username' =&gt; 'xxxxxxxxxxxxx',
                  'password' =&gt; 'xxxxxxxxxxxxx',
                  'className' =&gt; 'Smtp',
                  'tls' =&gt; true
              ]);
  
              $email = new Email();
              $email-&gt;transport('gmail');
              $email-&gt;from(['email@gmail.com' =&gt; 'Store Site'])
                  -&gt;to('email@outlook.com')
                  -&gt;emailFormat('html')
                  -&gt;subject(
                      FormatContactForm::getSubject(
                          $this-&gt;request-&gt;data['subject'],
                          ['suffix' =&gt; ' | Store Site']
                      )
                  )
                  -&gt;send(
                      FormatContactForm::getMessage(
                          $this-&gt;request-&gt;data,
                          ['uppercaseLabel' =&gt; true]
                      )
                  );
  
              return $this-&gt;redirect(['controller' =&gt; 'CustomStaticPages', 'action' =&gt; 'index']);
          }
      }
  
      public function beforeFilter(Event $event)
      {
          $this-&gt;Auth-&gt;allow(['index', 'perguntasFrequentes', 'email', 'politicasDePrivacidade', 'termosDeServico']);
      }
  }
  </code></pre>
  
  <p><strong>routes.php code</strong></p>
  
  <pre><code>Router::scope('/', function ($routes) {
  
      $routes-&gt;connect('/', ['controller' =&gt; 'CustomStaticPages', 'action' =&gt; 'index']);
  
      $routes-&gt;connect('/pages/*', ['controller' =&gt; 'CustomStaticPages', 'action' =&gt; 'index']);
  
      $routes-&gt;connect('/termos-de-servico', ['controller' =&gt; 'CustomStaticPages', 'action' =&gt; 'termosDeServico']);
  
      $routes-&gt;connect('/politicas-de-privacidade', ['controller' =&gt; 'CustomStaticPages', 'action' =&gt; 'politicasDePrivacidade']);
  
      $routes-&gt;connect('/perguntas-frequentes', ['controller' =&gt; 'CustomStaticPages', 'action' =&gt; 'perguntasFrequentes']);
  
      $routes-&gt;connect('/email', ['controller' =&gt; 'CustomStaticPages', 'action' =&gt; 'email']);
  
      $routes-&gt;fallbacks('DashedRoute');
  });
  </code></pre>