The OpenCart Router class takes a route string and converts it to an internal representation of a class and a method, typically for invoking a controller.
The Router class is specialized to the directory structure of OpenCart and the naming conventions for OpenCart controller files and class; therefore, before discussing the Router class in detail, it is important to understand the directory structure and naming conventions that OpenCart uses for controllers, controller directories, and controller files.
Controller Directory Structure
In OpenCart, catalog controllers are all located under DIR_APPLICATION/catalog/controller, where DIR_APPLICATION is the path to your application directory, and is defined in the OpenCart config.php file.
More specifically, files containing definitions of controller classes are contained in the following directories:
- DIR_APPLICATION/catalog/controller/account
- DIR_APPLICATION/catalog/controller/checkout
- DIR_APPLICATION/catalog/controller/common
- DIR_APPLICATION/catalog/controller/error
- DIR_APPLICATION/catalog/controller/feed
- DIR_APPLICATION/catalog/controller/information
- DIR_APPLICATION/catalog/controller/module
- DIR_APPLICATION/catalog/controller/payment
- DIR_APPLICATION/catalog/controller/product
As an example, the following files are contained in DIR_APPLICATION/catalog/controller/account:
- DIR_APPLICATION/catalog/controller/checkout/address.php
- DIR_APPLICATION/catalog/controller/checkout/cart.php
- DIR_APPLICATION/catalog/controller/checkout/confirm.php
- DIR_APPLICATION/catalog/controller/checkout/guest.php
- DIR_APPLICATION/catalog/controller/checkout/payment.php
- DIR_APPLICATION/catalog/controller/checkout/shipping.php
- DIR_APPLICATION/catalog/controller/checkout/success.php
Controller Class Naming Conventions
We see from the previous section how the controllers directories are laid out, but how are the controller classes named?
It is very straightforward. Each controller class name is built from the last two directory names and the file name of its path name.
For example, the file DIR_APPLICATION/catalog/controller/checkout/address.php contains the class ControllerCheckoutAddress.
Similarly, the file DIR_APPLICATION/catalog/controller/account/address.php contains the class ControllerAccountAddress.
How the Router class works
Now that we understand the naming conventions for controller classes and files, let’s look at how the Router class works.
The Router constructor accepts a required string parameter, route, and an optional array parameter, args.
The constructor creates three class scope variables, $class, $method, $args; which are made available through the following class methods:
- getClass
- getMethod
- getArgs
Here is what the Router does. If takes the route string
1 | $router = new Router("account/address/somemethod"); |
The classvariable will be set to “account/address” and the method variable will be set to “somemethod”.
It may seem odd that the class maintains the slash (”/”), but that’s how it works, and later when the class name is actually used to instantiate an object, the slashes are removed first.
This may seem like much ado about nothing, but that’s how it works. Later, after the $route variable is created in the Registry, it can be fished out and used instantiate the correct controller; in this case, the ControllerAccountAddress class located in the DIR_APPLICATION/catalog/controller/account/address.php file.
© 2009, Sam Mela. All rights reserved.
Hi Sam. Thanks for this article. Really useful. You should add this to the OpenCart Wiki for reference. It is really empty there.
Just a question. I want to create a new layout to display a list of retailers. I have created a new php file in /controller/common/retailers.php and the layout that goes with it in /view/theme/default/template/common/retailers.tpl
As I understand, my class should be as followed:
class ControllerCommonRetailers extends Controller { ... }
.
However, I get “Page not found” in my browser. Do I have to create the router as well and, if so, where? in index.php? I am not clear on that.