Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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');
});
share|improve this question
    
Every question on Code Review needs at least a language tag, which is, in this case, php. – 200_success Dec 18 '15 at 1:24

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.

share|improve this answer

For Index page, Navigations and other widgets use Cells http://book.cakephp.org/3.0/en/views/cells.html

share|improve this answer
1  
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. – SuperBiasedMan Dec 17 '15 at 15:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.