Call Views Of A Parent Directory

Hi,

I have a controller (Admin/Management/UserController.php). This Controller is in a sub-sub directory and I want to call a view (Sites/User.php) which does not belong to the dirctory of the controllerID.




- congfig

- controllers

       - admin

            - management

               UserController.php

- data

- ...

- views

    - sites

        User.php



I tried to call it with but i alwas get the error message, that the controller cannot find the requested file.




$this->render('//sites/user');

$this->renderPartial('//sites/user');



So I am sure it is a trivial solution.

Thanks!

You are messing with Yii conventions.


$this->render('/sites/user');

is this not working

No this doesn’t work too.

I only tried to localize the files with // because I read in a forum post, that i can say so that I am in the folder views.

well it could have to do with the capital U of your view:


$this->render('//sites/User');

?

No, unfortunately not.

I believe you can dot notation to render views so try something like


$this->render('application.views.sites.user', array(...));

Matt

can you please paste the whole error

Hi,

I’m sorry that the reply took so long. But here is the whole error message I get. It is a diffrent controller but the same problem.





CException


SiteController cannot find the requested view "application.views.sites.contact".


E:\wamp\www\ekc\framework\web\CController.php(878)


866     {

867         if(($viewFile=$this->getViewFile($view))!==false)

868         {

869             $output=$this->renderFile($viewFile,$data,true);

870             if($processOutput)

871                 $output=$this->processOutput($output);

872             if($return)

873                 return $output;

874             else

875                 echo $output;

876         }

877         else

878             throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',

879                 array('{controller}'=>get_class($this), '{view}'=>$view)));

880     }

881 

882     /**

883      * Renders a named clip with the supplied parameters.

884      * This is similar to directly accessing the {@link clips} property.

885      * The main difference is that it can take an array of named parameters

886      * which will replace the corresponding placeholders in the clip.

887      * @param string $name the name of the clip

888      * @param array $params an array of named parameters (name=>value) that should replace

889      * their corresponding placeholders in the clip

890      * @param boolean $return whether to return the clip content or echo it.


Stack Trace

#0 	

+

 E:\wamp\www\ekc\framework\web\CController.php(782): CController->renderPartial("application.views.sites.contact", array("model" => ContactiForm), true)

#1 	

+

 E:\wamp\www\ekc\eRouting\protected\controllers\admin\home\SiteController.php(75): CController->render("application.views.sites.contact", array("model" => ContactiForm))

#2 	

+

 E:\wamp\www\ekc\framework\web\actions\CInlineAction.php(49): SiteController->actionContact()

#3 	

+

 E:\wamp\www\ekc\framework\web\CController.php(308): CInlineAction->runWithParams(array("r" => "admin/home/site/contact"))

#4 	

+

 E:\wamp\www\ekc\framework\web\CController.php(286): CController->runAction(CInlineAction)

#5 	

+

 E:\wamp\www\ekc\framework\web\CController.php(265): CController->runActionWithFilters(CInlineAction, array())

#6 	

+

 E:\wamp\www\ekc\framework\web\CWebApplication.php(282): CController->run("contact")

#7 	

+

 E:\wamp\www\ekc\framework\web\CWebApplication.php(141): CWebApplication->runController("admin/home/site/contact")

#8 	

+

 E:\wamp\www\ekc\framework\base\CApplication.php(169): CWebApplication->processRequest()

#9 	

–

 E:\wamp\www\ekc\eRouting\index.php(13): CApplication->run()


08 defined('YII_DEBUG') or define('YII_DEBUG',true);

09 // specify how many levels of call stack should be shown in each log message

10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

11 

12 require_once($yii);

13 Yii::createWebApplication($config)->run();


2013-05-27 15:20:08 Apache/2.2.22 (Win64) PHP/5.4.3 Yii Framework/1.1.13




Something’s amiss here. Your first post’s directory structure says that the user view file is named User.php but you’re calling it with a lower case. Yii’s default code uses lowercase file names for views. Go through and make sure all directories’ and files’ case match what you’re calling in your render methods.

Also, are you using a theme?

Matt

[color=#666600][size=2]

[/size][/color]

Hi Matt,

it was an error in my first report. I will change this. Thanks.

I named all views in lower case and call they this way.

Yes I am using the bootstrap theme. But alle my views are in the standard direcotry and it works (only the call of a view in a parent directory doesn’t work)

That’s the way i always use and it works well.

application.views.sites.viewX

application.modules.ModuleX.views.viewX

But i don’t know if using themes will be a problem

According to this, if you’re using a theme it will call CTheme’s getViewFile which will look in the current theme’s (or module’s if appropriate) views directory. Not sure if this applies here though.

Do me a favor and disable the theme in your main config and see if Yii can find the views.

Matt

Hi Matt,

I disabled the theme and now both solutions work.




$this->render('application.views.site.contact', array('model'=>$model));

$this->render('//site/contact', array('model'=>$model));



Ok that is a little bit weird, but now I enabled the theme again and everything works quite fine.

If the error shows up again I will post it here again.

Till then thanks to all of you for your help

Hmmm, that is very weird. Do post back if you find anything out - It would be great if we can figure out why.

I’m not using any theme but i try creating a new one.

It doesn’t work with the theme and from the API

http://www.yiiframework.com/doc/api/1.1/CController#getViewFile-detail