Welcome Guest, Not a member yet? Register   Sign In
SVN url helper : current_url function added

It's a function i added after a few sites with CI Smile But i'm glad it's a default function now.

I mainly use it for the action attribute of the form tag. But also to build deeper urls so i have a $uri parameter like the site_url function

I have this
function current_url($uri = '')
    if($uri != ''){ $uri = '/'.$uri; }
    $CI =& get_instance();
    return $CI->config->site_url($CI->uri->uri_string().$uri);

Hi xwero,

small is beautiful !

see ya around

@xwero... i am having trouble understanding the utility of this function. that's not to say i don't value it... i simply don't understand what it is doing! ;-)

could you explain a little more how this is useful. i see your comment about using it in a form action attr. and i tried to divine why the CI site_url() method accepts a uri parameter but i couldn't see why. i thought it was just a simple function but you've shown an interesting use for it that i don't quite understand.

finally, what the heck is this regex in the site_url() method doing anyway (i'm pretty versed in regex and i am stumped as to what this does or for what purpose): EDIT: i understand the regex capturing expression to mean it is looking for the any number (including zero) of slashes then at least one character (ungreedy), then any number of slashes again (or zero slashes), then the end of the string. but, what does it find in between those slashes? doesn't it only find one segment??

return $this->slash_item('base_url').$this->slash_item('index_page').preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix;


EDIT 2: i see that the function you are talking about is in the CI SVN now... and i see the note
Quote:Returns the full URL (including segments) of the page being currently viewed.
about it in the SVN userguide but i still don't get how it is doing what it is doing.

Sorry for the late reply but here goes Smile

If you have a form that is shown after the action takes place up until now you had to do
<form action="<?php echo site_url('segments/to/form') ?>">
With current_url you can do
<form action="<?php echo current_url() ?>">
If you want to reuse that form in some other place you don't have to do something like
<form action="<?php echo site_url($segments) ?>">
<!-- or -->
<form action="<?php echo site_url($this->uri->uri_string()) ?>">
I'm a big fan of view portability so the last example is a big no-no in my book. The other example only fattens the controller.

A use of the additional segments is when you add parameters to the url to get content but at some point you want to edit that content.
site.com/user becomes site.com/user/1. As a security precaution you have to click to see the login data so you could do site.com/user/login/1 but i like site.com/user/1/login better so my link looks like this
<a href="&lt;?php echo current_url('login') ?&gt;">Login</a>

Lately i'm discovering the current_url function is too inflexible to use, even with my modification, in forms that use segments to specify extra content. So i came up with this modification
function current_url($uri = '',$segments = 0)
    if($uri != ''){ $uri = '/'.$uri; }
    $CI =& get_instance();
    $uri_string = $CI->uri->uri_string();
    if($segments > 0){ $uri_string = implode('/',array_slice($CI->uri->segment_array(),0,$segments)); }
    return $CI->config->site_url($uri_string.$uri);

[eluser]Colin Williams[/eluser]
Wait a sec.. can't you just leave the action attribute blank?

Colin check this out.

i don't understand please explain to me

I use the current_url function to put in the form action attribute and Colin says you can leave it empty if you want to form to have the same url after submitting. Although the browsers support the behaviour Colin suggests i like to play it save and add the current url to the action attribute. I'm not going to go in a discussion which is better, everybody has to decide that for himself.

[eluser]Colin Williams[/eluser]
If you feel spooked by having an empty action attribute, just use $_SERVER['REQUEST_URI']

Quote:Colin check this out.

And he concludes that an empty action attribute in fact DOES follow the HTML spec and works across all modern browsers.

Theme © iAndrew 2016 - Forum software by © MyBB