Routing in MVC3 - How can i call ReturnView() for a view in another folder? - asp.net-mvc-routing

i have a mvc3 project ( nopcommerce 2.65 ) with structure like this.
Nop.Plugin.Payments.KrediKarti (namespace)
--Controllers
----SecurePaymentReturnController.cs
:
:
Nop.Web (other namespace)
--Views
----Checkout
------Confirm.cshtml
I want to call ReturnView() method from SecurePaymentReturnController.cs for Confirm.cshtml view with its model as parameter like this "ReturnView("Confirm",model)".
But the view cannot be found by view engine. I tried these ;
ReturnView("Confirm",model)
ReturnView("Checkout/Confirm",model)
ReturnView("Nop.Web.Checkout.Confirm",model)
How can i call this view ? What is the proper view name (or route or something) for this view ?
Thanks in advance.

You should to declare full path to view include .cshtml extension, like this:
return View("~/Views/Checkout/Confirm.cshtml", model);

Related

call Zend framework view helper from within own view helper

Zend Framework 1.12
I have written my own view helper and need to call a Zend view helper from within it.
In my view file, I can call
$this->formSelect (...) to get a select dropdown
however in my own view helper file
$this->view->formSelect (...)
causes an error
Call to undefined method Zend_View_Helper_MilestoneList::formSelect()
How can I access Zend Framework view helpers from within there?
It is very simple to call another View Helper.
Your view helper extends must be extend Zend_View_Helper_Abstract, so that it has access to the $view. Then you may simply call helpers as you would from a view, i.e.
$this->view->generalFunctions()->progressMeter();
For example you can access it in to your view:
<?php
class Zend_View_Helper_FormVars extends Zend_View_Helper_Abstract {
/* ... */
public function mkCategoryCodeSelectGroup($codeTypeArr=array(),
$codesArr=array()) {
$html='';
$html. $this->view->generalFunctions()->progressMeter();
return $html;
}
}
Please set class name as per your need. and just try it.
let me know if i can help you
I found that
$selectFormHelper = $this->view->getHelper('FormSelect');
$selectFormHelper->formSelect(...)
works, but
$this->view->formSelect(...)
does not.
I'm not sure why that is, but happy to live with it for now.

Call Modules Controller from Application Bootstrap

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.

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.

ZEND, rendering different view with data

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

Zend Framework - Extending Controllers

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(){}
....
}