Default for text element in zf2 forms - zend-framework2

How can I setup default for Text element in ZF2 Forms?
I tried the following:
In the view file. This does not get to data, and is not saved:
if($form->get('agencyName')->getValue() === '')
$form->get('agencyName')->setValue('Virtual Field Practicum');
This affects neither view, nor DB:
$this->add(array(
'name' => 'agencyName',
'options' => array(
'label' => 'Agency Name',
),
'attributes' => array(
'disabled' => 'disabled',
'value' => 'Virtual Field Practicum',
)
));
I also tried to modify entity in two ways, but it did not affect anything:
public function __construct()
{
//set default agency name
$this->agencyName = 'Virtual Field Practicum';
}
OR:
public function setAgencyName($agencyName)
{
if ($agencyName === '')
$this->agencyName = 'Virtual Field Practicum';
else
$this->agencyName = $agencyName;
return $this;
}
Edit 1
Adding my generic actions to the post:
1) This one is responsible to load the forms, and process non-ajax calls:
public function editTabAction()
{
$buildName = $this->params()->fromRoute('buildName', 'unknown');
if ($buildName == 'unknown') {
$buildName = $this->params()->fromPost('buildName', 'unknown');
if ($buildName == 'unknown') {
trigger_error('Could not retrieve build name for ' . $buildName . ' entity for this form!');
}
}
//extract parameter from dispatch command
$studEvalId = (int)$this->params()->fromRoute('studEvalId', 0);
if ($studEvalId == 0) {
//extract parameter from the form submission
$studEvalId = (int)$this->params()->fromPost('studEvalId', 0);
if ($studEvalId == 0) {
return $this->notFoundAction();
}
}
$data = $this->getEntity($buildName, $studEvalId);
// Get your ObjectManager from the ServiceManager
$objectManager = $this->getEntityManager();
// get from from FormElementManager plugin
//forms are defined in Module.php
$formName = $buildName . "Form";
$sl = $this->getServiceLocator();
$form = $sl->get('FormElementManager')->get($formName);
$form->setHydrator(new DoctrineHydrator($objectManager ));
$form->setObject($this->getEntityInstanceFromBuildName($buildName));
$form->bind($data);
//set class and Id for buttons like SaveChanges to reference it
$form->setAttribute('class', "studentFormsClass_$studEvalId");
$form->setAttribute('id', "studentFormsId_$studEvalId" . "_$buildName");
//set buildName to the form
$form->get('buildName')->setAttribute('value', $buildName);
$request = $this->getRequest();
if ($request->isPost()) {
$formValidatorName = "OnlineFieldEvaluation\Form\\" . $buildName . "FormValidator";
$formValidator = new $formValidatorName();
$form->setInputFilter($formValidator->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->savetodb($form->getData(), $buildName);
// Redirect to list of forms
return false;
} else {
foreach ($form->getMessages() as $messageId => $message) {
echo '<pre>';
echo "Validation failure '$messageId':";
print_r($message);
echo '</pre>';
}
}
}
$view = new ViewModel(array(
'studEvalId' => $studEvalId,
'buildName' => $buildName,
'form' => $form,
));
$view->setTemplate('online-field-evaluation/tabs/edit' . $buildName . '.phtml');
return $view;
}
2) This one is responsible for ajax calls:
public function validatepostajaxAction()
{
$request = $this->getRequest();
$response = $this->getResponse();
$buildName = $this->params()->fromRoute('buildName', 'unknown');
if ($buildName == 'unknown') {
$buildName = $this->params()->fromPost('buildName', 'unknown');
if ($buildName == 'unknown') {
trigger_error('Could not retrieve build name for ' . $buildName . ' entity for this form!');
}
}
//extract parameter from dispatch command
$studEvalId = (int)$this->params()->fromRoute('studEvalId', 0);
if ($studEvalId == 0) {
//extract parameter from the form submission
$studEvalId = (int)$this->params()->fromPost('studEvalId', 0);
if ($studEvalId == 0) {
return $this->notFoundAction();
}
}
$data = $this->getEntity($buildName, $studEvalId);
$objectManager = $this->getEntityManager();
$formName = $buildName . "Form";
$sl = $this->getServiceLocator();
$form = $sl->get('FormElementManager')->get($formName);
$form->setHydrator(new DoctrineHydrator($objectManager ));
$entityName = 'OnlineFieldEvaluation\Entity\\' . $buildName;
$form->setObject(new $entityName());
$form->bind($data);
//set class and Id for buttons like SaveChanges to reference it
$form->setAttribute('class', "studentFormsClass_$studEvalId");
$form->setAttribute('id', "studentFormsId_$studEvalId" . "_$buildName");
//set buildName to the form
$form->get('buildName')->setAttribute('value', $buildName);
$messages = array();
if ($request->isPost()) {
$formValidatorName = "OnlineFieldEvaluation\Form\\" . $buildName . "FormValidator";
$formValidator = new $formValidatorName();
$form->setInputFilter($formValidator->getInputFilter());
$form->setData($request->getPost());
if (!$form->isValid()) {
$errors = $form->getMessages();
foreach ($errors as $key => $row) {
if (!empty($row) && $key != 'submit') {
foreach ($row as $keyer => $rower) {
//save error(s) per-element that
//needed by Javascript
$messages[$key][] = $rower;
}
}
}
}
if (!empty($messages)) {
$response->setContent(
\Zend\Json\Json::encode(
array('status' => 'error',
'messages' => (array) $messages,
'buildName' => $buildName,
'studEvalId' => $studEvalId
)));
} else {
//save to db <span class="wp-smiley wp-emoji wp-emoji-wink" title=";)">;)</span>
$this->savetodb($form->getData(), $buildName);
$response->setContent(
\Zend\Json\Json::encode(
array(
'status' => 'success',
'messages' => 'Successfuly saved.',
'buildName' => $buildName,
'studEvalId' => $studEvalId
)
));
}
}
return $response;
}

To setup a default value for an element, simply do the following:
Open controller's action, that renders desired view
Instantiate a form, get the element by its name and do call setValue() on that
That looks like as following:
public function addAction()
{
$form = new YourAgencyForm();
$form->get('agencyName')->setValue('Virtual Field Practicum');
....
It's really that simple

Related

How to implement acl and authorization in Module.php in zf3

I am working on Album Application in zf3.I added acl functionality to the application like this:
AlbumController.php
class AlbumController extends AbstractActionController
{
protected $role;
public function onDispatch(\Zend\Mvc\MvcEvent $e)
{
$userSession = new Container('user');
if (!isset($userSession->email)) {
return $this->redirect()->toRoute('login');
}
else {
$this->role = $userSession->role;
parent::onDispatch($e);
}
}
public function checkPermission($role,$action)
{
if($role == 'admin'){
$acl = new Acl();
if ($acl->isAllowed('admin', 'AlbumController', $action)) {
return true;
}
}
return false;
}
public function editAction()
{
$action = 'edit';
$permission = $this->checkPermission($this->role,$action);
if (!$permission) {
$this->flashMessenger()->addMessage('<div class="alert alert- danger" role="alert"><b>You dont have the privilege to edit!!</b></div>');
return $this->redirect()->toRoute('album');
}
$id = (int) $this->params()->fromRoute('id', 0);
if (0 === $id) {
return $this->redirect()->toRoute('album', ['action' => 'add']);
}
try {
$album = $this->table->getAlbum($id);
} catch (\Exception $e) {
return $this->redirect()->toRoute('album', ['action' => 'index']);
}
$form = new AlbumForm();
$form->bind($album);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
$viewData = ['id' => $id, 'form' => $form];
if (! $request->isPost()) {
return $viewData;
}
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
$edit = $request->getPost('submit', 'Cancel');
if($edit == 'Cancel'){
$this->flashMessenger()->addMessage('<div class="alert alert-danger" role="alert"><b>Cancelled by User...!!</b></div>');
return $this->redirect()->toRoute('album');
}
if (! $form->isValid()) {
$this->flashMessenger()->addMessage('<div class="alert alert-danger" role="alert"><b>Failed to Update...!!</b></div>');
return $viewData;
}else{
$this->table->saveAlbum($album);
$this->flashMessenger()->addMessage('<div class="alert alert-success" role="alert"><b>Record Updated Successfully...!!</b></div>');
}
// Redirect to album list
return $this->redirect()->toRoute('album', ['action' => 'index']);
}
}
This is working perfectly fine,now i want to move the onDispatch function to Module.php but don't know how to implement it.Can someone please help me
Module.php
<?php
namespace Album;
use Album\Controller\AlbumController;
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Album\Model\LoginTable;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig()
{
return [
'factories' => [
AlbumTable::class => function($container) {
$tableGateway = $container->get(Model\AlbumTableGateway::class);
return new AlbumTable($tableGateway);
},
Model\AlbumTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
Model\LoginTable::class => function($container) {
$tableGateway = $container->get(Model\LoginTableGateway::class);
$table = new LoginTable($tableGateway);
return $table;
},
Model\LoginTableGateway::class => function ($container){
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
return new TableGateway('login', $dbAdapter, null, $resultSetPrototype);
}
],
];
}
public function getControllerConfig()
{
return [
'factories' => [
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController($container->get(Model\AlbumTable::class));
},
Controller\LoginController::class => function($container) {
return new Controller\LoginController($container->get(Model\LoginTable::class));
},
Controller\LogoutController::class => function($container){
return new Controller\LogoutController($container->get(Model\LoginTable::class));
},
],
];
}
}
This is how I implemented it. In your Module.php, add a listener on EVENT_DISPATCH, with a closure as callback that will call your middleware class authorization :
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function onBootstrap(MvcEvent $e)
{
$app = $e->getApplication();
$eventManager = $app->getEventManager();
$serviceManager = $app->getServiceManager();
// Register closure on event DISPATCH, call your checkProtectedRoutes() method
$eventManager->attach(MvcEvent::EVENT_DISPATCH, function (MvcEvent $e) use ($serviceManager) {
$match = $e->getRouteMatch();
$auth = $serviceManager->get(Middleware\AuthorizationMiddleware::class);
$res = $auth->checkProtectedRoutes($match);
if ($res instanceof Response) {
return $res;
}
}, 1);
// Init ACL : could be improved
$this->initAcl($e);
}
You should have an AuthorizationMiddlewareFactory (call it as you want):
<?php
namespace MyModule\Factory;
use Interop\Container\ContainerInterface;
use MyModule\Middleware\AuthorizationMiddleware;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\Factory\FactoryInterface;
class AuthorizationMiddlewareFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$authService = $container->get(AuthenticationService::class);
$acl = $container->get('Acl'); // II init it in bootstrap(), could be improved
$response = $container->get('Response');
$baseUrl = $container->get('Request')->getBaseUrl();
$authorization = new AuthorizationMiddleware($authService, $acl, $response, $baseUrl);
return $authorization ;
}
}
And your AuthorizationMiddleware class:
<?php
namespace MyModule\Middleware;
use Symfony\Component\VarDumper\VarDumper;
use Zend\Authentication\AuthenticationService;
use Zend\Http\PhpEnvironment\Response;
use Zend\Permissions\Acl\Acl;
use Zend\Router\RouteMatch;
class AuthorizationMiddleware
{
private $authService ;
private $acl;
private $response;
private $baseUrl;
/**
* AuthorizationMiddleware constructor.
* #param AuthenticationService $authService
* #param Acl $acl
* #param Response $response
* #param $baseUrl
*/
public function __construct(AuthenticationService $authService, Acl $acl, Response $response, $baseUrl)
{
$this->authService = $authService;
$this->acl = $acl;
$this->response = $response;
$this->baseUrl = $baseUrl;
}
public function checkProtectedRoutes(RouteMatch $match)
{
if (! $match) {
// Nothing without a route
return null ;
}
// Do your checks...
}
It can be improved, but you have the idea... See also this Question and the answers: ZF3 redirection after ACL authorization failed

Inserting data From Zend's form to database

I have a problem with inserting Records into a DB using Zend FrameWork 2.
There are no records saved into the DB when submitting the Form and also no Error Messages Appear, just the Confirmation Message telling me that it << Registered Successfully >>
Here is my code, thanks in advance:
// App/config/autoload/global.php
return array(
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=zf_app;host=localhost;port:8080',
'username' => 'zf_user',
'password' => 'zf_pass',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' =>'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
#################################################################
//App\module\Users\src\Users\Model\Usertable.php
<?php
namespace Users\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class UserTable
{
protected $tableGateway ;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function saveUser(User $user)
{
$data = array(
'email' => $user->email ,
'name' => $user->name,
'password' => $user->password,
);
$id = (int)$user->id ;
if($id == 0)
{
$this->tableGateway->insert($data);
} else {
if($this->getUser($id))
{
$this->tableGateway->update($data , array('id' => $id));
} else {
throw new \Exception("User ID does not Exist ");
}
}
}
public function getUser($id)
{
$id = (int)$id;
$rowset = $this->tableGateway->select(array('id' => $id ));
$row = $rowset->current();
if(!$row)
{
throw new \Exception("Could not Find row $id ");
}
return $row ;
}
}
##########################################################
// App\module\Users\src\Users\Controller\RegisterController.php
<?php
namespace Users\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Users\Form\RegisterForm;
use Users\Form\RegisterFilter;
use Zend\Db\Adapter\Adapter;
class RegisterController extends AbstractActionController
{
public function indexAction()
{
$form = new RegisterForm();
$viewModel = new ViewModel(array('form' => $form));
return $viewModel;
}
public function confirmAction()
{
$viewModel = new ViewModel();
return $viewModel;
}
public function processAction()
{
if(!$this->request->isPost())
{
return $this->redirect()->toRoute(NULL , array(
'controller' => 'register',
'action' => 'index',
));
}
$post = $this->request->getPost();
$form = new RegisterForm();
$inputFilter = new RegisterFilter();
$form->setInputFilter($inputFilter);
$form->setData($post);
if (!$form->isValid())
{
$model = new ViewModel(array('error' => true , 'form' => $form));
$model->setTemplate('users/register/index');
return $model;
}
return $this->redirect()->toRoute(NULL , array('controller' => 'register' , 'action' => 'confirm' ,));
}
public function createUser(array $data)
{
$sm = $this->getServiceLocator();
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new \Users\Model\User);
$tableGateway = new \Zend\Db\TableGateway\TableGateway('user' , $dbAdapter , null , $resultSetPrototype);
$user = new User();
$user->exchangeArray($data);
$userTable = new UserTable($tableGateway);
$userTable->saveUser($user);
$this->createUser($form->getData());
return true;
}
}
Create // App/config/autoload/local.php
// App/config/autoload/local.php
return array(
'db' => array(
'username' => "zf_user",
'password' => "zf_pass",
)
);
and remove username and password from global.php
What is the User in public function saveUser(User $user) ? You didn't use User file.

ZEND This result is a forward only result set, calling rewind() after moving forward is not supported

I have this error after this code to :
This result is a forward only result set, calling rewind() after moving forward is not supported.
Page.phtml :
foreach ($company as $key => $value)
{
foreach ($userfeature as $key => $data)
{
echo "<tr>";
if($value->site_id == $data->site_id)
{
echo "<td>".$value->party_code." ".$value->party_name. "</td>";
}
}
}
Controller.php
public function indexsiteuserAction()
{
$this->layout('layout/admin');
$compteAdmin[] = $this->admincompte();
$user_id = (int) $this->params()->fromRoute('id', 0);
if (!$user_id)
{
return $this->redirect()->toRoute('administrateur', array('action' => 'adduser'));
}
try
{
$user = $this->getUserTable()->getUser($user_id);
$userfeature = $this->getAdminUserFeatureTable()->afficheruserFeature($user_id);
}
catch (\Exception $ex)
{
return $this->redirect()->toRoute('administrateur', array('action' => 'indexuser'));
}
return new ViewModel(
array(
'user_id' => $user_id,
'user'=> $user,
'userfeature' => $userfeature,
'compteAdmin' => $compteAdmin,
'company' => $this->getCompanyTable()->fetchAll(),
));
}
Userfeaturetable.php
public function getUserFeature($user_id)
{
$user_id = (int) $user_id;
$rowset = $this->tableGateway->select(array('user_id' => $user_id));
$row = $rowset->current();
if (!$row)
{
throw new \Exception("Could not find row $user_id");
}
return $row;
}
companyTable.php
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet ;
}
Why do not I have the right to embed my two ' foreach ()' ?
Your need to buffer the result.
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
$resultSet->buffer();
return $resultSet ;
}

Can't get bind() to work

I want my form fields to contain the previous data contained in database when the form page opens. I went through lots of queries here and came to know using populate() or bind() method is the way to do it. But when I try to use it, I get an undefined method error.
Is there any other way to do it?
I am unable to use bind() as well. I am getting a fresh form with default values after I submit.
Sorry if this is a stupid question. Its been only 4-5 days since I started learning Zend framework. Also, most of the methods I get online are for older frameworks. I am using Zend Framework2.
This is Controller Code
<?php
class ChatController extends AbstractActionController
{
protected $chatTable;
public function indexAction()
{
$form = new ChatForm();
$model= new Chat();
$form->bind($model);
$form->get('submit')->setValue('Save');
$request = $this->getRequest();
if ($request->isPost()) {
$gen_set = new Chat();
$form->setInputFilter($gen_set->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$gen_set->exchangeArray($form->getData());
$this->getChatTable()->saveChat($gen_set);
// Redirect to list of albums
return $this->redirect()->toRoute('chat');
}
}
return array('form' => $form);
}
public function getChatTable()
{
if (!$this->chatTable) {
$sm = $this->getServiceLocator();
$this->chatTable = $sm->get('Chat\Model\ChatTable');
}
return $this->chatTable;
}
}
My Entity Class, Here api_key and anon_prefix are rows of the column 'settings' and there is one more column with value.
<?php
class Chat implements InputFilterAwareInterface
{
protected $inputFilter;
public function exchangeArray($data)
{
$this->api_key=(isset($data['api_key'])) ? $data['api_key'] : null;
$this->anon_prefix = (isset($data['anon_prefix'])) ? $data['anon_prefix'] : null;
}
// Add content to these methods:
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'iflychat_external_api_key',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
));
$inputFilter->add(array(
'name' => 'iflychat_show_admin_list',
'required' => true,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => array(1,2),
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
}
This is used to enter values into db
<?php
class ChatTable
{
protected $tableGateway;
public function __construct(TableGateway $tableGateway)
{
$this->tableGateway = $tableGateway;
}
public function fetchAll()
{
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function saveChat(Chat $gen_set)
{
$data = array(
'value' => $gen_set->api_key,
);
$id='iflychat_external_api_key';
$this->tableGateway->update($data,array('settings' => $id));
$data = array(
'value' => $gen_set->anon_prefix,
);
$id='anon_prefix';
$this->tableGateway->update($data,array('settings' => $id));
}
}
I am getting this error, 'Cannot use object of type Chat\Model\Chat as array'
Your action doesn't make much sense as it is, you instantiate a Chat instance as $model and later another instance as $gen_set. What you should be doing is binding the first one, and using the form class getData method to later return the instance you bound to it, along with the values you gave it in the setData method. There's no need for any transformations from object to array and back again.
Here's how it should look ...
public function indexAction()
{
$form = new ChatForm();
// bind the model
$model= new Chat();
$form->bind($model);
$form->get('submit')->setValue('Save');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($gen_set->getInputFilter());
// set data from POST as properties of the bound model ...
$form->setData($request->getPost());
if ($form->isValid()) {
// get the bound model instance with the POSTed values
// ($gen_set is now the original $model object instance bound above)
$gen_set = $form->getData();
// and save it
$this->getChatTable()->saveChat($gen_set);
// Redirect to list of albums
return $this->redirect()->toRoute('chat');
}
}
return array('form' => $form);
}
Controller Code -
<?php
class ChatController extends AbstractActionController {
protected $chatTable;
public function indexAction() {
$model = $this->getChatTable()->fetchLastChat();
if($model === null || $model->count() == 0)
$model = new Chat();
//Now if no record exists in the database then $model will be empty
//Else $model will contain data of last record.
$form = new ChatForm();
$form->bind($model);
$form->get('submit')->setValue('Save');
$request = $this->getRequest();
if ($request->isPost()) {
$gen_set = new Chat();
$form->setInputFilter($gen_set->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$gen_set->exchangeArray($form->getData());
$this->getChatTable()->saveChat($gen_set);
}
}
return array('form' => $form);
}
public function getChatTable() {
if (!$this->chatTable) {
$sm = $this->getServiceLocator();
$this->chatTable = $sm->get('Chat\Model\ChatTable');
}
return $this->chatTable;
}
}
ChatTable Class Code -
<?php
//Other use statements
use Zend\Db\Sql\Select;
class ChatTable {
protected $tableGateway;
public function __construct(TableGateway $tableGateway) {
$this->tableGateway = $tableGateway;
}
public function fetchAll() {
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function fetchLastChat() {
$select = new Select('TABLE_NAME'); //Change the tablename accordingly
$select->order('PRIMARY_KEY DESC'); //Set the Primary Key of the table
$select->limit(1);
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet->current();
}
//Rest of the Code ....
Please take the idea from the above code.

How to forward data to another action using Forward Plugin and read it there in Zend Framework 2?

I have two actions in a controller: actionA() and actionB(). Dependent on a condition the actionA() should return a ViewModel object or be forwarded to actionB() (and return its result):
class MyController extends AbstractActionController {
public function aAction() {
...
$data = ...
...
if (...) {
$result = new ViewModel(array(
'data' => $data,
));
} else {
$result = $this->forward()->dispatch('MyModule\Controller\My', array(
'action' => 'b',
));
}
return $result;
}
I tried it with
$result = $this->forward()->dispatch('MyModule\Controller\My', array(
'action' => 'b',
'data' => $data,
));
But I have no idea, how to fetch this data now.
I'm sure, it's possible. How can I do it?
public function bAction() {
...
// so:
$params = $this->params()->fromRoute();
// or so:
$params = $this->getEvent()->getRouteMatch()->getParams();
...
}

Resources