【问题标题】:Is there too much code in my view? CakePHP / MVC design pattern我认为有太多代码吗? CakePHP / MVC 设计模式
【发布时间】:2011-07-13 00:46:54
【问题描述】:

我正在使用 dereuromark 的google maps cakePHP helper 在我的视图中显示地图。该地图也有标记,每个标记都有自己的关联侦听器,用于进行 ajax 调用。

地图正在完美渲染,一切都按预期工作。但是,我不确定是否所有这些代码都与我的视图分开?我是 MVC 架构的新手...

<h1>Render map</h1>
<?php

// returns true if user logged in
if (!$session->check('Auth.User.id')){
    echo $this->Html->link('Log in',array('controller'=>'users', 'action' => 'login'));
} else 
{   
    echo "Hello " . $session->read('Auth.User.username') . "... ";
    echo $this->Html->link('Log out',array('controller'=>'users', 'action' => 'logout'));
}
echo $this->GoogleMapV3->map(array('map' => array('lat' => '44.230065', 'lng' => '-76.5000', 'zoom' => 14), 'div'=>array('id'=>'my_map3', 'height' => '400px', 'width' => '786px')));

foreach ($posts as $post) {
    $options = array(
        'lat' => $post['Post']['lat'],
        'lng' => $post['Post']['lng'],
    );

    $marker = $this->GoogleMapV3->addMarker($options);
    $script = "$.ajax({
                          url: \"show_post.php?q=\"+{$post['Post']['id']},
                          success: function(html){
                            $(\"#results\").html(html);
                          }
                        });";
    $this->GoogleMapV3->addCustomEvent($marker,$script);

}

echo $this->GoogleMapV3->script();

?>

如果您还可以指出任何设计缺陷,那就太好了。谢谢

【问题讨论】:

  • 对我来说看起来不错,但正如 jcubic 所说,它不属于这里。再一次,看起来你将是 codereview 上唯一的 cakephp 提交。孤独的地方。

标签: php jquery ajax model-view-controller cakephp


【解决方案1】:

看起来不错,只要您不进行任何模型调用或视图中的太多业务逻辑,您就可以了。您可以将问候语粘贴在一个元素中,然后调用 $this->element('greeting');在顶部而不是 if

【讨论】:

    猜你喜欢
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多