ReflectionClass::getFileName()

Hi

Does Yii put somewhere the actual file of a CComponent-derived class, so I won’t have to go through the hassle of using ReflectionClass::getFileName()?

In my particular case, I’m having a BaseController which is the base class for all of my controllers across all the modules. I won’t go through explaining all this, but I need to know the path to the extended controller within BaseController itself.

Right now I’m using ReflectionClass($this)->getFileName(), as I said above, which works, but it would be overkill if Yii already keeps track of that information somewhere.

What I need this for? (in case there might be a better, more elegant solution)

Uhm in the base class I need to do some nasty “smart” decisions about where to look for the viewPath. It’s not complicated, but not easy either. Practically I’m injecting a controller of a module (to which it belongs to from the filesystem perspective) into another module (via controllerMap), but I want that controller (each BaseController-derived class) to look for views inside it’s logical parent module from the filesystem, not what CController::$module holds.

Please ask for clarification if something is not understandable. However try to re-read the explanations above first while not forgetting it’s not the “usual” use case of controllerMap. I’m trying to only halfly get the controller into a module, with one foot into to module it doesn’t belong to (which works great with controllerMap), while the other (the render() & co functions) foot still makes the controller look for views into the FS directory relative to the original file path, i.e:


dirname($refl->getFileName()).'/../views/'

Short said: I’m looking for something like basePath, but for CControllers (or better, CComponents).

Thanks

I’ve found it out, Yii provides this via CExtController, which is great.

Anyway since I need a slightly different directory layout, I’ll be duplicating almost everything as it’s done in that class.

EDIT

Actually I’m still working on it. Somehow it doesn’t find the view file.

Done. It was tricky to get it running. Not hard, just tricky

Here’s the snippet:


<?php

class Controller extends CController {

	private $_viewPath;

	protected $_defaultViewPath;

	protected $_defaultId;


	public function getViewPath() {

		if(NULL === $this->_viewPath) {

			$this->_viewPath = realpath($this->_defaultViewPath);

			if(FALSE === $this->_viewPath) {

				throw new Exception('Path "'.$path.'" is not accesible');

			}

		}

		return $this->_viewPath;

	}

	public function setViewPath($path) {

		$this->_viewPath = $path;

	}

	public function init() {

		parent::init();

		$class = new ReflectionClass($this);

		preg_match('/(.+)Controller/',get_class($this),$id);

		$id = lcfirst($id[1]);

		$this->_defaultViewPath = dirname($class->getFileName()).'/../views/'.$id;

		$this->_defaultId = $id;

	}

	public function run($actionID) {

		if(!method_exists($this,'action'.ucfirst($actionID))) {

			throw new Exception("Action $actionID doesn't exist");

		}

		return parent::run($actionID);

	}

}

The code is dirty, I know. It’s in the prototyping phase. Anyway do you see any mistakes, beside those inherent to the “dirtiness”?