I am mikek's Blog

The personal blog of Mike Karthauser, who you may know from Brightstorm Limited or the internet.

Named arguments vs cakephp sessions

Trying to capture named arguments and store them into a session in CakePHP was returning some unexpected results when I subsequently printed the session data.

My code simplified is as follows:

if( !empty($this->data)){
$params = $this->Session->read('Media.params');
pr($params);
} else {
$named = $this->params['named'];
$this->Session->write('Media.params',$named);
}

If i feed it a url as such http://testApp.com/controller/action/named1:value1/named:value2

I would expect the pr($params) to return an array as such:

array(
[named1]=>[value1]
[named2]=>[value2]
)

Except it was only returning:

array(
[named1]=>[value1]
)

Funny eh?

After a bit of fiddling, I managed to get it to work by adding an additional slash as such:
http://testApp.com/controller/action/named1:value1/named:value2/
which gave me the full named argument array.

For more about Sessions in CakePHP, visit http://book.cakephp.org/view/173/Sessions
and http://justkez.com/understanding-cakephp-sessions.

There are a few issues affecting this still, specifically if i add a namedArg to the end of a
$html->link() .

I have compensated for that as follows:
echo $html->link(__('Upload Media', true), array('controller'=> 'media', 'action'=>'upload','/article_id:'.$article['Article']['id']."/ "));
Note hacky ."/ "

When i establish whats up, I’ll post a reply here. If you know more than me, please leave a comment below.

Follow up:

The correct way to do this would be:

echo $html->link(__('Upload Media', true), array('controller'=> 'media', 'action'=>'upload','article_id:'=>$article['Article']['id']));

(thanks jon)

Advertisements

Filed under: development, , ,

2 Responses

  1. jon says:

    hi m,

    solution to link issue is pass the named params as key=>val pairs, eg:

    echo $html->link(__(‘Upload Media’, true), array(‘controller’=> ‘media’, ‘action’=>’upload’,’article_id’=>$article[‘Article’][‘id’]));

    hth

    j

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

mikek on twitter

%d bloggers like this: