Yii Framework Forum: has like getRoute() function? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

has like getRoute() function? Rate Topic: -----

#1 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 04 December 2009 - 03:02 AM

sometime, i want to get the current route without additional parameters~

like in action:

echo $this->route; <-- site/misc

echo $this->route; <-- mymodule/product/list ( in module )

BTW:

sometime, will want through route to get more information for moduleId, controllerId, and actionId

this function will look like:

$routeResource = Yii::app()->urlManager->parseRoute('user/update');

$routeResource will have:

moduleId => null,
controllerId => user,
actionId => update,

0

#2 User is offline   jerry2801 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 504
  • Joined: 18-November 09

Posted 13 December 2009 - 08:56 AM

qiang?
0

#3 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 13 December 2009 - 04:00 PM

View Postjerry2801, on 13 December 2009 - 08:56 AM, said:

qiang?


I needed something like this so i created a BaseController then extended from it a FrontendController then from that i extended all my front end controllers. in my BaseController i have code like this
<?php

class BaseController extends CController
{
	public function init()
	{
		parent::init();
	}
	
	public function beforeAction($action)
	{
		return parent::beforeAction($action);
	}
	
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image
			// this is used by the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'transparent'=>true,
			),
		);
	}
	
	public function routeIsActive($pattern,$controllerID,$actionID)
	{
		if(!is_array($pattern) || !isset($pattern[0]))
			return false;

		$pattern[0]=trim($pattern[0],'/');
		if(strpos($pattern[0],'/')!==false)
			$matched=$pattern[0]===$controllerID.'/'.$actionID;
		else
			$matched=$pattern[0]===$controllerID;

		if($matched && count($pattern)>1)
		{
			foreach(array_splice($pattern,1) as $name=>$value)
			{
				if(!isset($_GET[$name]) || $_GET[$name]!=$value)
					return false;
			}
			return true;
		}
		else
			return $matched;
	}
}


following that, to achieve what u want you can add a method eg: getActiveRoute

	public function getActiveRoute()
	{
		$controller = $this->uniqueID;
		$action = $this->action->id;
		return $route = $controller.'/'.$action;
	}

Then in all your controllers/views/widgets you can get the active route by calling $controllerInstance->getActiveRoute();

I figure it can be modified to deal with nested modules.

where $controllerInstance is the instance of your controller.
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users