Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Creating a custom controller

Kris Thom White edited this page Jul 18, 2013 · 12 revisions

To create a standalone custom controller for Web Store 3, use the following directions.

First, create your custom controller file in /custom/controllers using the naming convention XXXXXXController.php for both the filename and the classname inside. Here is the file for SampleController.php

<?php

class SampleController extends Controller
{
    public function actionIndex()
    {
        echo "this is the default index function";
    }

    public function actionTest()
    {

        echo "This is the test function";
    }

}

This file is saved as /custom/controllers/SampleController.php

Next, we need to tell Yii that this controller exists, and we do this by modifying our /config/main.php file and adjusting (or enabling) the ControllerMap.

In /config/main.php locate lines 79-83 and uncomment the lines, and update them to read

'controllerMap'=>array(
        'sample'=>array(
            'class'=>'custom.controllers.SampleController',
        ),
    ),

This lets the Yii framework find the controller sample and the correct file it references.

To test our function, we can use URLs such as

http://www.example.com/sample (this runs the actionIndex function)

http://www.example.com/sample/test (this runs the actionTest function)

Clone this wiki locally