Laravel return true validation for incorrect urls - laravel

in laravel i want to check simple url such as :
http://www.google.com
http://google.com
www.google.com
http://www.google.com
http://www.google
unfortunately laravel validation code as
['redirect_url' => 'required|url']
return false for this urls:
www.google.com
and return true for http://www.google address, how to check correctly urls in laravel, this validation is not best validation for urls

You can use regex validation rule for URL validation
['redirect_url' => ['required','regex:/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i']]

Before validation check if string contains http:// or https:// with this code:
if(!(starts_with($url, "http://") || starts_with($url, "https://")) {
$url = "http://".$url;
}
Function starts_with() is helper function from Laravel. docs

The regex solution that Jay Dhameliya provided is probably the best one for your scenario but I just thought I would explain why the url validator doesn't work as you expected.
If you look at the illuminate/validation/Validator validateURL function it is expecting a the following to be specified as part of the URL.
protocol ( http/https/etc )
optional basic auth
a domain or ip address
optional port
a trailing / or nothing or / followed by something else
There doesn't seem to be any alternatives provided for this validator method or ways to say don't require the protocol. So once again a custom regex seems the best option. If you are planning on using it in more than one place you might want to consider creating your own Custom Validation Rule see the docs for details.

Related

Codeigniter router get parameter

I use $.getJSON() to retrieve some data for a couple of cascading dropdowns in my form. $.getJSON() automatically appends the parameter at the end of the URL like domain.com/controller/method/?parent=5
So, I've declared my method like public function method($parent) which works file, but the same method will be used from other parts of the website that will call it like domain.com/controller/method/5
I tried to create a route in routes.php like the one below:
$route['business/regions/?parent=(:num)'] = 'business/regions/$1';
but it doesn't seem to work. Am I doing something wrong? Maybe ? is confusing the regex parser of the router? Do I have to escape it somehow to make it a 'literal' ? ?
Or is it that router is not used to 'rewrite' get parameters at all? I'm very confused, as it should work but it doesn't and I'm wondering what's wrong with it...
Codeigniter route parameters are for url parameters. It is particularly useful when trying to create a REST styled url pattern.
What you're trying to do is get url query string from the url which is not supported via the Codeigniter router. For you to get what you want you can do the following:
In your routes.php:
$route['business/regions'] = 'business/regions';
and in your controller Business.php:
public function regions() {
//the numeric id you're looking for
$parent = $this->input->get('parent');
}

CodeIgniter - How to route a controller with other name?

I have installed an authentication module named bit_auth in my codeIgniter.
I have a controller for that module named "bit_auth" so when calling functions on my controller my urls will be like this:
http://(mydomain.com)/bit_auth/
http://(mydomain.com)/bit_auth/edit_user/1
http://(mydomain.com)/bit_auth/activate/7b60a33408a9611d78ade9b3fba6efd4fa9eb0a9
Now I want to route my bit_auth controller to be called from http://(mydomain.com)/auth/....
I have defined these routes in my "config/routes.php":
$route['auth/(:any)'] = "bit_auth/$1";
$route['auth'] = "bit_auth";
It's working fine when I use: http://(mydomain.com)/auth/
but it shows me 404 page not found error when opening links such as:
http://(mydomain.com)/auth/edit_user/1
http://(mydomain.com)/auth/activate/7b60a33408a9611d78ade9b3fba6efd4fa9eb0a9
What am I doing wrong?
Because you are using more params than are in the route you will have to do this:
$route['auth/(:any)/(:num)'] = "bit_auth/$1/$2";
Hope this helps!
After googling and investigating I saw somewhere they where using another syntax for routing in CodeIgniter which was using regular expression directly rather than using codeigniter described patterns like (:any) or (:num) in its help. I just replaced (:any) with (.+) in my config/routes.php and now it is working perfect.
$route['auth/(.+)'] = "bit_auth/$1";
$route['auth'] = "bit_auth";
Because in CodeIgniter (:any) and (:num) are not including / and they are aliases for regular expression patterns of ([^\/]+) and (\d+) so if you want to match rest of the link including any number of / you can use manual regular expression pattern (.+) which includes / in its pattern and will trigger for the all rest of URL.

Shorten URLs within CodeIgniter

This question has been asked a few times but I can't seem to find a solution that helps me which is why I am trying here.
I have my site setup with the following for URLs I am using CodeIgniter I have a controller called user which loads a user view.
So my URLs are structured as follows:
http://example.com/user/#/username
I want to try and strip out the user controller from the URL to tidy up my URL so they would just read:
http://example.com/#/username
Is this possible I have been looking at route and have tried lots of different options but none have worked?
$route['/'] = "user";
Could anyone offer any solution?
Assuming the '#' in your URLs is a valid function and 'username' is a parameter for that function, then this route should work:
$route['#/(:any)'] = "user/#/$1";
Depending on what usernames are to be routed you may want to change the wildcard. For example, if you only wanted to route numbers as the parameter, you could change (:any) to (:num).
(:num) will match a segment containing only numbers.
(:any) will match a segment containing any character.
You can also use regular expressions to define routing rules, allowing you to further restrict what is routed.

cakephp url validation

One of my models has a 'url' field. I am using the default url validation rule on it. When trying to add a specific url it is not validated.
The url causing validation to fail is http://careers2.hiredesk.net/ViewJobs/JobDetail.asp?Comp=I3&TP_ID=1&PROJ_ID={BDA01FCD-5703-4D40-9197-CCF688633951}
The { character is causing the validation to fail. What workarounds do I have here?
Do you pass your given URL through a URL Encode Function first? See the linked page for an example.

Rewrite url: serve both query string and block urls

my journey of learning MVC continues and as hard as it is, I'm learning a lot of things I could never learn otherwise. Now I have faced the problem in routing. Currently I'm taking the $_SERVER["REQUEST_URI"] and get the controller and method and any args. When url is in format http://mysite.com/forum/thread/12/1123 there is no problem but I need to catch also requests like http://mysite.com/index.php?forum=12&&thread=1123.
I have read links in threads below but cannot get my head on QSA and I though I would better ask.
Thanks
mod_rewrite: Check for Custom query string in URL?
Rewrite url with query string in htaccess
I ended up writing something like before:
I redirect using htaccess
//No Controller specified in url (The current url is base url like http://example.com/hosanna_framework/)
if(!isset($_GET['base_url'])){
$url = $config["router"]["default_controller"];
}
//Controller is specified in url
else{
$url = $_GET['base_url'];
}

Resources