CakePHP静态页面(没有实体)但具有动态内容

时间:2015-12-17 14:53:25

标签: cakephp cakephp-3.0

我有几个页面没有与实体连接(索引页面,使用条款页面,电子邮件页面)。

详细说明:在CakePHP中,如果要拥有静态页面(例如索引),则应使用PagesController(未连接到任何实体)。但是我的静态页面具有动态内容作为索引页面(导航栏是动态的(登录时具有用户名,以及特殊按钮)。

1st:为此,我创建了一个CustomStaticPagesController控制器(未连接到任何实体),在他中我创建了方法(索引,电子邮件,termos-de-servico)。 / p>

第二名:我编辑了routes.php对没有标准localhost / controller / action的操作的引用,现在这个localhost / action。

问题:如何改进(正确)上述两点?还有其他要改进的地方吗?

CustomStaticPagesController控制器代码:

<?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代码

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

0 个答案:

没有答案