-
Notifications
You must be signed in to change notification settings - Fork 61
Creating a custom controller
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)