Cakephp makes "action" and "template" more concise

Asked

Viewed 117 times

1

I have some questions about how I can improve this "action" (method) in my "controller":

Question discussed in: Post functional code in stackoverflow for refactoring?

  1. Man template has a navigation bar with dynamic content (if the user is "logged in" a special button appears, his name appears in the navigation bar among other customizations) also the insertion of "modals" is dynamic, depending on whether the user is logged in or not, my controller calls some methods that will check if the user is "logged in" and "set" the data to the template.

  2. I am entering the code that searches the database (Query Builder) directly in the "action" (method) of the controller.

  3. I’m using a comment line like this "//--------------------------" to separate concepts and operations. This is correct?

  4. I’m using a full "Template" for each "action" (method) (e.g.: A complete template for the action (method) add controller’s Products(Note: Most pages (add, edit, delete, index) use different. js and . css files).

Controller:

<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;

class StoresController extends AppController
{
     public function miniMap()
    {
        $setting = [
            'fields' => ['id', 'banner_description', 'path_banner', 'url_redirect'],
            'conditions' => ['banner_type_id' => 2],
            'limit' => 1
        ];
        $fullBanners = TableRegistry::get('Banners')
            ->find('all', $setting)->hydrate(false)->toArray();
        $this->set('fullBanners', $fullBanners);

        //-------------------------------------------------------------------------

        $setting = [
            'fields' => ['id', 'banner_description', 'path_banner', 'url_redirect'],
            'conditions' => ['banner_type_id' => 1],
            'limit' => 3
        ];
        $smallBanners = TableRegistry::get('Banners')
            ->find('all', $setting)->hydrate(false)->toArray();
        $this->set('smallBanners', $smallBanners);

        //-------------------------------------------------------------------------

        $this->set('userId', $this->Auth->user('id'));

        //-------------------------------------------------------------------------

        $this->set('username', $this->Auth->user('username'));
    }
}

Template:

<?php    
    $this->layout = false;
?>
<!DOCTYPE html>
<html>
    <head>
        <?= $this->Html->charset() ?>
        <?= $this->Html->meta('viewport','width=device-width, initial-scale=1.0') ?>
        <?= $this->Html->meta('title',$pageTitle) ?>
        <?= $this->Html->meta('favicon.ico','/cart.png', ['type' => 'icon']) ?>
        <?= $this->Html->meta('keywords','') ?>
        <?= $this->Html->meta('description','') ?>
        <?= $this->Html->meta('robots','index,follow') ?>

        <?= $this->Html->css('library/datepicker/css/datepicker.css') ?>
        <?= $this->Html->css('library/bxslider-4-4.1.2/jquery.bxslider.css') ?>
        <?= $this->Shrink->css(['styles/style.css', 'styles/menu-plugin.css']) ?>

        <?= $this->Html->script('library/bxslider-4-4.1.2/jquery.bxslider.min.js',['defer' => true]) ?>
        <?= $this->Html->script('actions/main.js',['defer' => true]) ?>

        <?= $this->Shrink->fetch('css') ?>
    </head>
    <body>
        <?= $this->element('Navbar/navbar_main') ?>
        <div class="wrapper">
            <div class="container">
                <div class="row">
                    <?= $this->element('Body/categories') ?>
                    <?= $this->element('Body/stores') ?>
                </div>
            </div>
        </div>
        <?= $this->element('Footer/footer_information') ?>
        <?php if ($userId == false): ?>
            <?= $this->element('Modal/create_account_modal') ?>
            <?= $this->element('Modal/login_modal') ?>
        <?php else: ?>
            <?= $this->element('Modal/logout_modal') ?>
        <?php endif; ?>
    </body>
</html>
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.