OpenCart についての小さな提案
OpenCart は古いオープンソースの無料 B2C 電子商取引システムです。最近、github でホストされているそのプロジェクトを観察しましたが、今でも非常に活発で、毎日バグが報告されていることがわかりました。
予備的な確認では、バックグラウンドで複数のストアの管理をサポートしており、同じ商品でも異なるストアでは異なる価格を設定でき、ストア間で異なるドメイン名をバインドできることがわかります。ここはかなりユニークな場所です。
しかし、そのコードを見ると、何年にもわたってほとんど変更されておらず、コードの役割分担に進化の感覚がないことがわかりました。
言語、リンク、現在のパスなど、ビューによって構築されるべき大量のデータは、
フロントエンドのログイン ページと同様に、コントローラーに配置されます。 :
<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>
このようにして、コントローラーはそのような退屈なことを心配する必要がなくなり、ビジネスに集中できますロジックとコア データと呼ばれます。