I am working on a search form that I would like to post the searched by values in the url. I am having trouble getting the url to include the parameters however. They will post if I key in values in the view( if instead of $this->search_zip I key '12345'). Currently the search works as desired except for the url. I am currently getting the search terms from the form, would I need to change my controller setup to get them from the url instead? If this is the case how would I filter?
Ultimately I would like my url to read:
results/12345/otherparam
I am currently getting
results
No matter what variables I key into the form.
Module Config
return array(
'router' => array(
'routes' => array(
'home' => array(
'type' => 'segment',
'options' => array(
'route' => '/',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
'may_terminate' => true, //START OF CHILD ROUTES
'child_routes' => array(
'results' => array(
'type' => 'segment',
'options' => array(
'route' => 'results[/:search_zip][/:search_industry]',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'results',
Results view
$form->setAttribute('action', $this->url(
'home/results',
array(
'action' => 'results',
'search_zip'=> $this->search_zip,
'search_industry' => 'industry_name'
echo $this->formRow($form->get('industry_name'));//this is the form field
echo $this->formSubmit($form->get('submit'));
Controller
//beginning of the results action
$request = $this->getRequest();
$form = new SearchForm($dbAdapter);
if ($request->isPost()) {
$search = new MainSearch();
$form->setInputFilter($search->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
At the end of my resultsAction I return the form and the results (per the album example)
return array(
'form' => $form,
'pros' => $fetchPros,
);
Thank you,
M
//This will give you an array containing your desired parameters
$params = $this->params()->fromRoute();
//Then you can simply use them like this
$search_zip = $params['search_zip'];
$search_industry = $params['search_industry'];
Related
Why is Zend 2 such a !##(#(!##??
OK, so I'm trying to get a simple redirect working. I have a controller called 'listitems' with an action called 'editlistitem'. After hours of banging on it with a hand sledge, I've finally got the form to work and the validation to work and the hydration to Doctrine to work so I can save the result.
The last step is to redirect the user to the 'showlistitem' action which includes the id trailing it. (full route sub path is 'listitem/showlistitem/2' where 2 is the id I want to see)
I have tried:
$this->redirect()->toRoute('/listitem/showlistitem/2');
$this->redirect()->toRoute('listitem/showlistitem/2');
$this->redirect()->toRoute('showlistitem/2');
$this->redirect()->toRoute('listitem/showlistitem', array('id' => 2));
$this->redirect()->toRoute('listitem-showlistitem', array('id' => 2));
None of them flippin work! (they all return route not found)
A route to the controller is in modules.config.php with a child route to the action. I can go directly to the url by typing it in manually and it works fine. How in the bleep do I get Zend to redirect the user to that route from an action?
The toRoute method provided by the The Redirect plugin needs the route name to be passed as parameter. This is its desciption :
toRoute(string $route = null, array $params = array(), array $options = array(), boolean $reuseMatchedParams = false)
Redirects to a named route, using the provided $params and $options to assembled the URL.
Given this simple route configuration example :
//module.config.php
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Segment',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'index',
'action' => 'index',
),
),
),
'app' => array(
'type' => 'Literal',
'options' => array(
'route' => '/app',
'defaults' => array(
'controller' => 'index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id'=>'[0-9]+',
),
),
),
),
),
),
),
This redirection works :
return $this->redirect()->toRoute('app/default',
array('controller'=>'controller-name', 'action'=>'action-name', 'id'=>$id));
In your case, this would work :
return $this->redirect()->toRoute('app/default',
array('controller'=>'listitem', 'action'=>'showlistitem', 'id'=>2));
In module.config.php
...
'may_terminate' => true,
'child_routes' => array(
'resetpassword' => array(
'type' => 'Literal',
'options' => array(
'route' => '/reset-password',
'defaults' => array(
'controller' => 'User\Controller\Index',
'action' => 'resetpassword',
),
),
),
)
...
And form reset password .phtml
<?php
$user_id = 1;
$token = 'ABCXYZ'
$form = $this->form;
$form->prepare();
$form->setAttribute('action', $this->url('user/resetpassword?user_id='.$user_id.'&token='.$token));
$form->setAttribute('id', 'reset-password-form');
?>
If I set $form->setAttribute('action', $this->url('user/resetpassword')); is OK, but when set params is user_id with token => Error
How to fix it?
Because the first argument of Url view helper is route name. If you want add some query params, you can use third argument ($options - see documentation)
Example:
$url = $this->url('user/resetpassword', [], ['query' => ['user_id' => $user_id]]);
I have a /files controller that will have two actions: upload and download
it's defined as follows:
'files' => array(
'type' => 'Segment',
'options' => array(
'route' => '/files[/:action]',
'defaults' => array(
'controller' => 'Application\Controller\Files',
'action' => 'index',
),
),
),
I want the download action to be accessed like /files/download/1?authString=asdf. 1 in this case is a fileId.
I understand I can just change the route to /files[/action[/:fileId]] to set up the route, correct me if I'm wrong, but then how do I access the fileId inside the downloadAction? And is there anything else I need to change about the route definition to make it work??
I can just change the route to /files[/action[/:fileId]] to set up the route, correct me if I'm wrong
You're not wrong, that would be a valid route.
is there anything else I need to change about the route definition?
If you add the fileId as a optional route param then you will need to do some manual checking within the downloadAction() to ensure it is set.
Another solution would be to separate the routes into children, this ensures that unless you have the correct parameters on each route, it would not be matched.
'files' => array(
'type' => 'Segment',
'options' => array(
'route' => '/files',
'defaults' => array(
'controller' => 'Application\Controller\Files',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'download' => array(
'type' => 'Segment',
'options' => array(
'route' => '/download/:fileId',
'defaults' => array(
'action' => 'download',
),
'constraints' => array(
'fileId' => '[a-zA-Z0-9]+',
),
),
),
'upload' => array(
'type' => 'Literal',
'options' => array(
'route' => '/upload',
'defaults' => array(
'action' => 'upload',
),
),
),
),
),
how do I access the fileId inside the downloadAction
The easiest way would be to use the Zend\Mvc\Controller\Plugin\Params controller plugin to fetch the parameter from the route).
// FilesController::downloadAction()
$fileId = $this->params('fileId');
Or specifically from the route
// FilesController::downloadAction()
$fileId = $this->params()->fromRoute('fileId');
I'm trying to redirect the user using this code:
return $this->redirect()->toRoute('application', array(
'controller' => 'Index',
'action' => 'connexion',
null,
array('e' => 'n'),
));
And getting from the layout the content of the e param this way:
$_REQUEST['e']
But doing that I don't catch anything. How do I to get it please?
Thanks in advance!
Matched Route params in view :
$this->getHelperPluginManager()
->getServiceLocator()
->get('Application')
->getMvcEvent()
->getRouteMatch()
->getParams()
Request Query/Post in view :
$this->getHelperPluginManager()
->getServiceLocator()
->get('Request')
->getQuery()->toArray()
$this->getHelperPluginManager()
->getServiceLocator()
->get('Request')
->getPost()->toArray()
As mentioned in the comments of your question, the way to go is: $this->params()->fromRoute(); mentioned by #Notuser. Will use it in a simple example for your use as you're passing the parameters to the view.
class ExampleController extends AbstractActionController
{
public function rerouteAction()
{
// Notice that 'param' is a route within our route.config.php and in there we
// define the controller and action, so we do not need to set the controller
// and action in the redirect. So param now points to paramAction of ExampleController.
return $this->redirect()->toRoute('param', array('e' => 'n'));
}
public function paramAction()
{
// Leaving fromRoute() blank will return all params!
$params = $this->params()->fromRoute();
$e = $params['e'];
return array('e' => $e);
}
}
So in your view.phtml you can now easily do <?php echo $this->e; ?> which should contain: n.
The route.config of the above example will look something like this:
return array(
'router' => array(
'routes' => array(
'reroute' => array(
'type' => 'segment',
'options' => array(
'route' => 'reroute',
'defaults' => array(
'controller' => 'Application\Controller\ExampleController',
'action' => 'reroute'
)
)
),
'param' => array(
'type' => 'segment',
'options' => array(
'route' => 'param',
'defaults' => array(
'controller' => 'Application\Controller\ExampleController',
'action' => 'param'
)
)
)
)
)
);
I am a Zend Framework beginner.
I guess My question is very basic... but I can't solve it by myself.
In indexAction, $request->isPost() is always false.
What is happening?
EntryController::indexAction
public function indexAction() {
$form = new AgreementForm();
$form->get('submit')->setValue('Go Entry Form');
$request = $this->getRequest();
if ($request->isPost()) {
var_dump('//// $request->isPost() is true //////');
if ($form->get('agreementCheck')) {
// Redirect to list of entries
return $this->redirect()->toRoute('entry');
} else {
return array('form' => $form);
}
} else {
var_dump('//// $request->isPost() is false //////');
return array('form' => $form);
}
}
form in index.phtml
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('entry', array('action' => 'index')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formCheckbox($form->get('agreementCheck'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
AgreementForm is generated using code generator.
http://zend-form-generator.123easywebsites.com/formgen/create
as below.
class AgreementForm extends Form {
public function __construct($name = null) {
parent::__construct('');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'agreementCheck',
'type' => 'Zend\Form\Element\MultiCheckbox',
'attributes' => array(
'required' => 'required',
'value' => '0',
),
'options' => array(
'label' => 'Checkboxes Label',
'value_options' => array(
'0' => 'Checkbox',
),
),
));
$this->add(array(
'name' => 'csrf',
'type' => 'Zend\Form\Element\Csrf',
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
Please tell me some hints.
update:
In the result of analysis by Developer Tools, POST and GET works at the same time.
update:
router definition #module.config.php is this.
'router' => array(
'routes' => array(
'entry' => array(
'type' => 'segment',
'options' => array(
'route' => '/entry[/][:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Entry\Controller\Entry',
'action' => 'index',
),
),
),
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Entry\Controller\Entry',
'action' => 'index',
),
),
),
),
),
A few things are wrong:
In the form class you add a csrf element, but you don't render it in the view. This will cause a validation error. So you need to add this to your view:
echo $this->formHidden($form->get('csrf'));
You're adding a Multicheckbox element to the form, but in your view you're using the formCheckbox view helper to render it. If you really want a Multicheckbox then you should render it with the formMultiCheckbox helper:
echo $this->formMultiCheckbox($form->get('agreementCheck'));
After these changes it should work.
Edit: Also you may want to pass a name to the form constructor:
parent::__construct('agreementform');
I think, you do not need to explicitly say
$form->setAttribute('action', $this->url('entry', array('action' => 'index')));
Omit that line, and see what happens.