Zend url : get parameter always stay in the url - zend-framework

I have some trouble using the Zend url helper with get parameter.
In a view, I have pagination which send extra parameters in get (so in the url), so that's ok. But that is not ok is that the parameters always stay in the url even if I change page.
In fact, the zend url helper - I use to generate the url of link or form's action - add automaticaly the parameter at the end of the url so whatever the link I click, I have this parameters...
//In my controller
$this->_view->url(array("action"=>"action-name");
// generate for example : "mywebsite/controller-name/action-name/pays/4" but I don't want the "/pays/4"
Thank you for your help

The url method accepts additional parameters. One of them resets the get-string parameters.
url (
array $ urlOptions = array(),
$ name = null,
$ reset = false,
$ encode = true
)
Generates an url given the name of a route.
Parameters:
array $urlOptions - Options passed to the assemble method of the Route object.
mixed $name - The name of a Route to use. If null it will use the current Route
bool $reset - Whether or not to reset the route defaults with those provided
Returns:
string Url for the link href attribute.
It's all in the doc. The above is for ZF version 1.10

The definition or url() is
public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
So try setting the third parameter ($reset) to true

Related

what is wrong with my $this->redirect(...) call in this code?

I have two extensions, I want to redirect from one to another in a certain action. This is my redirect code in the saveAction of my bpsmessagecontroller of my bpsmessagecentre extension:
$this->redirect('list', 'Coupon', 'Bpscoupons', array('message' => 'Look Ma, no syntax errors!' ));
When that runs it just redirects back to they list page of the calling extension (bpsmessagecenter), it doesn't seem to find the Bpscoupons extension at all.
When I try the same code with a forward call instead of redirect I get a 500 error.
The 'list' action is accessible and works from a link on the site and from some other redirects inside the bpscoupons extension.
Why won't that redirect work? Is there something about internal or external redirects I have to configure somewhere?
I am using typo3 4.5.32 .
Thanks
PS, btw in my query string I see I get these params:
tx_bpscoupons_bpsmessagecentre[message]:Hi ma... etc
tx_bpscoupons_bpsmessagecentre[action]:list
tx_bpscoupons_bpsmessagecentre[controller]:Coupon
Looks to me like it is looking for a bpsmessagecentre object a bpscoupons object but I don't know.
Are those 2 Plugins on the same page? If this is not the case, you have to pass the PageId of the target page as well, as $this->redirect in an extbase conroller takes the following arguments:
/**
* Redirects the request to another action and / or controller.
*
* #param string $actionName Name of the action to forward to
* #param string $controllerName Unqualified object name of the controller to forward to. If not specified, the current controller is used.
* #param string $extensionName Name of the extension containing the controller to forward to. If not specified, the current extension is assumed.
* #param array $arguments Arguments to pass to the target action
* #param integer $pageUid Target page uid. If NULL, the current page uid is used
* #param integer $delay (optional) The delay in seconds. Default is no delay.
* #param integer $statusCode (optional) The HTTP status code for the redirect. Default is "303 See Other"
*/
protected function redirect(
$actionName,
$controllerName = NULL,
$extensionName = NULL,
array $arguments = NULL,
$pageUid = NULL,
$delay = 0,
$statusCode = 303
)
And I think the extension name should start with a lower case letter.

zend framework this->url get different url on the same page but different url

in application\modules\admin\layouts\scripts\layout.phtml
<?php echo $this->url(array('action'=>'logout','controller'=>'user','module'=>'admin'),null,true);?>
when I visited zfmul/public/admin-cate/ , It return
/zfmul/public/admin-cate/logout
but when I visited zfmul/public/admin/categories, It return
/zfmul/public/admin/user/logout
and the two url is render to the same module, same controller, same action, I wonder why it retrun different result?
I didi some configs in application.ini,
resources.router.routes.admincategories.route = "admin-cate/:action/:id"
resources.router.routes.admincategories.defaults.module = "admin"
resources.router.routes.admincategories.defaults.controller = "categories"
resources.router.routes.admincategories.defaults.action = "index"
resources.router.routes.admincategories.reqs.action = "save|edit|index|new"
resources.router.routes.admincategories.defaults.id = "1"
resources.router.routes.admincategories.reqs.id = "\d+"
When you use $this->url, you're in fact using function url from library/Zend/View/Helper/Url.php, whose 1st line is:
$router = Zend_Controller_Front::getInstance()->getRouter();
Since you declared a custom admincategories route, you now have 2 to access those particular module/controller/actions:
Default - accessible via zfmul/public/admin/categories;
Custom - accessible via zfmul/public/admin-cate/.
Depending on the URL you use to access, the $router variable value will change accordingly and so will the result of the $this->url call as you're experiencing.
Here are a few references to questions on SO that might help you work around that behaviour:
Zend Framework: How to disable default routing?;
Zend framework: Removing default routes.

What is the best way to get the action name with Zend Framework

I'm pretty new to Zend digging the docs but i can't find a good way to get the parameter i passed to my action...
Here is my uri :
/entreprise/name/foo
I would like to extract the name, foo. I'm actually able to get the action name with
$this->getRequest()->getActionName();
or the URI with
$this->getRequest()->getRequestUri();
With these two data i can parse the second string and get the name of my entreprise, but i quite surprised there is no best way to do this...
Is there a best to way to it ?
Get the action name with:
$this->getRequest()->getActionName();
And additional data passed to this action by GET with:
$name = $this->_getParam('name', NULL);
This will get the GET-Value name if passed by URI, if not $name is set to null.
Edit:
As mentioned in the comments the way to pass vars is a bit different. Change it to
/entreprise/name/var/foo
And you'll be able to access foo this way:
$var = $this->_getParam('var', NULL);
In the following URI : /entreprise/name/foo if entreprise is your controller and name is your action, then the additional parameter will be used to call your action so you can get it by doing the following:
public function nameAction( $name = null )
{
// $name == "foo"
}

Zend_Controller_Router_Route_Regex allow '?' in pattern

Imagine situation, when the url should looks like
/catalog/sectionIdent?page=1
where page param is optional.
Of course, custom route should be defined. Consider the following code:
$route = new Zend_Controller_Router_Route_Regex(
'catalog/([-a-z]+)(?:\?page=([0-9]*))?',
array('controller'=>'catalog','action'=>'list','page'=>''),
array(1=>'section',2=>'page'),
'catalog/%s?page=%d'
);
$router->addRoute('catalog-section-page',$route);
But this route won't be triggered with '?' symbol in url.
Without '?' (for example, by adding escaped '!' symbol to pattern) everything works as it should.
Is there any way to achieve '?' presence in custom defined regex route? Maybe I'm doing something wrong in pattern?
P.S.: Don't offer to use '/' instead of '?', question is exactly about pattern restrictions in Zend_Controller_Router_Route_Regex implementation.
The ZF routing classes operate on the REQUEST_URI with the query string stripped off, so you may have a hard time get this working in the way you are expecting. However, I believe GET parameters are put into the request object by default, so you shouldn't need to cater for them in your routes. I'd suggest changing your route to remove the query string parts:
$route = new Zend_Controller_Router_Route_Regex(
'catalog/([-a-z]+)',
array('controller'=>'catalog','action'=>'list'),
array(1=>'section'),
'catalog/%s'
);
$router->addRoute('catalog-section-page',$route);
You should still be able to access the params in your controller as if they had been populated by the routes:
public function listAction()
{
echo $this->_getParam('page');
}
and you can use the same method to set a default:
public function listAction()
{
$page = $this->_getParam('page', 1); // defaults to 1 if no page in URL
}
You just may need to sanitise them there (make sure they are numeric).
Edit:
Example of URL helper with this route:
echo $this->url(array('section' => 'foo', 'page' => 2), 'catalog-section-page')

How to change the separation character of Zend Url?

I use Zend URL view helper for building my urls. Everythings works exactly as I'd like to, except one thing: The character used for replacing spaces in the url is a plus (+). I'd like it to be a 'min' (-). How can I change this?
Example:
Now: /nl/nieuws/bericht/3/title/nieuwe**+affiches
Wish: /nl/nieuws/bericht/3/title/nieuwe-**affiches
Thanks in advcance!
This isn't in the documentation anywhere, but it appears that the Zend URL view helper can take a parameter in it's $urlOptions array called chainNameSeparator. No guarantee that's what you're looking for, but trying playing with that and see if it changes anything.
This is likely happening because, by default, Zend_View_Helper_Url will urlencode() what you send it, which would translate spaces into +. My suggestion to you would be to create a new view helper for the type of URL in your code that needs the special inflection.
Something like:
class Default_View_Helper_SpecialUrl extends Zend_View_Helper_Abstract
{
public function specialUrl(array $opts = array(), $name = null, $reset = false, $encode = true)
{
if (!empty($opts['whatever'])) {
$opts['whatever'] = str_replace(' ', '-', $opts['whatever']);
}
$router = Zend_Controller_Front::getInstance()->getRouter();
return $router->assemble($opts, $name, $reset, $encode);
}
}
This way the spaces are changed for whatever necessary route parameters before URL encoding happens by the router.