URL Query Parameters and HTML Entities

hi people,

this is a very simple question (so I’ll get plenty of replies ;) ) regarding a very common problem that you may have already solved :

in order for a xhtml page (doctype : XHTML 1.0 Transitional) to be valid, there should not be url written like that :


http://host/path?param1=12&param2=15

…but like that :


http://host/path?param1=12&param2=15

…where the ‘&’ character is replaced by XML entity ‘&’.

Ok, very well then, but how can I achieve this in Yii ? Should I always create my own CUrlManager so createUrl() would always use ‘&’ as parameter separator (instead of default ‘&’) ? If yes, wouldn’t it be nice to set the separator has a configuration param ?

And what about CHtml::link() ? … there is no way to define the parameter separator when it is invoked … is there ?

Thanks for your help

ciao

8)

Of course you know about the third parameter in CController::createUrl() right? ;)

:) …yes Mike I do, so you confirm that this is what I have to do : always set the third parameter to ‘&’ instead of using default value ‘&’ ? … ok, I’ll do that, but again it would have been nice to be able to set this value in the config file once for all, instead of setting it in each call to createUrl(). I mean, this is a requirement if you want to generate valid xhtml pages, which I guess is a quite comon task (or is it that everyone uses nice URL rewritting and gave up url parameters ?).

Thanks for your reply

B)

I see your point. Actually i wasn’t even aware of this issue in XHTML, so thanks for pointing it out. If you check CHtml, all URLs are generated in CHtml::normalizeUrl(). There’s no third parameter used in the call to createUrl() there. So how about a change like this:


/**

 * @var string the token separating name-value pairs in the URL.

 */

public static $ampersand='&';


// and in normalizeUrl():

if(($c=Yii::app()->getController())!==null)

    $url=$c->createUrl($url[0],array_splice($url,1),self::$ampersand);

else

    $url=Yii::app()->createUrl($url[0],array_splice($url,1),self::$ampersand);



Now you could set CHtml::$ampersand somewhere at the beginning of the request. If you think that’s reasonable, we could create a ticket.

Hi Mike,

before creating a ticket for this I wanted to be sure that I was not missing something ;) … you’re right, it would be nice to be able to globally define (in config file) a default value for ampersand.

Your change is ok, however what about direct calls to the CController::createUrl() method you mentioned ? Let’s be crazy !! let’s imagine that it would be possible to set the separator to use, when initializing the CUrlManager component in the config file !! … in the end, each URL creation is done by CUrlManager->createUrl() right ? So why not write something like :




array(

    ......

    'components'=>array(

        ......

        'urlManager'=>array(

            'ampersand'=>'&',

        ),

    ),

);



If not set, default value would be ‘&’. Then, I assume that all method with this $ampersand as third parameter should be modified from …


public function createUrl($route,$params=array(),$ampersand='&')

… to …


public function createUrl($route,$params=array(),$ampersand=null)

In CUrlManager->createUrl($route,$params=array(),$ampersand=null) if $ampersand is NULL set it to $this->ampersand.

Ok, it may be not so easy and I’m not a Yii core developper, but that’s just an idea. If it is not (for any reason) feasible, the solution you propose will be fine too… I just don’t want to go through all my code and modify all URL creation call ! ;)

ciao

8)

Your proposal is even better :). It only takes 2 more lines of code and should not be a problem with backwards compatibility. Let’s sum it up again:


public $ampersand='&';


public function createUrl($route,$params=array(),$ampersand=null)

{

    if ($ampersand===null)

        $ampersand=$this->ampersand;

...



A ticket should never hurt ;)

Yes, that it !

ok then, what we could do is wait some time because maybe someone will post some reply saying “hey guys you can do this very easely …” (and then provide the solution we didn’t see before). If this doesn’t happen, I will add a ticket, and mention your help, and then the feature will be added to Yii with our names, and then we will be famous for ever in the Yii comunauty first, then worldwide, and we will earn a lot of money and … :lol: :lol:

Ok first : the ticket ;)

ciao

8)

Yipieh, finally famous! ;D

I think, you can create a ticket right away and add a link to this discussion. Devs react faster on tickets … ;)

;D

… I’ve post in the ‘feature request’ and depending on replies, I will enter a new ticket later today (or tomorrow)… so wait before ordering a Ferrari ;)

8)