EllisLab text mark
Advanced Search
     
url helper: anchor() with array param losing first assoc key?
Posted: 12 October 2010 09:34 AM   [ Ignore ]
Joined: 2010-10-12
3 posts

Running PHP 532 and CI 172:

Test code in a controller function:

$this->load->helper('url');
$assoc_uri $this->uri->uri_to_assoc(1); // get full uri
$test_link anchor($assoc_uri'test link'); // bugged? skips the first key?
$test_link2 anchor($this->uri->assoc_to_uri($assoc_uri), 'test link 2'); 

The first link is bugged. It loses the name of the controller in the final URI. Compare that with the second use of anchor (string), using the same data, that works as expected. For example:

test_link = http://localhost/testsite/about (broken)
test_link2 = http://localhost/testsite/welcome/about (works)

Is this a known issue? Am I missing something glaringly obvious here?

cheers,
// SM

 
Posted: 12 October 2010 09:52 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2010-09-17
606 posts

That’s because uri_to_assoc converts your URI to an array looking like this:

$array = (
  
'testsite' => 'welcome',
  
'about' => false
); 

And when you pass that into anchor (which then sends it to site_url as it’s an array) it will only use the keys of the array (‘testsite’, ‘about’) giving you the result:

test_link = http://localhost/testsite/about (broken)


—-
Is there any specific reason as to why you’re using uri_to_assoc?
If you want to split your URI into an array to be able to edit the array/URI then you have to convert it back into an URI from assoc (as you do in the 2nd example).

 Signature 

I love lasagne!

 
Posted: 12 October 2010 09:52 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2010-09-17
606 posts

That’s because uri_to_assoc converts your URI to an array looking like this:

$array = (
  
'testsite' => 'welcome',
  
'about' => false
); 

And when you pass that into anchor (which then sends it to site_url as it’s an array) it will only use the keys of the array (‘testsite’, ‘about’) giving you the result:

test_link = http://localhost/testsite/about (broken)


—-
Is there any specific reason as to why you’re using uri_to_assoc?
If you want to split your URI into an array to be able to edit the array/URI then you have to convert it back into an URI from assoc (as you do in the 2nd example).

 Signature 

I love lasagne!

 
Posted: 12 October 2010 10:36 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Joined: 2010-10-12
3 posts

Ah thanks - it’s expecting a single-dimensional array, I assumed an assoc array. My bad.

The overarching goal here is to grab the current URI, manipulate select querystring params etc, then render an updated URI as a link.

 
Posted: 12 October 2010 11:21 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2010-09-17
606 posts

You’re welcome smile
You can still do what you want to do with an assoc. array, but you have to convert it back into an URI again.

 Signature 

I love lasagne!