Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup fURL method functionality/naming #3

Open
wbond opened this issue Apr 27, 2012 · 2 comments
Open

Cleanup fURL method functionality/naming #3

wbond opened this issue Apr 27, 2012 · 2 comments

Comments

@wbond
Copy link
Member

wbond commented Apr 27, 2012

Ticket #369 by vxnick:

Consider making fURL::get() return the entire URL - including the domain and query string (if set). Alternatively, add a new method to return the entire URL, perhaps fURL::getURL().

Comment by vxnick:

Perhaps consider adding fURL::getProtocol() to return http://, https://, ftp://, etc, and to save on manually checking whether SSL is set in $_SERVER or the port is 443, etc.

Comment by vxnick:

fURL::getAnchor() might be another useful method - return an anchor if it exists (some/url#anchor).

Comment by @wbond:

Yeah, it seems like there should be:

fURL::getProtocol() - returns http://, etc
fURL::getHost() - returns example.com, with any non-standard port
fURL::getPath() - the current fURL::get(), /path/to/script
fURL::getQuery() - return ?key=value

Unfortunately the anchor is never sent to the server, so we can't return that.

The other issue I see is methods that return a combination of the above parts. For instance the protocol and host are often used together (currently fURL::getDomain()). Perhaps fURL::get() should return the whole URL by default, but optionally allow the programmer to pass in the parts they want?

fURL::get() - the whole URL http://example.com:81/path/to/script?key=value
fURL::get('protocol', 'host') - http://example.com:81
fURL::get('path', 'query') - /path/to/script?key=value

Or perhaps all of the different methods should be discarded for a single fURL::get() which you can pass elements into? This would also make it so it could allow schema (http) and port (80) without extra methods.

Comment by vxnick:

I like the idea of passing elements into fURL::get() - that seems like a really nice solution.

Comment by mantaworks:

The problem with passing elements in is that they might not go together. What's the expected output of fURLget('protocol', 'path') or fURLget('query', 'host')? There's ambiguity.

I'd say a method like fURL::getAllParts() or fURL::split() that returns an associative array with all the parts named would be the way to go. You'd still have the parts you're interested in displayed clearly, without being extremely verbose.

Comment by alexleeds:

According to the php parse_url function (http://php.net/manual/en/function.parse-url.php) it is possible to get the "fragment" (aka the part after the #)

parse_url('http://username:password@hostname/path?arg=value#anchor') returns:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

Comment by @wbond:

@alexleeds

parse_url() will return the fragment if you pass in a URL that contains one. The issue is that browsers don't send the fragment to the web server, so we can't get it.

Comment by @wbond:

@mantaworks

How about allowing only a single element to be specified, but with a < before or > after to indicate the element requested plus everything before it or everything after it?

// Would return http://example.com
fURL::get('<hostname');

// Would return /path/to/script?key=val
fURL::get('path>');

When working with URLs we are all looking for a way to get a single result usually containing the most specific, or least specific pieces.

@mattsah
Copy link
Contributor

mattsah commented May 15, 2012

If you're looking towards method cleanup in general, can I also add that I would like to see fURL as instantiable and able to modify (as it can with the current URL) a particular URL...

I'm not sure how you would go about cleanly maintaining backwards compatibility and not have more confusing methods, but it would be nice if I could create a URL object with any URL I want and use replaceInQueryString() and removeFromQueryString() or equivalents.

@mattsah
Copy link
Contributor

mattsah commented May 15, 2012

Also, if the plan would be to have parts, might I suggest piggybacking the PHP parse_url constants?

fURL::get('http://www.example.com:8080/whatever/test?q=answer')->parse(PHP_URL_PORT);
fURL::get('http://www.example.com:8080/whatever/test?q=answer')->replaceInQuery('q', 'new_answer');

Similar to fDate() these objects could be immutable, so the methods always return new instances with differing internal states... the __toString() could always return the full URL, calling fURL::get() without a parameter would create an fURL object of the current URL. Idea being, if I pass fURL::get() to a method that takes a string, the __toString() method should be invoked, maintaining some if not all backwards compatibility.

Oh, and lastly, any ORM registered URL columns for validation, etc, could return fURL objects. Could even keep things consistent with a prepare() method, perhaps that could replaced %tokens with values. $url->prepare('%s%d%p/%r?%q') [scheme, domain, port, request, query]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants