首頁  >  文章  >  後端開發  >  關於OpenCart的一個小建議

關於OpenCart的一個小建議

WBOY
WBOY原創
2016-07-29 09:10:251063瀏覽

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 <span>class</span>=<span>"container"</span>>
    <div <span>class</span>=<span>"row"</span>>
      <<span>?</span>php <span>if</span><span>(</span><span>$informations</span><span>)</span> { <span>?></span>
      <div <span>class</span>=<span>"col-sm-3"</span>>
        <h5><<span>?</span>php <span>echo</span><span>$lang</span>->get<span>(</span><span>'text_information'</span><span>);</span><span>?></span></h5>
        <ul <span>class</span>=<span>"list-unstyled"</span>>
          <<span>?</span>php <span>foreach</span><span>(</span><span>$informations</span><span>as</span><span>$information</span><span>)</span> { <span>?>
          <li><a href=<span>"<?php echo </span><span>$information</span><span>[&#39;href&#39;]; ?>"><<span>?</span>php <span>echo</span><span>$information</span><span>[</span><span>&#39;title&#39;</span><span>];</span><span>?></a></li>
          <<span>?</span>php } <span>?>
        </ul>
      </div>
      <<span>?</span>php } <span>?></code>

這樣控制器中就不用再管這種無聊的事情,而集中精力負責業務邏輯和核心資料的調用了。

以上就介紹了關於OpenCart的一個小建議,包括了github方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn