simple-php-router icon indicating copy to clipboard operation
simple-php-router copied to clipboard

Any Simple Way of Checking if URL exist?

Open devsrealm opened this issue 4 years ago • 5 comments
trafficstars

When using the getUrl method it returns whatever is in the name parameter even if the URL doesn't exist, it would be cool to have a method that can check if the route alias exists.

Note: Nice package you've got, so, good job.

Edit: Thinking about it, if a method is expected to return a value, but there is nothing to return, it shouldn't return something that doesn't exist. Maybe returning an empty string might be better, just giving you something to consider.

I might end up extending the class, but I'll wait for your response before I proceed.

devsrealm avatar Aug 18 '21 00:08 devsrealm

Thanks man, I'm glad you find it useful.

Good point, might change the behavior for getUrl not sure why it always returns a value to be honest.

Take a look at the findRoute method (i think the method called). The Router class does have functions to find a route based on a url that you can use. Can't remember if the method is public or not though, but in that case I'll provide a patch so you don't have to extend the class.

/ skipperbent

skipperbent avatar Aug 18 '21 03:08 skipperbent

Okay, thanks. If you care, this is the culprit:

https://github.com/skipperbent/simple-php-router/blob/749f252ffba98955311235cc2befc8ab742a151c/src/Pecee/SimpleRouter/Router.php#L726-L733

You are assuming at that point, Will the sun rise tomorrow? It probably will, but we are not sure it will. 😃

I pray it does though, lol. Thanks, appreciate you took your time to reply, it's hard for open-source maintainer to reply to issue these days, maintaining open-source project isn't easy, so, good one.

Let me know anytime you've added the patch to extend the class. Take your time.

devsrealm avatar Aug 18 '21 04:08 devsrealm

Haha you're right - i hope so too! Wonder what thoughts went into those lines of code - can't figure out why you would ever use url with a hardcoded url.. maybe as a shortcut to create a Url object? I've see no problem in removing it though.

Seems like a spoke to fast. The findUrl must have been something i dreamt off, because it doesn't seem to exist in the class.

However I'll add it in a future patch. If you need this functionality yesterday, you can extend the class, create a method and loop through $this->routes property checking if $route->getUrl() is equal to the url you are searching for.

Thanks I appreciate when people realize how much work goes into maintaining a project like this. What really gets me is how some expect commercial grade service - for something they got absolutely free without contributing anything. I don't mind helping but when some can't even be bothered with reading the documentation you can sometimes get the feeling of "what's the point" lol. So thanks again for your nice words.

I'll have a patch ready shortly...

/ skipperbent

skipperbent avatar Aug 18 '21 12:08 skipperbent

Okay I think I've uncovered the madness behind the code.

The reason it always returns a url object is so that you avoid having to constantly check if url() is null.

However maybe a nicer solution would be to instead return some kind of UrlEmpty object that extends the Url object so you have a better way of identifying if the url exists or not.

skipperbent avatar Aug 18 '21 13:08 skipperbent

Yh, something like that would work too. Thinking about it, I think your last proposal is the right way to go, it would make for more granular checking.

devsrealm avatar Aug 18 '21 13:08 devsrealm