首页  >  文章  >  后端开发  >  关于OpenCart的一个小提议

关于OpenCart的一个小提议

WBOY
WBOY原创
2016-06-13 12:27:57948浏览

关于OpenCart的一个小建议

OpenCart是一套老牌的开源自由B2C电商系统,最近watch了其托管在在github上的项目,发现还是很活跃的,每天都有人反馈bug。

初步的看一下,它是支持在后台管理多个店铺的,而且可以同一个商品在不同店铺有不同的价格;店铺之间可以绑定不同的域名。这是一个比较有特色的地方。

但查看它的代码,就发现多年来改动很小,在代码的分工上,没有进化的感觉。

大量本该由视图来进行构造的数据如语言、链接、当前路径等,都放到控制器中,

像前台登录页面:

<code class="sourceCode php"><span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;breadcrumbs&#39;</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">&#39;breadcrumbs&#39;</span><span class="ot">][]</span> = <span class="fu">array</span><span class="ot">(</span>            <span class="st">&#39;text&#39;</span> => <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;text_home&#39;</span><span class="ot">),</span>            <span class="st">&#39;href&#39;</span> => <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">&#39;common/home&#39;</span><span class="ot">)</span>        <span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;breadcrumbs&#39;</span><span class="ot">][]</span> = <span class="fu">array</span><span class="ot">(</span>            <span class="st">&#39;text&#39;</span> => <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;text_account&#39;</span><span class="ot">),</span>            <span class="st">&#39;href&#39;</span> => <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">&#39;account/account&#39;</span><span class="ot">,</span> <span class="st">&#39;&#39;</span><span class="ot">,</span> <span class="st">&#39;SSL&#39;</span><span class="ot">)</span>        <span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;breadcrumbs&#39;</span><span class="ot">][]</span> = <span class="fu">array</span><span class="ot">(</span>            <span class="st">&#39;text&#39;</span> => <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;text_login&#39;</span><span class="ot">),</span>            <span class="st">&#39;href&#39;</span> => <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">&#39;account/login&#39;</span><span class="ot">,</span> <span class="st">&#39;&#39;</span><span class="ot">,</span> <span class="st">&#39;SSL&#39;</span><span class="ot">)</span>        <span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;heading_title&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;heading_title&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;text_new_customer&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;text_new_customer&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;text_register&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;text_register&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;text_register_account&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;text_register_account&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;text_returning_customer&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;text_returning_customer&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;text_i_am_returning_customer&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;text_i_am_returning_customer&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;text_forgotten&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;text_forgotten&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;entry_email&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;entry_email&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;entry_password&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;entry_password&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;button_continue&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;button_continue&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;button_login&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->language->get<span class="ot">(</span><span class="st">&#39;button_login&#39;</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">&#39;warning&#39;</span><span class="ot">]))</span> {            <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;error_warning&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->error<span class="ot">[</span><span class="st">&#39;warning&#39;</span><span class="ot">];</span>        } <span class="kw">else</span> {            <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;error_warning&#39;</span><span class="ot">]</span> = <span class="st">&#39;&#39;</span><span class="ot">;</span>        }        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;action&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">&#39;account/login&#39;</span><span class="ot">,</span> <span class="st">&#39;&#39;</span><span class="ot">,</span> <span class="st">&#39;SSL&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;register&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">&#39;account/register&#39;</span><span class="ot">,</span> <span class="st">&#39;&#39;</span><span class="ot">,</span> <span class="st">&#39;SSL&#39;</span><span class="ot">);</span>        <span class="kw">$data</span><span class="ot">[</span><span class="st">&#39;forgotten&#39;</span><span class="ot">]</span> = <span class="kw">$this</span>->url-><span class="fu">link</span><span class="ot">(</span><span class="st">&#39;account/forgotten&#39;</span><span class="ot">,</span> <span class="st">&#39;&#39;</span><span class="ot">,</span> <span class="st">&#39;SSL&#39;</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">&#39;language&#39;</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">&#39;url&#39;</span><span class="ot">);</span><span class="co">//加载url对象</span><span class="kw">$this</span>->language<span class="ot">(</span><span class="st">&#39;common/footer&#39;</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">&#39;text_information&#39;</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">[&#39;href&#39;]; ?>"</span>><<span class="ot">?</span>php <span class="fu">echo</span> <span class="kw">$information</span><span class="ot">[</span><span class="st">&#39;title&#39;</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>

这样控制器中就不用再管这种无聊的事情,而集中精力负责业务逻辑和核心数据的调用了。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn