I create a URL like this:
$app->createAbsoluteUrl('/', array(
'param1' => 'val1',
'param2' => 'var2',
);
The generated URL is:
http://mysite.com/param1/var1/param2/var2
But I expect a url like this:
http://mysite.com/?param1=var1¶m2=var2
In function manual it says:
$params array additional GET parameters (name=>value). Both the name and value will be URL-encoded.
But it doesn’t seem to work like that. How I can generate the expected URL? Thanks.
You need to specify that the
urlManagerapplication component should use the “get” format for the URLs it generates; the default is to use the “path” format. The Yii guide explains how to do it inside your application configuration:Update: So your
urlFormatis “path” and that’s by design… what about alternatives?If you don’t mind extending
CWebApplicationand using your own derived class in its place then you have several options such as:Define your own
createUrlExmethod based on the originalcreateUrl. It could look like this:Override
registerCoreComponentsso that you can have a second url manager:You can now call
Yii::app()->specialUrlManager->createUrl(...)anytime.You can also approach the problem in other ways:
Extend
CUrlManagerand expose a method that allows you to select the flavor of url to create on the spot.If you only need “get” urls in one or two places, you can always create a new
CUrlManagerobject, configure it on the spot, callcreateUrland then discard it. You could also hide this ugliness behind a free function. Essentially this (admittedly not recommended) approach is a low-tech version of the first workaround given that has the advantage that you don’t need to extendCWebApplication.