Setting action parameter to form in Zend - php

Im using "Zend form". What should I do to get correct "action" parameter in my FORM tag?
For example, the form Form_Search used in SearchController in indexAction. Where and how should I set the action parameter to form to make it "/my/app/directory/search/index"?
Update: "action" parameter should be the same as the form will assume if not set. I just need the param in FORM to simplify JS coding.

You can use setAction method of Zend_Form, e.g.:
$yourForm->setAction('/my/app/directory/search/index');
Hope this is what you are looking for.
EDIT:
You can retrieve many info about the request, in an action, using $this->getRequest() method that returns an instance of Zend_Controller_Request_Http. For example, if to get basePath and everything between the BaseUrl and QueryString you can do:
$infoPath = $this->getRequest()->getPathInfo();
$basePath = $this->getRequest()->getBaseUrl();
$yourForm->setAction($basePath . $infoPath);
Zend_Controller_Request_Http has other methods that might be useful to you.

You can use the url() view helper to create your action urls.
$view->url(array(
'module' => 'mymodule',
'controller' => 'mycontroller',
'action' => 'myaction',
'param1' -> 'myvalue1',
// etc
), 'default');
// 'default' here refers to the default route.
// If you specify a custom route, then that route will be used to
// assemble() the url.
Depending where you call $form->setAction(), you can access to the $view in different ways.
In the controller: $this->view
In the form itself: $this->getView()
In a view script: $this
In view helper extending Zend_View_Helper_Abstract: $this->view
Hope it helps!

Just an addition to previous answers. Kind of "industry standard approach" is to render/validate/process the form all on the same page. Then there is no need to set the action as it's filled with current URL.
Exception is for example serch field in layout. Then use $form->setAction($url). But always remember to echo the form in the search action. If the form value is invalid, ZF would expect you to render it in page with the errors. Or you can use $form->getErrors().

`$this->setAttribs(array(
'accept-charset' => 'utf-8',
'enctype' => Zend_Form::ENCTYPE_URLENCODED,
'method' => 'get',
'action' => '/controller/action/param/attr'
))`

Related

How to include multiple action&controller in Zend Framework 2

I would like to include multiple views within a view in ZF2.
I read this link:
http://framework.zend.com/manual/current/en/modules/zend.view.quick-start.html
but there is a problem. In this way, I have to pass the values that are within a view, like this:
$secondView = new ViewModel (array('var1' => $var1 ......));
In this mode the controller and second action are bypassed.
Is there a way to include a view without bypassing them? I would like variables to be passed from the second action controller, like an include php statement
If I understood correctly your question, I think that what you're asking is not possible.
My suggestion would be to move the retrieval of the data you need for the secondView somewhere else, away from the controller, and call it both from the second controller and form the first controller to pass them to the secondView.
If you really want to go on with your approach, the only possibility I see is to use javascript and ajax calls to retrieve the partials you need in your view
You can use partials for that.
In your module.config.php under the 'view_manager' key you define a template map for your partial:
'view_manager' => array(
'template_map' => array(
'myPartial' => __DIR__ . '/../view/mymodule/partial/myPartial.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
Then in the view container you can use that partial using the partial() View Helper:
<div><?php echo $this->partial('myPartial', array('var1' => 'value1'); ?></div>
You can also pass variables to your partial. Those variables are referenced in your partial like any other view variable:
echo $var1;

Getting parameters from URL in Zend

In a Zend application with an example url like this one:
http://example.com/profile/423423(some id)
How do I get param from url?
I try to use:
$this->getRequest()->getParam('action');
But I get action does not exist.
then I try something like this:
protected function _initRouter()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('profile/:catid', new Zend_Controller_Router_Route('profile/:catid',
array(
'module' => 'default',
'controller' => 'profile',
'action' => 'index' // Check your action and controller
)));
}
How do I get param from url?.
You can use the getParam() methods to retrieve request variables like route, GET and POST.
Inside your controllers action you can do this.
$this->getParam('parameterName'); or $this->getRequest()->getParam('parameterName');
If you want to get the name of action and controller that matched the current route you need to use following methods.
$this->getRequest()->getActionName() and $this->getRequest()->getControllerName()
You should just use
$this->getRequest()->getParam('profile');
from your controller.
You shouldn't need to use a custom route.
Kind regards
Garry
EDIT
I have just noticed that you wish to use the profile controller. The simplest solution would be to add an id into your url like.
http://example.com/profile/id/423423
and get the profile id with
$this->getRequest()->getParam('id');

PHP_SELF in yii action

How do you add in the yii form the action for PHP_SELF, I am currently using this:
'action' => '/site/contact',
but I want to replace the action to call itself again. How do I do that?
UPDATE:
tried removing the action in the array, now it does not go into validating the form.
Use Yii controller method createUrl - calling it without params will point to current controller and action.
In views you can just call $this->createUrl():
'action' => $this->createUrl(),
Important: do not hardcode actions like in your question. Always call createUrl, this will ensure proper urls if you change url rules:
'action' => $this->createUrl('otherController/someAction'),
or same controller different action
'action' => $this->createUrl('otherAction'),
etc.
Do mean perhaps a page refresh?
Just do $this->refresh() inside your controller action.

Getting parameter from url in Zend

My route setup :
Zend_Controller_Front::getInstance()
->getRouter()
->addRoute('view', new Zend_Controller_Router_Route('controller/action/:name'))
My link in view :
$this->url(array("name" => "John"), "view", TRUE);
// returns "controller/action/John" as should
Now, when I am at controller/action/John, how do I get the name from URL ? I tried
$this->getRequest()->getParam("name");
but the name param isn't there - getRequest() returns only controller, action and module params.
When you set up your route configuration the route definition should either directly match the controller/action names or be set with defaults. Actually setting the defaults in any case is just good practice and helps you avoid issues like this.
So, in your case according to the comments your route should probably look like this.
$defaults = array(
'controller'=> 'offers',
'action' => 'view',
'name' => ''
);
$route = new Zend_Controller_Router_Route('offers/view/:name',$defaults);
As mentioned in the comments you can always check what route has been used with Zend_Controller_Front::getInstance()->getRouter()->getCurrentRouteName(). If it doesn't show your expected route the Router isn't able to find a match and moves on until it usually ends in the "default" route.
As a side note to your question: When you use $this->url(array("name" => "John"), "view", TRUE) you only create the link based on the route. This method is only part of the view and does nothing in terms of dispatching to a controller or action.
For those who found this question and for future reference, you can get params from route using this : $this->params()->fromRoute('param1', 0); in Zend Framework 2 at least. This is what I was looking for in this question.

detecting variable type in zend router variables

so I'm using zend_router to route my index page in my controller to an action other than the index action:
$route = new Zend_Controller_Router_Route(
'/controller/:page',
array(
'action' => 'not-index',
'controller' => 'controller',
'page' => 1,
),
);
And the reason why I'm adding :page into the path is because there's pagination in that index and I'd like to have paginator to work even if the user only enters '/controller' into the browser, so it's got to be there (see: http://www.joewebster.co.uk/articles/zend-framework/7-zend-pagination-and-zend-router)...
the problem is....now other actions within the controller will redirect to that page as well and not to the proper action
so suppose I enter '/controller/other-action' to the browser, it goes to 'controller/not-index' with page = other-action instead of 'controller/other-action' due to that routing definition....is there a way to limit the :page in that router definition to integers only, ie. if I enter '/controller/2' then go to 'controller/not-index' with page = 2, and if I enter '/controller/other-action' it'll actually go to 'controller/other-action'
I think you might want to take a look at the Regex_router:
http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.regex
That should help you achieving your goal. I'm afraid the standard Router isn't flexible enough.

Categories