Dear Friend
I want to share something with you on this topic.
There are two approaches I have employed here.
- In the first approach, we can organise the folder structure to any extent.We can place any subfolder
inside the subfolder.The url management is very easy.
The disadvantage is that we have to declare one action for each subfolder.
public function actions()
{
return array(
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
'page'=>array(
'class'=>'CViewAction',
),
'help'=>array(
'class'=>'CViewAction',
'basePath'=>'pages/help'
),
'event'=>array(
'class'=>'CViewAction',
'basePath'=>'pages/events'
),
'meeting'=>array(
'class'=>'CViewAction',
'basePath'=>'pages/events/meetings'
),
);
}
As you can see in basepath, we are having directory events and help inside pages,directory meetings inside the events.
The following url rules declared.
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'site/<action>/<view:\w+>'=>'site/<action>',
'site/<action>/<view:\w+>'=>'site/<action>',
),
),
Now we can do the following.
pages/about.php
http://localhost/boost/index.php/site/page/about
pages/help/greet.php
http://localhost/boost/index.php/site/help/greet
pages/events/schedule.php
http://localhost/boost/index.php/site/event/schedule
pages/events/meetings/current.php
http://localhost/boost/index.php/site/meeting/current
The cumbersome thing is that we have to declare as many actions,but we can customize the things to greater extend.
2.In the second approach,we have to extend CViewAction.
The advantage here is that we are declaring one action.
The disadvantage is that right now I am not able to create folders inside the subfolders.
components/SViewAction.php
class SViewAction extends CViewAction
{
public $viewFolder='folder';
private $_viewPath;
public function getRequestedView()
{
if($this->_viewPath===null)
{ if(!empty($_GET[$this->viewFolder]))
{
if(!empty($_GET[$this->viewParam]))
$this->_viewPath=$_GET[$this->viewFolder].".".$_GET[$this->viewParam];
else
$this->_viewPath=$_GET[$this->viewFolder].".".$this->defaultView;
}
else
{
if(!empty($_GET[$this->viewParam]))
$this->_viewPath=$_GET[$this->viewParam];
else
$this->_viewPath=$this->defaultView;
}
}
return $this->_viewPath;
}
}
url rules
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'site/page/<folder:\w+>/<view:\w+>'=>'site/page',
'site/page/<view:\w+>'=>'site/page',
),
),
SiteController.php
public function actions()
{
return array(
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
'page'=>array(
'class'=>'SViewAction',
'basePath'=>'pages',
),
);
}
Now we can access the files in the following way.
pages/about.php
http://localhost/boost/index.php/site/page/about
pages/help/greet.php
http://localhost/boost/index.php/site/page/help/greet
pages/events/schedule.php
http://localhost/boost/index.php/site/page/events/schedule
Now I can not access the pages/events/meeting/current.
With second approach, we can make a link like this.
echo CHtml::link("connect",array("site/page","folder"=>"help","view"=>"greet"));
We can make url like this.
echo Yii::app()->createUrl("site/page",array('folder'=>"help","view"=>"greet"));
Regards.