Using Static Files In Subdirectories

I am trying to use subdirectories to organize my static files a bit.

I have /protected/views/site/pages/help/test.php and would like the URL to look something like <server>/site/page/help/text.

I found some help with getting the URL Manager to almost work here http://www.yiiframework.com/forum/index.php/topic/25480-urlmanager-rules-for-static-pages-in-subdirectories/page__gopid__190392#entry190392

Also, the topic below is closed, but seems to describe similar behaviour to what I am seeing.

http://www.yiiframework.com/forum/index.php/topic/2178-changes-from-1-0-3-broke-my-app/

I have run into the same problem and was going to solve it by making my url use a different character.

This rule works, and replaces the separator with a -

'site/page/help-&#60;scriptname:[-&#092;w]+&gt;'=&gt;'site/page/view/help.&#60;scriptname&gt;',

So /site/page/dir-file which becomes site/page?view=dir.file which maps correctly to the /protected/views/site/pages/dir/file.php.

I would prefer to use /site/page/dir/file as the URL, but I always get /site/page/dir%2Ffile.

I understand that I may be able to make this work by changing my .htaccess, but I can live with - instead.

Does anyone know how to make this work the way I want?

Is this a bug within Yii where it is urlencoding the strings before passing them through the URL transformations? I understand that URL parameters should be encoded, but since this is being transformed into the path of the URL, it doesn’t need to be encoded.

Dear Friend

I want to share something with you on this topic.

There are two approaches I have employed here.

  1. 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.