I have some questions about how I can improve this "action" in my "controller":
Question discussed in: Post functional code in stackoverflow for refactoring?
My template has a navigation bar with dynamic content (if the user is "logged in" a special button appears, their name appears in the navigation bar among other customizations) of the "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 for the template.
I am inserting the code that searches the database (Query Builder) directly into the controller's "action".
I'm using a comment line like this " //--------------------------
" to separate concepts and operations. Is this correct?
I'm using a complete "Template" for each "action" (for example: A complete template for the action add
of controller 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>