Home >Backend Development >PHP Tutorial >A little suggestion about OpenCart
OpenCart is an old open source free B2C e-commerce system. I recently watched its project hosted on github and found that it is still very active, with people reporting bugs every day.
At a preliminary look, it supports the management of multiple stores in the background, and the same product can have different prices in different stores; different domain names can be bound between stores. This is a rather unique place.
But looking at its code, I found that there have been very few changes over the years, and there is no sense of evolution in the division of labor in the code.
A large amount of data that should be constructed by the view, such as language, link, current path, etc., are placed in the controller,
Like the front-end login page:
<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>
It is quite cumbersome, in fact, under the existing framework features , which can be placed in the view file.
At the head of the view file:
<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>['href']; ?>"><<span>?</span>php <span>echo</span><span>$information</span><span>[</span><span>'title'</span><span>];</span><span>?></a></li> <<span>?</span>php } <span>?> </ul> </div> <<span>?</span>php } <span>?></code>
This way the controller no longer has to worry about such boring things, and can concentrate on calling business logic and core data.
The above introduces a small suggestion about OpenCart, including github content. I hope it will be helpful to friends who are interested in PHP tutorials.