how to have two controller actions, one shared view for Zend_Controller_Action Class? - zend-framework

How do you specify a custom view script for a given Controller Action method?
For example:
Class UserGalleryController extends Zend_Controller_Action
{
public function fooAction()
{
$this->view->actionMsg = 'foo';
// (uses foo.phtml automagically)
}
public function barAction()
{
$this->view->actionMsg = 'bar';
//use foo's view script ?????
}
}
I basically want to have one view script (foo.phtml)
Thanks :-)

public function barAction()
{
$this->view->actionMsg = 'bar';
$this->render('foo');
}

Related

Rendering Custom View in SuiteCRM not working

I have a controller that calls my custom view, but the custom view is not getting rendered.
I cant understand the issue, everything looks correct to me.
Neither is it displaying any errors or warnings.
My module name is SCRV_SSRS_CRM_Reports_View
I have below code in:
custom/modules/SCRV_SSRS_CRM_Reports_View/controller.php
require_once('include/MVC/Controller/SugarController.php');
class SCRV_SSRS_CRM_Reports_ViewController extends SugarController
{
function action_test(){
$GLOBALS['log']->fatal('Am in Controller');
$this->view = "test";
}
}
And in
custom/modules/SCRV_SSRS_CRM_Reports_View/views/view.test.php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('include/MVC/View/views/view.list.php');
class SCRV_SSRS_CRM_Reports_ViewViewtest extends ViewList
{
public function display()
{
echo "HIIII";
$GLOBALS['log']->fatal('Am in View');
}
}
Cant see the view getting rendered when I access it by
http://localhost:8080/dev-crm/index.php?module=SCRV_SSRS_CRM_Reports_View&action=test
I cant see HIIII displayed on screen or the log entry.
Controller should be
class CustomMeetingsController extends SugarController {
$this->view = 'invite';
}
and View should be like
class CustomMeetingsViewinvite extends ViewList {
public function display() {
echo 'hiiiii';
}
}

How to call a method of another controller in zend?

I have a one controller SearchController in Hotel module and i have a one method Search in this controller like below.
public function searchAction()
{
// Code for search
// want to call getlatlng method
}
now i created a one new controller DistanceController in same module and
created a one getlatlng method in distance controller.my getlatlng method like this.
public function getlatlng()
{
$location = $_REQUEST['lat'].','.$_REQUEST['lng'];
return $location;
}
Now i want to call a getlatlng method in searchAction method. which returns a latitude and longitude of the current place.I pass latitude and logitude to getlatlng function using post or get.
So how can i call getlatlng method in searchAction method?
You can call like this, If you are using ZF Version 1.x:
class SearchController extends Zend_Controller_Action
{
public function searchAction() {
echo "search_action_from_SearchController";
require_once ('DistanceController.php');
$distanceCtrl = new DistanceController($this->_request, $this->_response);
$distanceCtrl->xyzAction();
die;
}
}
class DistanceController extends Zend_Controller_Action
{
public function getlatlng() {
echo "getlatlng_from_DistanceController";
die;
}
}
Output:
URL: http://www.example.com/search/search
search_action_from_SearchController
getlatlng_from_DistanceController

Assigning Values to Views when using $this->renderScript

in order to avoid copy/paste, i can use a unique view for different actions, i can do this using one of these ways
$this->renderScript('index/index.phtml');
$this->view->var = "hello world";
or
$this->_helper->viewRenderer('index');
$this->view->var = "hello world";
if i want to use a different controller i have to use the 1st one, the viewRenderer is ok, but when i use the renderScript it shows nothing like var is not defined. how can i assign values to the view script?
index.phtml would be somthing like this
echo $this->var ;
It should work the way you described it
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
$this->view->var = 'echo me in any action viewscript and i will show you text';
}
public function indexAction()
{
// action body
$this->view->test = 'Don\'t put me here becuase this is not the action that is run';
}
public function testAction()
{
// action body
$this->view->test = 'Hello world';
$this->renderScript('index/index.phtml');
// or $this->_helper->viewRenderer('index');
}
}
in my view (index.phtml)
i have
<?php echo $this->test;?>
When i go to /index/test/ it will show "hello world"...
Also when i do it in another controller it gives me the result
class MyController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
$this->view->test = 'Hello world';
$this->renderScript('index/index.phtml');
}
}

implement Zend_Controller_Router_Interface

Here I would like to have a tree of controllers different from that offered by Zend.
Let me explain, in many of my projects I find myself with controllers with over 1000 lines of code which is not very top side of code maintainability. So I want to cut my controllers, ie.
Example current controller:
UserController class extends Zend_Controller_Action {
listAction public function () {}
editAction public function () {}
.......
groupListAction public function () {}
groupEditAction public function () {}
.......
roleListAction public function () {}
roleEditAction public function () {}
.... etc.
}
So in this example I will like to outsource the concept of groups and roles in other controllers and other issues.
Desired architecture:
- controllers /
- UserController.php
- User /
--- GroupController.php
--- RoleController.php
-> url:
http://www.site.com/user/ -> class UserController
http://www.site.com/user_group/ -> class User_GroupController
http://www.site.com/user_role/ -> class User_RoleController
So I do not know how to apply this type of cutting.
If someone with an idea I'm interested.
Thank you in advance.
Create a custom controller dispatcher. Class must implements Zend_Controller_Dispatcher_Interface, or you can extends Zend_Controller_Dispatcher_Standard.
The easiest way is to extend Zend_Controller_Dispatcher_Standard, because you will only need to override some of the parent methods.
class My_Dispatcher extends Zend_Controller_Dispatcher_Standard
{
public function isDispatchable(Zend_Controller_Request_Abstract $request)
{
// your code to find the correct class
}
public function loadClass($className)
{
// your code to load the correct class
// return the correct class name (e.g. User_RoleController)
}
public function getActionMethod(Zend_Controller_Request_Abstract $request)
{
// your code to find the correct method name
}
}
Set the new dispatcher class in your bootstrap:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initMyFrontController()
{
$this->bootstrap('frontController');
$frontController = $this->getResource('frontController');
$frontController->setDispatcher(new My_Dispatcher());
return $frontController;
}
}
thank you for your reply but i find the solution just by create custum routes.
ie.
routes.core_user_group_index.type = "Zend_Controller_Router_Route_Static"
routes.core_user_group_index.route = "admin/core/user_group"
routes.core_user_group_index.defaults.module = "core"
routes.core_user_group_index.defaults.controller = "user_group"
routes.core_user_group_index.defaults.action = "index"

View overloading in Zend Framework

Let's say I have 2 controllers, content and news:
class ContentController extends Zend_Controller_Action { }
and
class NewsController extends ContentController { }
If there are no views found for the news controller, I want Zend to use the scripts path of its parent controller.
How can I achieve this without having to route to its parent controller?
You will have to add the scriptPath manually:
class ContentController extends Zend_Controller_Action {
public function init()
{
$this->view->addScriptPath(APPLICATION_PATH . '/views/scripts/content');
}
}
and
class NewsController extends ContentController {
public function init ()
{
// Ensure that ContentController inits.
parent::init();
$this->view->addScriptPath(APPLICATION_PATH . '/views/scripts/news');
}
}
This will use the stack functionality of the view script inflector. It will first look in the path last specified, which is APPLICATION_PATH . '/views/scripts/news', if the script isn't found there, it will look in the second directory on the stack, which is APPLICATION_PATH . '/views/scripts/content'.