Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>OpenCart 1.5.X developer quick start guide for beginners</h1> <p>This guide is written for developers already familiar with PHP, OOP and the MVC architecture</p> <p>In the following, you'll see examples for the catalog side of the cart. The admin side is identical in function with the exception of the views which is noted in the relevant section</p> <hr> <h2>Understanding Libraries</h2> <p>All of the library functionality is accessible through Controller, Model and Views using <code>$this-&gt;library_name</code>. All of these can be found in the <code>/system/library/</code> folder. For example, to access the current shopping cart's products, you'll need to use the <code>Cart</code> class, which is in <code>/system/library/cart.php</code> and can be accessed using <code>$this-&gt;cart-&gt;getProducts()</code></p> <p>Commonly used items</p> <ul> <li><code>customer.php</code> - Customer related functions</li> <li><code>user.php</code> - Admin user related functions</li> <li><code>cart.php</code> - Cart related functions</li> <li><code>config.php</code> - All settings are loaded from this</li> <li><code>url.php</code> - URL generation functions</li> </ul> <hr> <h2>Understanding the route parameter</h2> <p>OpenCart's framework relies on the <code>route=aaa/bbb/ccc</code> in the query string parameter to know what to load, and is the underpinning feature to finding the files you need to edit for each page. Most route's actually only use the <code>aaa/bbb</code> which should be seen as two parts, however some contain three parts <code>aaa/bbb/ccc</code> The first part <code>aaa</code> generally related to the folder within a generic folder such as the controller or template folders. The second part usually relates to the file name, without the relevant <code>.php</code> or <code>.tpl</code> extension. The third part is explained in the section "Understanding controllers" below</p> <hr> <h2>Understanding languages</h2> <p>Languages are stored in <code>/catalog/language/</code> folder in the <code>your-language</code> subfolder. Within this, general text values used across various pages are stored in the <code>your-language.php</code> file inside the folder, so for the English language on the catalog side, you'll find the values in <code>catalog/language/english/english.php</code>. For specific page text, you'll need the <code>route</code> for the page (This is generally the case, but not <em>always</em> as you can specify any language file you like). For example, the search page has the route <code>product/search</code>, and therefore the language specific text for that page can be found in <code>catalog/language/english/product/search.php</code> (Notice the file's name and subfolder match the route followed by <code>.php</code>.</p> <p>To load the language in a controller, you use</p> <pre><code>$this-&gt;language-&gt;load('product/search'); </code></pre> <p>Then you can use the language library function <code>get</code> to retrieve specific language texts, such as</p> <pre><code>$some_variable = $this-&gt;language-&gt;get('heading_title'); </code></pre> <p>The language variables are assigned in the language file using a special variable <code>$_</code> which is an array of keys and text values. In your <code>/catalog/language/english/product/search.php</code> you should find something similar to</p> <pre><code>$_['heading_title'] = 'Search'; </code></pre> <p>The values in the global language file <code>english/english.php</code> are automatically loaded and available to use without the <code>$this-&gt;language-&gt;load</code> method</p> <hr> <h2>Understanding controllers</h2> <p>Controllers are loaded based on the <code>route</code> and are fairly straight forward to understand. Controllers are located in the <code>/catalog/controller/</code> folder. Continuing from the last example, the Controller for the Search page is in <code>/product/search.php</code> within this folder. Notice again that the route followed by <code>.php</code> is used.</p> <p>Opening the controller file, you'll see a Pascal Case classname extending the <code>Controller</code> class, called <code>ControllerProductSearch</code>. This again is specific to the route, with <code>Controller</code> followed by the subfolder name and file name without the extension capitalised. The capitalisation is not actually required, but it's recommended for easy readability. It's worth noting that classnames don't take any values from the subfolder and file name other than letters and numbers. Underscores are removed.</p> <p>Within the class are the methods. Methods in the class declared <code>public</code> are accessible to be run via the route - <code>private</code> are not. By default, with a standard two part route (<code>aaa/bbb</code> above), a default <code>index()</code> method is called. If the third part of a route (<code>ccc</code> above) is used, this method will be run instead. For example, <code>account/return/insert</code> will load the <code>/catalog/controller/account/return.php</code> file and class, and try to call the <code>insert</code> method</p> <hr> <h2>Understanding Models</h2> <p>Models in OpenCart are found in the <code>/catalog/model/</code> folder and are grouped based on function, not route, and therefore you will need to load them in your controller via </p> <pre><code>$this-&gt;load-&gt;model('xxx/yyy'); </code></pre> <p>This will load the file in the subfolder <code>xxx</code> called <code>yyy.php</code>. It is then available to use via the object</p> <pre><code>$this-&gt;model_xxx_yyy </code></pre> <p>and as with controllers, you can only call its <code>public</code> methods. For instance, to resize an image, you would use the <code>tool/image</code> model and call its <code>resize</code> method as follows</p> <pre><code>$this-&gt;load-&gt;model('tool/image'); $this-&gt;model_tool_image-&gt;resize('image.png', 300, 200); </code></pre> <hr> <h2>Understanding variable assignment in views from the controller</h2> <p>In order to pass values to the view from the controller, you simply need to assign your data to the <code>$this-&gt;data</code> variable, which is essentially an array of key => value pairs. As an example</p> <pre><code>$this-&gt;data['example_var'] = 123; </code></pre> <p>Accessing this in a view is a little should be easy to understand if you're familiar with the <a href="http://php.net/extract" rel="noreferrer">extract()</a> method which converts each key into a variable. So the <code>example_var</code> key becomes <code>$example_var</code> and can be accessed as such in the view.</p> <hr> <h2>Understanding themes</h2> <p>Themes are available to the catalog side only, and are basically a folder of templates, stylesheets and theme images. Theme folders are placed in the <code>/catalog/view/theme/</code> folder followed by the theme name. The folder name isn't of importance with exception to the <code>default</code> folder</p> <p>The admin side uses <code>/admin/view/template/</code> (skipping the <code>/theme/theme-name/</code> from the path as it doesn't allow differing themes)</p> <p>Template files reside in a <code>template</code> folder within the theme folder. Should any template not be available for the currently selected theme, the default folder's template is used instead as a fallback. This means themes can be created with very few files and still function fully. It also reduces code duplication and issues as upgrades are made</p> <hr> <h2>Understanding views (templates)</h2> <p>As with language and models, the view file's are generally related to the route, though don't have to be at all. Templates on the catalog side are usually found in <code>/catalog/view/theme/your-theme/template/</code> unless it doesn't exist, in which case the default theme's templates will be used. For our search page example above, the file is <code>product/search.tpl</code>. For routes with three parts, it is generally in <code>aaa/bbb_ccc.tpl</code> though there's no hard set rule. In the admin, most pages follow this, with the exception that pages listing items, like the product listing page, are in <code>catalog/product_list.tpl</code> and the product editing form is in <code>catalog/product_form.tpl</code>. Again, these aren't set, but a standard for the default cart.</p> <p>The template file is in fact just another php file, but with a .tpl extension and is actually run in the controller file, therefore all of the things you can code in a controller can be run in a template file (though not recommended unless absolutely necessary)</p> <hr> <h2>Understanding the database object</h2> <p>Queries are run using</p> <pre><code>$result = $this-&gt;db-&gt;query("SELECT * FROM `" . DB_PREFIX . "table`"); </code></pre> <p><code>DB_PREFIX</code> as the name suggests is a constant containing the database prefix if one exists</p> <p><code>$result</code> will return an object for <code>SELECT</code> queries, containing a few properties</p> <p><code>$result-&gt;row</code> contains the first row's data if one or more are returned as an associative array</p> <p><code>$result-&gt;rows</code> contains an array of row results, ideal for looping over using foreach</p> <p><code>$result-&gt;num_rows</code> contains the number of results returned</p> <p>There are also a few extra methods the <code>$this-&gt;db</code> object has</p> <p><code>$this-&gt;db-&gt;escape()</code> uses <a href="http://php.net/mysql_real_escape_string" rel="noreferrer">mysql_real_escape_string()</a> on the value passed</p> <p><code>$this-&gt;db-&gt;countAffected</code> returns the number of rows affected by an <code>UPDATE</code> query and so on</p> <p><code>$this-&gt;db-&gt;getLastId()</code> returns the last auto increment id using <a href="http://php.net/mysql_insert_id" rel="noreferrer">mysql_insert_id()</a></p> <hr> <h2>Understanding reserved variables</h2> <p>OpenCart has predefined variables to use in place of the standard <code>$_GET</code>, <code>$_POST</code>, <code>$_SESSION</code>, <code>$_COOKIE</code>, <code>$_FILES</code>, <code>$_REQUEST</code> AND <code>$_SERVER</code></p> <p><code>$_SESSION</code> is edited using <code>$this-&gt;session-&gt;data</code> where data is an associative array mimicking the <code>$_SESSION</code></p> <p>All of the others can be accessed using <code>$this-&gt;request</code> and have been "cleaned" to comply with magic quotes enabled/disabled, so</p> <p><code>$_GET</code> becomes <code>$this-&gt;request-&gt;get</code></p> <p><code>$_POST</code> becomes <code>$this-&gt;request-&gt;post</code></p> <p><code>$_COOKIE</code> becomes <code>$this-&gt;request-&gt;cookie</code></p> <p><code>$_FILES</code> becomes <code>$this-&gt;request-&gt;files</code></p> <p><code>$_REQUEST</code> becomes <code>$this-&gt;request-&gt;request</code></p> <p><code>$_SERVER</code> becomes <code>$this-&gt;request-&gt;server</code></p> <hr> <h2>Summary</h2> <p>While the above isn't a bulletproof guide for developers, hopefully it will serve as a good starting point for those getting started</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload