redirect different view scripts - zend-framework

I want to redirect to different viewscripts depends on a searchtype the user can fill in.
For example: The user wants to search for a person, than I want to use the matching viewscripts for persons (ansprechpartner). Please have a look at a part of my controller action:
switch ($suche['suchtyp']) {
case 1: //Ansprechpartner
$view = new ViewModel([
'ansprechpartner' => $this->ansprechpartnerTable->sucheAnsprechpartner($suche['suche']),
]);
$view->setTemplate('ansprechpartner/index');
return $view;
break;
case 2: //Mandant
$view = new ViewModel([
'mandant' => $this->mandantTable->sucheMandant($suche['suche']),
]);
$view->setTemplate('mandant/index');
return $view;
break;
case 3: //vertrag
$view = new ViewModel([
'vertrag' => $this->vertragTable->sucheVertrag($suche['suche']),
]);
$view->setTemplate('vertrag/index');
return $view;
break;
default:
return $this->redirect()->toRoute('index', ['action' => 'index']);
}
In the screenshot my folders will be shown:
So how can I use the existing viewscripts in this case, without to call the matching controller actions?

I think you should provide full template path to the setTemplate, in your switch
$view = new ViewModel([
'ansprechpartner' => $this->ansprechpartnerTable->sucheAnsprechpartner($suche['suche']),
]);
$view->setTemplate('stammdaten/ansprechpartner/index');
return $view;

This switch should be in the Action (in a Controller). It's the kind of logic that should never be in view. However, if you do have it in an action, you can leverage ZF to set a different layout
Example from link:
// A controller's action method that uses an alternative
// layout template.
public function indexAction()
{
//...
// Use the Layout plugin to access the ViewModel
// object associated with layout template.
$this->layout()->setTemplate('layout/layout2');
//...
}

Related

Where to put controller code for Yii2 sidebar modal form?

I want to have a modal Report Issue form in the sidebar that loads on every page. I can't seem to wrap my head around where to place the controller/form initialization code.
A typical controller action:
public function actionContact()
{
$model = new Feedback();
if ($model->load(Yii::$app->request->post()) && $model->sendEmail()) {
Yii::$app->getSession()->addFlash('success', 'Thank you for contacting us.<br /><br />We will respond as soon as possible.');
$model = new Feedback();
}
if (Yii::$app->request->isPjax) {
return $this->renderAjax('contact', [
'model' => $model,
]);
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
The $model is set when the action is first called, but if the modal is to be placed in the sidebar, every controller and/or controller/action could be called, and I don't want to have the $model initialization repeated each time (DRY).
I'm not sure if this is a prime condition for a custom widget, or a controller->beforeAction, or something else altogether.
Any insight is greatly appreciated.
Use a widget. The widget will create an empty $model and pass it to it's view. You can insert the widget in any view or in the layout too.
Regarding the
every controller and/or controller/action could be called
why would any controller be called? Your form should send the info to 1 controller/action not any of them. The widget will be shown by any controller/action but the widget's form will always just call 1.

How to disable layout and view renderer in ZF2?

How can i disable layout and view renderer in Zend Framework 2.x? I read documentation and can't get any answers looking in google i found answer to Zend 1.x and it's
$this->_helper->viewRenderer->setNoRender(true);
$this->_helper->layout->disableLayout();
But it's not working any more in Zend Framework 2.x. I need to disable both view renderer and layout for Ajax requests.
Any help would be great.
Just use setTerminal(true) in your controller to disable layout.
This behaviour documented here: Zend View Quick Start :: Dealing With Layouts
Example:
<?php
namespace YourApp\Controller;
use Zend\View\Model\ViewModel;
class FooController extends AbstractActionController
{
public function fooAction()
{
$viewModel = new ViewModel();
$viewModel->setVariables(array('key' => 'value'))
->setTerminal(true);
return $viewModel;
}
}
If you want to send JSON response instead of rendering a .phtml file, try to use JsonRenderer:
Add this line to the top of the class:
use Zend\View\Model\JsonModel;
and here an action example which returns JSON:
public function jsonAction()
{
$data = ['Foo' => 'Bar', 'Baz' => 'Test'];
return new JsonModel($data);
}
EDIT:
Don't forget to add ViewJsonStrategy to your module.config.php file to allow controllers to return JSON. Thanks #Remi!
'view_manager' => [
'strategies' => [
'ViewJsonStrategy'
],
],
You can add this to the end of your action:
return $this->getResponse();
Slightly more info on the above answer... I use this often when outputting different types of files dynamically: json, xml, pdf, etc... This is the example of outputting an XML file.
// In the controller
$r = $this->getResponse();
$r->setContent(file_get_contents($filePath)); //
$r->getHeaders()->addHeaders(
array('Content-Type'=>'application/xml; charset=utf-8'));
return $r;
The view is not rendered, and only the specified content and headers are sent.

adding Zend_Form_Element_Captcha to form causes form to not display

I am having trouble getting a captcha element to display in a signup form. It was working previously, but now for some reason when I try to add the captcha element to the form the entire form does not display.
Below is the function in my controller file to create the form
private function getSignupForm()
{
// create form
$form = new Zend_Form();
$form->setAction('success');
$form->setMethod('post');
$form->setAttrib('sitename', 'loudbite');
// add elements
require "form/elements.php";
$loudbiteElements = new Elements();
// create username field
$form->addElement($loudbiteElements->getUsernameTextField());
// create email field
$form->addElement($loudbiteElements->getEmailTextField());
// create password field
$form->addElement($loudbiteElements->getPasswordTextField());
// add captcha
$form->addElement($loudbiteElements->getCaptcha());
// create submit button
$form->addElement('submit', 'submit');
$submitElement = $form->getElement('submit');
$submitElement->setLabel('Create My Account!');
return $form;
}
and here is the action to pass the form to the corresponding view
public function newAction()
{
// get the form
$form = $this->getSignupForm();
// add the form to the view
$this->view->form = $form;
}
If I comment out the line in my getSignupForm() function where I add the captcha to the form then the form will display ok minus the captcha element. This leads me to assume that there is a problem with the captcha contruction.
Below is the function in an external model file where the captcha is constructed
public function getCaptcha()
{
$captchaElement = new Zend_Form_Element_Captcha(
'signup',
array('captcha' => array(
'captcha' => 'Figlet',
'wordLen' => 6,
'timeout' => 600))
);
$captchaElement->setLabel('Please type in the words below to continue:');
return $captchaElement;
}
All the other form elements are created in this external model file. Based on the fact that the other elements display correctly if I do not add the captcha element, I assume it is not a problem with accessing the model file.
The last time I used this signup form everything was working ok! And I don't remember having changed anything between then and now.
Any help is greatly appreciated.
Regards
Roan

Load Zend_Form from a view helper in Zend?

Is it possible to load a Zend_Form from a view helper? I'm using thise form in a login action method. But I also want this form to be visible on the navigation on every page (so without the login action actually being called yet), the post method of the form will send to the login action method.
I'm guessing it should be done with a view helper but I don't see how.
Any ideas?
I tried with this:
my view helper:
class Zend_View_Helper_LoginForm
{
function getLoginForm(){
$form = new Form_LoginForm();
return $form;
}
}
and I call it from my layout like this:
<?php echo $this->form(); ?> but this doesn't work. (I'm able to call the same form through an action method though!)
In this case it gives me this error (which doesn't make sense because my helper is only 9 lines long):
Warning: Missing argument 1 for Zend_View_Helper_Form::form() in C:\xampplite\htdocs\zendpr\library\Zend\View\Helper\Form.php on line 44
Your view helper should extends the Zend_View_Helper_Abstract class and the method of the view helper must have the same name as the class :
class Zend_View_Helper_LoginForm extends Zend_View_Helper_Abstract
{
function loginForm() {
$form = new Form_LoginForm();
return $form;
}
}
and you call it like this in your view script :
echo $this->loginForm();
If you call :
echo $this->form();
You are using the view helper Zend_View_Helper_Form
Have your View_Helper extend Zend_View_Helper_Abstract and override the setView()
class Zend_View_Helper_XX extends Zend_View_Helper_Abstract {
public $view;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
Initialise the form in your controller action and set the form reference
// controller action code
$this->view->form = $form;
Then in the view helper you can reference the form via the view
// view helper code
$this->view->form;
class Zend_View_Helper_LoginForm extents Zend_Form {
function getLoginForm(){
$form = new Form_LoginForm();
return $form;
}
}
OR
$this->view->form=$form;
Both are going to return the form. The view form is more specific for view.
Add this into your phtml view file
echo $this->form();
To answer this question - Remove the Parenthetical
Should be
echo $this->form;

Zend Framework Action Helper Problem

I'm a relative noob when it comes to Zend Framework, however I've got a form that I need to use if a couple of views so I thought I might use a Action Helper to instantiate the form set a few attributes and pass it to the relevant view. I've created the Action Helper and can call it from within the relevant controller's action, however when I try to pass the form to the action's view nothing gets rendered, ie:
$form = new Application_Form_Colour;
if($this->_request->isPost() && $form->isValid($this->_request->getPost()))
{
$model = new Application_Model_Colour();
$model->changeColour($form->getValues());
$form->reset();
}
else
{
$form->newColour->setAttrib('disabled', 'disabled');
}
$this->view->form = $form;
Is there something I am doing wrong or have I got the wrong idea of what an Action Helper can be used for? Maybe its not an Action helper that I need to use?
It turned out I was just being stupid! Instead of
$this->view->form = $form;
at the end of the Action Helper I should have done:
return $form;
Then in my controller:
$this->view->form = $this->_helper->myActionHelper->myActionHelperMethod();
Silly me...