Yii Framework Forum: How to check if a controller exists - Yii Framework Forum

Jump to content

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

How to check if a controller exists Rate Topic: -----

#1 User is offline   Shiki 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 12-November 09

Posted 22 November 2009 - 11:26 PM

Hi,

Does anyone have an idea on how to check if a controller exists based on a name? I'm working on a custom Url manager and I need to check first whether a route is a controller name (ie value is "site" > points to SiteController). So I'm trying to figure out a fast way to check if there is a SiteController class. I don't think I could use class_exists() though because the controller class probably hasn't been loaded yet. Right now I'm using CWebApplication::createController() which may not be a good idea.

Thanks
0

#2 User is offline   datashaman 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 56
  • Joined: 27-September 09
  • Location:Cape Town, South Africa

Posted 23 November 2009 - 02:02 AM

View PostShiki, on 22 November 2009 - 11:26 PM, said:

Hi,

Does anyone have an idea on how to check if a controller exists based on a name? I'm working on a custom Url manager and I need to check first whether a route is a controller name (ie value is "site" > points to SiteController). So I'm trying to figure out a fast way to check if there is a SiteController class. I don't think I could use class_exists() though because the controller class probably hasn't been loaded yet. Right now I'm using CWebApplication::createController() which may not be a good idea.

Thanks


file_exists() ?
0

#3 User is offline   Pentium10 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 174
  • Joined: 10-December 10

Posted 24 January 2013 - 02:35 AM

Were you able to find a solution, I am looking for the exact same thing?
Can you share your working code?
0

#4 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 24 January 2013 - 01:44 PM

Dear Friend

We can do it in the following way.

function isControllerExists($id) //$id is controllerID eg)post,user
{
	$i=ucfirst($id);
	$controllerPath=Yii::app()->getControllerPath();
	return file_exists($controllerPath.DIRECTORY_SEPARATOR.$i."Controller.php");//returns boolean
	
}

echo isControllerExists('user');//return 1 if there is UserController.php in controller folder.


Regards.
0

#5 User is offline   Pentium10 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 174
  • Joined: 10-December 10

Posted 24 January 2013 - 01:46 PM

this sounds to me that doesn't resolve modules
0

#6 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 24 January 2013 - 02:51 PM

Dear Friend

Declare the following function in Controller.php (From which all the controllers are derived).

public function isControllerExists($id)
{
	$id=ucfirst($id);
	$controllerPath=($this->module==null)?Yii::app()->controllerPath:$this->module->controllerPath;
	return file_exists($controllerPath.DIRECTORY_SEPARATOR.$id."Controller.php");

}


Now anywhere in your application
You can do it.
echo $this->isControllerExists("default");


If it is a module it looks into the controller folder inside the module.Else it looks into main application controller folder.

Regards.
0

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