Here is the code I have:
$this->getView()->setScriptPath($templatePath);
$this->_helper->viewRenderer($page);
This code is handled in the Core_PageController view action. The problem I have is now the view object looks for my script files in $templatePath/page since page is the controller. What I'd like is for the view object to look in just the $templatePath directory path (without the page directory);
Thanks for the help!
You can instruct the ViewRenderer to not use the controller name as part of the view script path.
To do that, try:
$this->_helper
->viewRenderer
->setNoController(true); // do not use controller name as part of the view path
setNoController($flag = true) can be used to tell render() not to look
for the action script in a subdirectory named after the controller
(which is the default behaviour). getNoController() retrieves the
current value.
More info on the ViewRenderer helper.
Related
The default view path is view/scripts while i've another view/abc/scripts so for my default system it will use the default path while abc system use the abc path.
I have layout plugin to set it from bootstrap so it will see either one of the path.
But I just noticed that for abc system I may have view exactly the same from default. I can copy the file to abc folder but then I will have duplicate file and the content is exactly the same.
So I'm wondering if there's a way I can put in some code in one place other than controller(instead of every controller or one controller while other extends it) so if the view file not exists then it will look at the default path for the file?
You can add multiple paths in bootstrap
protected function _initView(){
$view = new Zend_View();
$view->addScriptPath(APPLICATION_PATH . 'path/to/path1');
$view->addScriptPath(APPLICATION_PATH . 'path/to/path2');
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);
return $view;
}
I think that it is simple to use file_exists function.
I've asked a question like this previously but I believe this is different (that one was just a general question).
I implemented Zend_Navigation.
For menu I used DB Table to store menu items and did recursion on Array-s to get the tree of menu items.
All of this action takes place in my module called Menu. Inside I have:
Menu --
Controllers --
IndexController.php
Models--
DbTable--
Menu.php
Bootstrap.php
inside index controller I have a function menuGenerator($menu_id)
So following tutorials on Zend_Navigation, the menu is initialized in the application bootstrap.
my function inside application's bootstrap looks like this:
public function _initMenus() {
$menuArray = new Menu_IndexController();
$outArray = $menuArray->menuGenerator(1);
$mainmenu = new Zend_Navigation($outArray);
$this->view->navigation($mainmenu);
}
and it gives me an error:
Fatal error: Class 'Menu_IndexController' not found in D:\Server\xampp\htdocs\project\application\Bootstrap.php on line 8
So, Any ideas how should I make it to work correctly?
P.S. is it possible to start 2 new menus at a time? for ex: I need 1. main menu 2. footer menu (any link to an article would be nice)
By default, Zend Framework's autoloader doesn't autoload controllers in the same way it loads other components (models, view helpers, forms, etc), so PHP throws the error saying it can't find the class. The quickest way to get around this is to explicitly include the controller in Bootstrap.php. The following should work:
public function _initMenus() {
require_once('./Controllers/IndexController.php');
$menuArray = new Menu_IndexController();
$outArray = $menuArray->menuGenerator(1);
$mainmenu = new Zend_Navigation($outArray);
$this->view->navigation($mainmenu);
}
It's pretty unusual to call a controller method during Bootstrap since there are many bootstrapping tasks upon which controller actions depend. In your case, the controller method menuGenerator() is not actually an action, so presumably it will not be a problem.
Nonetheless, it's still unusual enough that I would move the menuGenerator() method out into its own class. Then invoke that operation both at Bootstrap and in your controller.
am new in Zend framework.
I have one .phtml file, includes menus section. i need to add that .phtml file in to the views i.e views/scripts/index.phtml page.
how can refer that
please advice
thanks in advance
You can use the render helper:
<?=$this->render('menu.phtml')?>
If you want to pass specific variables to the rendered script, use the partial helper:
<?=$this->partial('menu.phtml', array("varname" => "value"))?>
The menu.phtml file must be in the search path, which usually is the path to the current module's scripts directory ( view/scripts ). You can either place the menu.phtml file in this directory, or add a directory to the search path using the addScriptPath() method on the view object.
Try this
echo $this->partial('path/file.phtml', 'ModuleName')
I have a problem as I want to render view from different controller and pass there datas. Do You know how to do it?
I was trying:
$this->renderScript('index/index.phtml')->entries = $result;
But my if:
if (count($this->entries) <= 0)
return 0
Do You know how to do it?
THANKS!
Do you mean you just want to render a different controller action's view script?
$this->view->entries = $result;
$this->_helper->viewRenderer('index/index', null, true);
Check out the manual page for the ViewRenderer helper.
Render view with action's output data.
in view page you wish to display data write this simple code.
echo $this->action('list','users','main');
list is my action name
users is my controller name
main is my module name (if module using in your project).
I'd like to have my controller - i.e. Page_IndexController - extend a base controller.
For example;
class Page_IndexController extends Content_IndexController {
}
However, it seems the autoloader doesn't pick up the fact it's a controller class at any point - I get the error Fatal error: Class 'Content_IndexController' not found
First question: How do I fix this?
I can temporarily fix this by require_once'ing the generic 'content' controller, but this is hardly ideal.
The next issue is that if my Page controller has it's own view script for an action, it works no problem.
But if I'm extending a controller, and I call for example 'listAction' on the Page controller, but this action is implemented in Content_IndexController, it still looks for the list view script in the page controllers "scripts" directory.
Second question: How do I configure my controller to use its parents view script if it doesn't have its own?
If your application can find Page_IndexController you probably have a Page module. If you are not using modules in your application you have to name your controllers PageController and ContentController, not Page_IndexController, ... So the solution is to register "Content_" namespace with the autoloader.
As for the second question. You can extend the provided ViewRenderer controller action helper and override methods for finding the view script so they can look in other places if needed. You just have to pass your viewrenderer to the front controller. For passing your own ViewRenderer to the controller take a look at Advanced Usage Examples.
The auto loader can't find your controller because you haven't told it where to search. The Content_IndexController isn't in your "library" folder (I assume its inside of the Content module)
What I would suggest is creating a My_Controller_IndexBase class in your library folder that both Content_IndexController and Page_IndexController inherit.
Did a little more research on the topic of the view script. You could change up the view's script paths during init() somewhere. I'm pretty sure this would probably need to be done in a ViewRenderer - but might also work inside the controller's init/action code.
$this->view->setScriptPath(
array(
realpath(APPLICATION_PATH+'/../path/to/other/module/views'),
) + $this->view->getScriptPath());
Script paths are processed Last In First Out according to the Zend_View_Abstract
For the second question:
If you don't want to write your own ViewRenderer, you can use $this->renderScript('parent/index.phtml') to render a specific view script. You could call that in your child controllers instead of letting the views be rendered for you automatically, or if your child controllers rely on the parent controller to do the rendering of the script you can just place that in your parent controllers.
I do that mode.
Frist I register a new name who a plugin in my index.php into the public folder:
/public/index.php
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('modelo_');
Secound I create a new folder to put my controller
/library/modelo/
Third I create my controller model and put it into the folder created and rename it.
class Modelo_ModeloController extends Zend_Controller_Action
{
protected $_db = null;
protected $_menu = null;
protected $_util = null;
protected $_path = null;
... actions to my model
public function inicial(){}
}
and I extend this class im my application
class Sispromo_PedidoController extends Modelo_ModeloController
{
public function init(){}
....
}