Zend framework - check if view exists and not exists use another path? - zend-framework

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.

Related

TYPO3 set template view for controller action

I want to use the view template of the list action for my listByYear action. I tried setTemplatePathAndFilename without success. It still cannot find the template.
Sorry, the requested view was not found.
The technical reason is: No template was found. View could not be
resolved for action "listByYear" in class
"XXX\YYY\Controller\EventController".
/**
* action listByYear
* #param \XXX\YYY\Domain\Model\Event $event
*
* #return void
*/
public function listByYearAction(\XXX\YYY\Domain\Model\Event $event)
{
$date = $event->getStart();
$events = $this->eventRepository->findByYear($date->format('Y'));
$this->view->setTemplatePathAndFilename(
'typo3conf/ext/' .
$this->request->getControllerExtensionKey() .
'/Resources/Private/Templates/Event/List.html'
);
debug('typo3conf/ext/' .
$this->request->getControllerExtensionKey() .
'/Resources/Private/Templates/Event/List.html');
$this->view->assign('events', $events);
}
How do I make it use the template for the list?
The very short answer is, you can't. The view will already have been initialised and asked to resolve a template well before your action fires, indeed well before any point where you can affect the template filename that it would look for.
The template file that by convention would be resolved must always exist. This is what allows your controller action to render. You can then, but I would not recommend that you do, override the template file by setting the template name (the action).
Overall recommendation: use the default template naming logic. If you need to re-use templates, consider refactoring the template parts you need to reuse, placing them in partial templates.
// Do not forget the use in the header ...,
// or write fully qualified class path..
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
// then add something like this in your action before the assign...
// or maybe create a Standalone view: search the web for "Extbase Standaloneview"
// have a look at: /typo3/sysext/about/Classes/Controller/AboutController.php
$this->view = GeneralUtility::makeInstance(StandaloneView::class);
$this->view->setTemplate('ActionName');
$this->view->setTemplateRootPaths(['EXT:your_ext/Resources/Private/Templates']);
$this->view->setPartialRootPaths(['EXT:your_ext/Resources/Private/Partials']);
$this->view->setLayoutRootPaths(['EXT:your_ext/Resources/Private/Layouts']);
$this->view->assignMultiple([
'whatever' => $whatever,
'youLike' => $youLike,
]);

Zend Framework + Tricky controller name and module name conflict

We have a Zend application that has these following modules:
Users
Shop
etc...
Front - A content management module
While the Front module has the following controllers:
UsersController
ShopController
AuthController
etc...
In the middle of our development cycle we decided to set the default module for the Zend application to the Front module, but inadvertently broke our links, as http://domain.com/front/users/list are now generated as http://domain.com/users/list, which is now pointing to the wrong action.
We are generating links using the URL view helper, (i.e. $this->url(array('module' => 'front', 'controller' => 'users', 'action' => 'list'));), but the 'front' URI segment is omitted since switching the default module to the Front module.
I totally understand why this is so, but we are avoiding renaming all controllers under the Front module to avoid conflicts.
My question is, is there is a way to instruct the URL view helper to always include the 'front' module URI segment even if it is already set as the default one?
My question is, is there is a way to instruct the URL view helper to
always include the 'front' module URI segment even if it is already
set as the default one?
You can create your own url view helper with same name and override default url view helper add it to Zend_View object in your bootstrap.
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view)
{
$viewRenderer->initView();
}
$view = $viewRenderer->view;
$view->addHelperPath('/path/to/some/helpers', 'My_View_Helper');
Now create class My_View_Helper_Url let it extend Zend_View_Helper_Url override url method.
Here is reference form ZF doc about this procedure
In fact, you can "stack" paths using the addHelperPath() method. As
you add paths to the stack, Zend_View will look at the
most-recently-added path for the requested helper class. This allows
you to add to (or even override) the initial distribution of helpers
with your own custom helpers.
Having said that I think http://domain.com/users/list, should have worked correctly in first place since you have specified default module to front.

Zend view script path setup

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.

how to refer one .phtml in the other views in Zend

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')

Adding Zend View Helper to all views in application (in bootstrap)

I am using Zend Framework 1.6 hence I'm not utilizing Zend_Application.
I have a simple, normal View Helper (extending Zend_View_Helper_Abstract). It works perfectly fine as long as I add it to the view in my action controller. But I want to be able to use it in every view in every module. I figured it should be easy to just get the global view in my bootstrap file and do this:
$view->addHelperPath(PATH_VIEW_HELPERS, 'RT_View_Helper_');
But I can't seem to be able to get the $view object in my bootstrap file. I also tried
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view) {
$viewRenderer->initView();
}
$view = $viewRenderer->view;
$view = new Zend_View(array('encoding'=>'UTF-8'));
$view->addHelperPath(PATH_VIEW_HELPERS, 'RT_View_Helper_');
But that doesn't do the trick either. I've tried putting it in the preDispatch() and postDispatch() of my boostrap (which is a front controller plugin).
Does anyone have any thoughts on how to do this? It seems it should be so simple, yet I haven't been able to find a solution for it in two days.
Thanks all :)
Ali
If you are using Zend_Layout then something along these lines should get you the Zend_View:
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
// set helpers, doctype, etc...
Im not sure understand your Problem but, in my opinion you could register the view Object in Zend_Registry
Zend_Registry::set('view', new Your_view);
You can do the following in your "setup.php / bootstrap.php or whatever file"
$view = new Zend_View();
// add Helper Path
$view->setHelperPath('/path/to/more/helpers', 'My_View_Helper');