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

[eluser]xwero[/eluser]
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
Code:
function current_url($uri = '')
{
    if($uri != ''){ $uri = '/'.$uri; }
    $CI =& get_instance();
    return $CI->config->site_url($CI->uri->uri_string().$uri);
}
#2

[eluser]sikkle[/eluser]
Hi xwero,

small is beautiful !

see ya around
#3

[eluser]sophistry[/eluser]
@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??

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

thanks!

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.
#4

[eluser]xwero[/eluser]
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
Code:
<form action="<?php echo site_url('segments/to/form') ?>">
With current_url you can do
Code:
<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
Code:
<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
Code:
<a href="&lt;?php echo current_url('login') ?&gt;">Login</a>
#5

[eluser]xwero[/eluser]
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
Code:
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);
}
#6

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

[eluser]xwero[/eluser]
Colin check this out.
#8

[eluser]Unknown[/eluser]
i don't understand please explain to me
#9

[eluser]xwero[/eluser]
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.
#10

[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