关于OpenCart的一个小建议
OpenCart是一套老牌的开源自由B2C电商系统,最近watch了其托管在在github上的项目,发现还是很活跃的,每天都有人反馈bug。
初步的看一下,它是支持在后台管理多个店铺的,而且可以同一个商品在不同店铺有不同的价格;店铺之间可以绑定不同的域名。这是一个比较有特色的地方。
但查看它的代码,就发现多年来改动很小,在代码的分工上,没有进化的感觉。
大量本该由视图来进行构造的数据如语言、链接、当前路径等,都放到控制器中,
像前台登录页面:
<code class="sourceCode php"><span class="kw">$data</span><span class="ot">[</span><span class="st">'breadcrumbs'</span><span class="ot">]</span> = <span class="fu">array</span><span class="ot">();</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'breadcrumbs'</span><span class="ot">][]</span> = <span class="fu">array</span><span class="ot">(</span> <span class="st">'text'</span> => <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'text_home'</span><span class="ot">),</span> <span class="st">'href'</span> => <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">'common/home'</span><span class="ot">)</span> <span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'breadcrumbs'</span><span class="ot">][]</span> = <span class="fu">array</span><span class="ot">(</span> <span class="st">'text'</span> => <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'text_account'</span><span class="ot">),</span> <span class="st">'href'</span> => <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">'account/account'</span><span class="ot">,</span> <span class="st">''</span><span class="ot">,</span> <span class="st">'SSL'</span><span class="ot">)</span> <span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'breadcrumbs'</span><span class="ot">][]</span> = <span class="fu">array</span><span class="ot">(</span> <span class="st">'text'</span> => <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'text_login'</span><span class="ot">),</span> <span class="st">'href'</span> => <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">'account/login'</span><span class="ot">,</span> <span class="st">''</span><span class="ot">,</span> <span class="st">'SSL'</span><span class="ot">)</span> <span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'heading_title'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'heading_title'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'text_new_customer'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'text_new_customer'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'text_register'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'text_register'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'text_register_account'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'text_register_account'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'text_returning_customer'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'text_returning_customer'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'text_i_am_returning_customer'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'text_i_am_returning_customer'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'text_forgotten'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'text_forgotten'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'entry_email'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'entry_email'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'entry_password'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'entry_password'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'button_continue'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'button_continue'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'button_login'</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">'button_login'</span><span class="ot">);</span> <span class="kw">if</span> <span class="ot">(</span><span class="fu">isset</span><span class="ot">(</span><span class="kw">$this</span>->error<span class="ot">[</span><span class="st">'warning'</span><span class="ot">]))</span> { <span class="kw">$data</span><span class="ot">[</span><span class="st">'error_warning'</span><span class="ot">]</span> = <span class="kw">$this</span>->error<span class="ot">[</span><span class="st">'warning'</span><span class="ot">];</span> } <span class="kw">else</span> { <span class="kw">$data</span><span class="ot">[</span><span class="st">'error_warning'</span><span class="ot">]</span> = <span class="st">''</span><span class="ot">;</span> } <span class="kw">$data</span><span class="ot">[</span><span class="st">'action'</span><span class="ot">]</span> = <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">'account/login'</span><span class="ot">,</span> <span class="st">''</span><span class="ot">,</span> <span class="st">'SSL'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'register'</span><span class="ot">]</span> = <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">'account/register'</span><span class="ot">,</span> <span class="st">''</span><span class="ot">,</span> <span class="st">'SSL'</span><span class="ot">);</span> <span class="kw">$data</span><span class="ot">[</span><span class="st">'forgotten'</span><span class="ot">]</span> = <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">'account/forgotten'</span><span class="ot">,</span> <span class="st">''</span><span class="ot">,</span> <span class="st">'SSL'</span><span class="ot">);</span></code>
相当累赘,其实在现有框架特性下,完全可以放到视图文件中去的。
在视图文件的头部:
<code class="sourceCode php"><span class="kw"><?php</span><span class="kw">$lang</span> = <span class="kw">$this</span>->registry->get<span class="ot">(</span><span class="st">'language'</span><span class="ot">);</span><span class="co">//加载语言包管理对象</span><span class="kw">$url</span> = <span class="kw">$this</span>->registry->get<span class="ot">(</span><span class="st">'url'</span><span class="ot">);</span><span class="co">//加载url对象</span><span class="kw">$this</span>->language<span class="ot">(</span><span class="st">'common/footer'</span><span class="ot">);</span><span class="co">//载入语言包</span><span class="kw">?></span><footer> <div <span class="kw">class</span>=<span class="st">"container"</span>> <div <span class="kw">class</span>=<span class="st">"row"</span>> <<span class="ot">?</span>php <span class="kw">if</span> <span class="ot">(</span><span class="kw">$informations</span><span class="ot">)</span> { <span class="kw">?></span> <div <span class="kw">class</span>=<span class="st">"col-sm-3"</span>> <h5><<span class="ot">?</span>php <span class="fu">echo</span> <span class="kw">$lang</span>->get<span class="ot">(</span><span class="st">'text_information'</span><span class="ot">);</span> <span class="kw">?></span></h5> <ul <span class="kw">class</span>=<span class="st">"list-unstyled"</span>> <<span class="ot">?</span>php <span class="kw">foreach</span> <span class="ot">(</span><span class="kw">$informations</span> <span class="kw">as</span> <span class="kw">$information</span><span class="ot">)</span> { <span class="kw">?></span> <li><a href=<span class="st">"<?php echo </span><span class="kw">$information</span><span class="st">['href']; ?>"</span>><<span class="ot">?</span>php <span class="fu">echo</span> <span class="kw">$information</span><span class="ot">[</span><span class="st">'title'</span><span class="ot">];</span> <span class="kw">?></span></a></li> <<span class="ot">?</span>php } <span class="kw">?></span> </ul> </div> <<span class="ot">?</span>php } <span class="kw">?></span></code>
这样控制器中就不用再管这种无聊的事情,而集中精力负责业务逻辑和核心数据的调用了。