<?php
class TestController extends CController
{
/**
* @var string the default layout for the views. Defaults to ‘column2’, meaning
* using two-column layout. See ‘protected/views/layouts/column2.php’.
*/
//public $layout = ‘application.views.layouts.main’;
/**
* @var CActiveRecord the currently loaded data model instance.
*/
private $_model;
protected function beforeAction($action)
{
// $this->layout = Yii::app()->settings->get(‘theme’, ‘layoutMain’, ‘application.views.layouts.main’);
// $_SESSION[“theme-layoutMain-” . Yii::app()->controller->id] = $this->layout;
// Dynamically include all action files from the OrdersController folder
return true;
}
/**
* @return array action filters
*/
public function filters()
{
return array(
‘accessControl’, // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the ‘accessControl’ filter.
* @return array access control rules
*/
public function accessRules()
{
return array(/*
array(‘allow’, // allow authenticated user to perform ‘create’ and ‘update’ actions
‘users’=>array(‘@’),
),
array(‘deny’ ),
array(‘allow’, // allow all users to perform ‘index’ and ‘view’ actions
‘actions’=>array(‘index’,’view’),
‘users’=>array(‘*’),
),
array(‘allow’, // allow authenticated user to perform ‘create’ and ‘update’ actions
‘actions’=>array(‘create’,’update’),
‘users’=>array(‘@’),
),
array(‘allow’, // allow admin user to perform ‘admin’ and ‘delete’ actions
‘actions’=>array(‘admin’,’delete’),
‘users’=>array(‘admin’),
),
array(‘deny’, // deny all users
‘users’=>array(‘*’),
), .
*/
);
}
public function actions()
{
$controllerName = str_replace(‘Controller’, ”, get_class($this));
$cacheKey = strtolower($controllerName) . ‘_actions’;
$cache = Yii::app()->cache;
$actions = $cache->get($cacheKey);
if ($actions === false) {
$actions = array();
$basePath = Yii::getPathOfAlias(‘application.controllers.actions.’ . $controllerName);
foreach (glob($basePath . ‘/*.php’) as $file) {
$className = basename($file, ‘.php’);
$actionId = str_replace(‘Action’, ”, $className);
$actions[strtolower($actionId)] =
‘application.controllers.actions.’ . $controllerName . ‘.’ . $className;
}
$cache->set($cacheKey, $actions, 3600, new CDirectoryCacheDependency($basePath));
}
return $actions;
}
public function createAction($actionID)
{
$actions = $this->actions();
$lookupID = strtolower($actionID);
if (isset($actions[$lookupID])) {
$classPath = $actions[$lookupID];
$className = substr($classPath, strrpos($classPath, ‘.’) + 1);
Yii::import($classPath);
return new CaseInsensitiveAction($this, $actionID, $className);
}
return parent::createAction($actionID);
}
public function functions()
{
$controllerName = str_replace(‘Controller’, ”, get_class($this));
$cacheKey = strtolower($controllerName) . ‘_functions’;
$cache = Yii::app()->cache;
$functions = $cache->get($cacheKey);
if ($functions === false) {
$functions = array();
$basePath = Yii::getPathOfAlias(‘application.controllers.functions.’ . $controllerName);
foreach (glob($basePath . ‘/*.php’) as $file) {
$className = basename($file, ‘.php’);
// Always store lowercase key
$functions[strtolower($className)] =
‘application.controllers.functions.’ . $controllerName . ‘.’ . $className;
}
$cache->set($cacheKey, $functions, 3600, new CDirectoryCacheDependency($basePath));
}
return $functions;
}
public function runFunction($name, $params = array())
{
static $loaded = array(); // memory-level cache
$functions = $this->functions();
$lookupName = strtolower($name);
if (!isset($functions[$lookupName])) {
throw new CException(“Function ‘$name’ not mapped.”);
}
$alias = $functions[$lookupName];
$classPath = Yii::getPathOfAlias($alias) . ‘.php’;
if (!file_exists($classPath)) {
throw new CException(“Function file not found: $classPath”);
}
$cacheKey = ‘function_class_’ . md5($classPath);
$fileModified = filemtime($classPath);
$cached = Yii::app()->cache->get($cacheKey);
if ($cached && $cached[‘mtime’] === $fileModified) {
$className = $cached[‘class’];
if (!class_exists($className, false)) {
include_once($classPath);
}
} else {
include_once($classPath);
$className = basename($classPath, ‘.php’);
Yii::app()->cache->set($cacheKey, array(‘class’ => $className, ‘mtime’ => $fileModified), 3600);
}
// ✅ Instantiate and assign controller
$obj = new $className();
if (property_exists($obj, ‘controller’)) {
$obj->controller = $this; // 🔥 assign controller reference
}
return call_user_func_array(array($obj, ‘run’), $params);
}
}