You are viewing a read-only archive of the Blogs.Harvard network. Learn more.

Yii forward with params

Yii’s forward doesn’t work with passing params to actions. I.e. This doesn’t work: $this->forward(“/mycontroller/myaction/1/2”);

Because of the way urlmanager works I made the decision early on to have all action parameters take on the var names $id and $id2. So in the main config:

'components'=>array(
			// uncomment the following to enable URLs in path-format
			'urlManager'=>array(
				'urlFormat'=>'path',
				'rules'=>array(
					'<controller:\w+>/<id:\d+>'=>'<controller>/view',
					'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
					'<controller:\w+>/<action:\w+>/<id:\d+>/<id2:\d+>'=>'<controller>/<action>',
					'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
					'<controller:\w+>/<action:\w+>/<cact:\w+>'=>'<controller>/<cact>',
				),
			),

So I used Controller.php in the components directory to override the forward method like so:

	 /**
	 * Overloading CController::forward
	 * added the routeArr lines so the ids are passed
	 */
	public function forward($route,$exit=true){
		//error_log("Controller::forward");
		$routeArr = explode("/", $route);
		if(isset($routeArr[3]))
			$_GET['id'] = $routeArr[3];
		if(isset($routeArr[4]))
			$_GET['id2'] = $routeArr[4];

		parent::forward($route, $exit);

	}

And that’s it.

Leave a comment