Webocreation

Saturday, April 16, 2011

What is a Controller in Opencart?






What is a Controller in Opencart?

A OpenCart Controller is simply a class file that is named in a way that can be associated with a URI.
Consider this URI:
rupaknepali.com/index.php?route=account/account
In the above example, Opencart would attempt to find a controller named account.php in account folder and load it. Your file will be included in the
catalog/controller/account/account.php
rupaknepali.com/ admin/index.php?route=sale/customer
Here the file loaded will be:
admin/controller/sale/customer.php
When a controller's name matches the first segment of a URI, it will be loaded.

Let's try it:  Hello World!

Let's create a simple controller so you can see it in action. Using your text editor, create a file called hello.php, and put the following code in it:
Then save the file to your catalog/controllers/account/ folder.
Now visit your site using a URL similar to this:
rupaknepali.com/index.php?route=account/hello
If you did it right, you should see Hello World!.
Note: Class names must start with an uppercase letter. In other words, this is valid:

class ControllerAccountHello extends Controller {

}
?>
This may not be valid:

class controllerAccountHelloextends Controller {
}
?>
Also, always make sure your controller extends the parent controller class so that it can inherit all its functions.

Functions

In the above example the function name is index(). The "index" function is always loaded by default if the second segment of the URI is empty. Another way to show your "Hello World" message would be this:
rupaknepali.com/index.php?route=account/hello/index
The second segment of the URI determines which function in the controller gets called.
Let's try it. Add a new function to your controller:
Now load the following URL to see the comment function:
rupaknepali.com/index.php?route=account/hello/comments
You should see your new message.

example in opencart, controller example in the opencart, learn opencart by example, how controllers work in opencart?,what is controller in opencart,opwncart

1 comment: