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