I'm trying to achieve a thank you page after submitting a form in Zend 1.12
in the index i have form and i want if the validation passes then should go to another view (NOT INDEX) for thank you page. how can i do this in my code:
public function indexAction()
{
// action body
$C_form = new Application_Form_Eform();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($C_form->isValid($formData)) {
$this->_helper->redirector('','result');
exit;
} else {
$C_form->populate($formData);
}
}
$this->view->form = C_eform;
}
and after that where should i create the .phtml file? in the application\views\scripts\index?
I think you're looking for render:
$this->view->render('index/yourotherview.phtml');
In this case, index/ is referring to your views/scripts/index folder, and the yourotherview.phtml file.
So, all together it would be:
public function indexAction()
{
// action body
$C_form = new Application_Form_Eform();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($C_form->isValid($formData)) {
$this->_redirect('/index/result);
} else {
$C_form->populate($formData);
}
}
$this->view->form = C_eform;
}
EDIT:
From your comment it looks like you just want to be redirected instead of displaying a different view. In this case, it's as easy as creating a new action and making the view for it:
public function resultAction() {
// Code here if you need it
}
Then create the file result.phtml in the index/ views directory, and you'll need $this->_redirect('/index/result'); in the index controller. (See above code)
Related
In cakePHP 4
I have a controller and view.php connected with it.
I can use a route like this: sitename.com/projects/45, where 45 - is sample project ID.
Using this url I can reach a page with the content of particular project. But If I want to construct something like a page of settings of this project, how I have to do it?
For example via url sitename.com/projects/45/settings
Help please
It's simple:
// sitename.com/projects/45
// public function view($id) { ... }
// sitename.com/projects/45/settings
public function view($id, $passed = null) {
if($passed == 'settings') {
// do ...
}
}
or
public function view($id) {
$passed = $this->getRequest()->getParam('pass');
if (in_array('settings', $passed)) {
// do ...
}
}
Sorry for my English, but what I'm trying to say is explained below.
I have a controller say ControllerCard which has an action like this.
function actionScanCard()
{
...
$this->redirect('/transaction/redeem');
...
}
In other controllers, ControllerTransaction, I am trying to get that it comes/redirected from /card/scan-card
function actionRedeem()
{
$redirectFrom = ????;
if ($redirectFrom === '/card/scan-card')
{
// some actions
}
else
throw new ForbiddenHttpException('Must scan card!');
}
How do I get this $redirectFrom value with Yii2?
You could use the remember() & previous() methods in yii\helpers\BaseUrl.
function actionScanCard()
{
...
\yii\helpers\Url::remember();
$this->redirect('/transaction/redeem');
...
}
in TransactionController (or other)
function actionRedeem()
{
$url = \yii\helpers\Url::previous();
if($url === Url::to('card/scan-card')) {
// some actions
} else{}
}
I am using Zend 1.12 and php 5.4.3 and flashMessenger->getMessages() has suddenly stopped working in a controller action.
In AController, a certain type of account is created and it takes 9 steps to create it, so I have 9 actions create1-9Action
On each step I pass the form data to the next step using flashmessenger.
This is the typical structure of an action:
public function create5Action()
{
$form = new My_Form();
$messages = $this->_helper->flashMessenger->getMessages();
$data = $messages[0];
if ($this->_request->isPost())
{
if ($form->isValid($this->_request->getPost()))
{
/* form treatment */
$this->_helper->flashMessenger->addMessage($data);
$this->_redirect($this->_helper->url("create6", "A", null)); // redirect to next step
}
}
$this->_helper->flashMessenger->addMessage($data);
$this->view->form = $form;
}
In this action (create5) the data is intact when arriving from create4Action, it is intact when adding it as a message before $this->view->form = $form;, but when I add new elements to the form and submit it, $messages = $this->_helper->flashMessenger->getMessages(); is null and I do not know why, since it is working for all other actions.
You may have missed adding the else in the isPost() loop.
public function create5Action()
{
$form = new My_Form();
$messages = $this->_helper->flashMessenger->getMessages();
$data = $messages[0];
if ($this->_request->isPost())
{
if ($form->isValid($this->_request->getPost()))
{
/* form treatment */
$this->_helper->flashMessenger->addMessage($data);
$this->_redirect($this->_helper->url("create6", "A", null)); // redirect to next step
}
} else {
$this->_helper->flashMessenger->addMessage($data);
$this->view->form = $form;
}
}
However this is not the anticipated use of the flash messenger. I think a part of your problem is that every time a request is made the flash messenger namespace is cleared during the postDispatch() portion of the dispatch loop.
You may be better off using your own Zend_Session_Namespace instance instead of relying on the instance that flash messenger is using.
public function create5Action()
{
$session = new Zend_Session_Namespace('data');//set elsewhere and forwarded and can be persistent
$form = new My_Form();
$data = $session->data;
if ($this->_request->isPost())
{
if ($form->isValid($this->_request->getPost()))
{
/* form treatment */
$session->newData = $newData;//forward data if needed, old data will persist as you require
$this->_redirect($this->_helper->url("create6", "A", null)); // redirect to next step
}
} else {
$this->view->form = $form;
}
}
I am using Kohana 3.2 and I am having problems calling the ouput of a controller in another controller.
What I want...
In some pages I have got a menu, and in others I don't. I want to use make use of the flexability of the HMVC request system. In the controller of a page I want to call another controller which is responsible for the creation of the menu.
What I have a the moment:
file menu.php:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Menu extends Controller
{
private $_model = null;
public function __construct(Request $request, Response $response)
{
parent::__construct($request, $response);
$this->_model = Model::factory('menu');
}
public function action_getMenu()
{
$content = array();
$content['menuItems'] = $this->_model->getMenuItems();
// Render and output.
$this->request->response = View::factory('blocks/menu', $content);
//echo '<pre>'; print_r($this->request->response->render()); echo '</pre>'; die();
}
}
somepage.php
public function action_index()
{
$this->template->title = 'someTitle';;
$contentData['pageTitle'] = 'someTitle';
$contentData['contentData'] = 'someData';
#include the menu
$menuBlock = Request::factory('menu/getMenu')->execute();
$menuData = array('menu' => $menuBlock);
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
$this->template->content = View::factory('pages/somePage', $contentData);
$view = $this->response->body($this->template);
$this->response->body($view);
}
If I uncomment the following line in menu.php, I see the menu rendered:
//echo '<pre>'; print_r($this->request->response->render()); echo '</pre>'; die();
So I guess that part is alright. The problem is in the following line in somepage.php:
$menuBlock = Request::factory('menu/getMenu')->execute();
This gives me back a response object. Whatever I do, I do not get the output in $this->template->menu.
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
What must I do to have $this->template->menu contain the view, so I can use it correctly?
I hope this all makes sense. This is the way I would like to do it, but maybe I am completely on the wrong track.
I would do it this way:
class Controller_Menu extends Controller
{
public function action_build()
{
// Load the menu view.
$view = View::factory('navigation/menu');
// Return view as response-
$this->response->body($view->render());
}
}
In your controller get the menu as follows:
// Make request and get response body.
$menu = Request::factory('menu/build')->execute()->body();
// e.g. assign menu to template sidebar.
$this->template->sidebar = Request:.factory('menu/build')->execute()->body();
I would not use the __construct method in your controllers. Use before() instead, this is sufficient for most of the problems (for example auth):
public function before()
{
// Call aprent before, must be done here.
parent::before();
// e.g. heck whether user is logged in.
if ( !Auth::instance()->logged_in() )
{
//Redirect if not logged in or something like this.
}
}
I found the answer to my problem in less than an hour after asking.
I just forgot to put it here.
In somePage.php change :
$menuBlock = Request::factory('menu/getMenu')->execute();
$menuData = array('menu' => $menuBlock);
$this->template->menu = View::factory('pages/menu')->set('menu',$menuData);
To:
$this->template->menu = Request::factory('menu/getMenuBlock')->execute()->body();
And in menu.php change:
$this->request->response = View::factory('blocks/menu', $content);
To:
$request = View::factory('blocks/menu', $content);
$this->response->body($request);
I hope this will help someone else.
Once you're OK with basic record form built after example from Tutorial, you realize you want more professionally designed Record Form. E.g. I don't want to duplicate record form for the same table in User and Admin areas.
1) Does anyone use some mechanism, possibly inheritance, to reduce duplication of almost similar admin and user forms? Is that burdensome or sometimes you better just do with copy-pasting?
2) Has anyone considered it to be a good idea to build some basic Record class
that can determine that among several record forms on this page, the current post is addressed specifically to this record form
that can distinguish between Edit or Delete buttons clicks in some organized fashion.
3) My current practice includes putting all form config code (decorators, validations, initial values) into constructor and form submit handling is put into a separate ProcessSubmit() method to free controller of needless code.
All the above addresses to some expected Record Form functionality and I wonder if there is any guideline, good sample app for such slightly more advanced record handling or people are still reinveting the wheel. Wondering how far you should go and where you should stop with such impovements...
Couple of suggestions:
First of all - Use the init() function instead of constructors to add your elements when you are subclassing the form. The init() function happens after the parameters you pass to the class are set.
Second - Instead of subclassing your form - you can just set an "option" to enable the admin stuff:
class My_Record_Form extends Zend_Form {
protected $_record = null;
public function setRecord($record) {
$this->_record = $record;
}
public function getRecord() {
if ($this->_record === null || (!$this->_record instanceOf My_Record)) {
throw new Exception("Record not set - or not the right type");
}
return $this->_record;
}
protected $_admin = false;
public function setAdmin($admin) {
$this->_admin = $admin;
}
public function getAdmin() { return $this->_admin; }
public function init() {
$record = $this->getRecord();
$this->addElement(......);
$this->addElement(......);
$this->addElement(......);
if ($this->getAdmin()) {
$this->addElement(.....);
}
$this->setDefaults($record->toArray());
}
public function process(array $data) {
if ($this->isValid($data)) {
$record = $this->getRecord();
if (isset($this->delete) && $this->delete->getValue()) {
// delete button was clicked
$record->delete();
return true;
}
$record->setFromArray($this->getValues());
$record->save();
return true;
}
}
}
Then in your controller you can do something like:
$form = new My_Record_Form(array(
'record'=>$record,
'admin'=>My_Auth::getInstance()->hasPermission($record, 'admin')
));
There is nothing "wrong" with making a My_Record_Admin_Form that handles the admin stuff as well - but I found this method keeps all the "record form" code in one single place, and a bit easier to maintain.
To answer section 2: The edit forms in my code are returned from a function of the model: $record->getEditForm() The controller code ends up looking a little like this:
protected $_domain = null;
protected function _getDomain($allowNew = false)
{
if ($this->_domain)
{
return $this->view->domain = $this->_domain;
} else {
$id = $this->_request->getParam('id');
if (($id == 'new' || $id=='') && $allowNew)
{
MW_Auth::getInstance()->requirePrivilege($this->_table, 'create');
$domain = $this->_table->createRow();
} else {
$domain = $this->_table->find($id)->current();
if (!$domain) throw new MW_Controller_404Exception('Domain not found');
}
return $this->view->domain = $this->_domain = $domain;
}
}
public function editAction()
{
$domain = $this->_getDomain(true);
MW_Auth::getInstance()->requirePrivilege($domain,'edit');
$form = $domain->getEditForm();
if ($this->_request->isPost() && $form->process($this->_request->getPost()))
{
if ($form->delete && $form->delete->getValue())
{
return $this->_redirect($this->view->url(array(
'controller'=>'domain',
'action'=>'index',
), null, true));
} else {
return $this->_redirect($this->view->url(array(
'controller'=>'domain',
'action'=>'view',
'id'=>$form->getDomain()->id,
), null, true));
}
}
$this->view->form = $form;
}
So - the actual id of the record is passed in the URI /domain/edit/id/10 for instance. If you were to put multiple of these forms on a page - you should make sure to set the "action" attribute of the form to point to an action specific to that form.
I created a SimpleTable extends Zend_Db_Table and SimpleForm extends Zend_Db_Form classes. Both of these assume that your table has an auto-incrementing ID column.
SimpleTable has a saveForm(SimpleForm $form) function which uses the dynamic binding to match form element names to the columns of the record. I also included an overridable saveFormCustom($form) for any special handling.
The SimpleForm has an abstract setup() which must be overridden to setup the form. I use the init() to do the initial setup (such as adding the hidden ID field).
However, to be honest, I really don't like using the Zend_Form object, I feel like that should be handled in the View, not the Model or Controller.