Angular UI router href returned absolute URL lacks path - angular-ui-router

I'm using angular-ui-router, when browsing on my app, on URL
http://localhost:8080/referential/browse.action#/referential/collectivity/38470/181/services
I can call
$state.href(COLLECTIVITY_STATE, {collectivityId: collectivityId})
and I get
#/referential/collectivity/38470/181/services
That's fine but I want a full URL (ready to bookmark or send by email), according to the documentation, i add a parameter to href call:
$state.href(COLLECTIVITY_STATE, {collectivityId: collectivityId}, {absolute: true})
but what I get is
http://localhost:8080/#/referential/collectivity/38470/121/services
which is not the expected first URL: the path is lacking. How should I get the full URL?

You can use:
$location.absUrl();
to return the absolute URL (which includes the domain and protocol).
Be sure to include $location in your controller or factory dependency list.
app.factory("test", function($location)
{
console.log($location.absURL());
});

Related

Laravel route is not letting me load my assets properly

In my laravel 9 project, when I have more than 4 '/' in my route, the project doesn't assets properly for that page. Because it includes the first keyword of my defined route.
For example: If I define a get route:
example.com/word1/word2/word3/word4/word5
In this case, all my other related routes such as my image links, where I've used route('/images/..')
The application loads the link: example.com/word1/images/... instead of example.com/images/...
I used '/' to solve this.
When you use a relative URL (e.g. /images/example.jpg), the browser will interpret it as being relative to the current page's URL. So if you're on a page with a URL like example.com/word1/word2/word3/word4/word5, then the relative URL /images/example.jpg will be interpreted as example.com/word1/word2/word3/word4/images/example.jpg.
To avoid this issue, you can use absolute URLs instead of relative URLs. An absolute URL includes the full URL, including the protocol (e.g. https://) and domain name. In your case, you can use the URL helper function to generate absolute URLs for your assets, like this: This will generate an absolute URL that includes only the domain name and the path to the asset, regardless of the current page's URL
<img src="{{ url('/images/example.jpg') }}" alt="Example">

how to get full url with # in laravel

I am using laravel 5 and I am trying to get my full url which is
/faq#manual
However I can only get /faq
when I call the function
Request::url() or Request::fullUrl()
is there any way I can get the full url
with #manual?
It is impossible to get the fragment from a URL with Standard HTTP. So using variables like $_SERVER or $_GET can't help you. Actually, the server never knows about this value because the browser won't even send a request with a fragment part.
If you want to have the fragment value in the server side, you should get the value by javascript first then pass it as a URL parameter and get it by $_GET in your server side.
That's not possible in http or php however you can use javascript to get the full url including the hash.
var url = document.URL;
or
var url = window.location.href;
Hope this helps.

Laravel How to get Url

When trying to get page url with
URL::current();
it's ignore the pagination.
For example:
current url is www.google.com/searc?page=2
dd(URL::current());
gives us www.google.com/search. Using default pagination.
My question is: Any function for get url with paginations?
Simply use :
\URL::full();
It will give the url along with query string.
You will need the full URL, not just the URL. Try this:
\URL::full();
As described in the docs of Laraval. You can use URL::full()
Accessing The Current URL
If no path is provided to the url helper, a
Illuminate\Routing\UrlGenerator instance is returned, allowing you to
access information about the current URL:
// Get the current URL without the query string...
echo url()->current();
// Get the current URL including the query string...
echo url()->full();
// Get the full URL for the previous request...
echo url()->previous();
Each of these methods may also be accessed via the URL facade:
use Illuminate\Support\Facades\URL;
echo URL::current();

External Links in CodeIgniter

I have the following code:
<div><strong>Name: </strong><?php echo anchor('http://'.$link, $row->Name); ?></div>
Which takes a users input for a link ($link) and puts the url into an anchor tag. It, however, is not redirecting to the external link but simply amending the base url for the site with the stored URL. I attempted to add 'http://' to the beginning of the submitted link which works unless the user has already supplied http in the link input. Any advice on how to overcome this would be amazing.
Yes, per the documentation, anchor() creates links based on your site's URL.
If things are working as expected when URL's are prefixed with http://, but you're having trouble with users sometimes adding http:// and sometimes not, you could simply check the link to determine whether it's ok, or if you need to prefix it. Here's a basic example using strpos:
if(strpos($link, 'http') === FALSE){
// link needs a prefix...
$link = 'http://' . link;
} else {
// link is ok!
}
...use CodeIgniter's prep_url() function (thanks to #cchana for reminding me of it!):
This function will add http:// in the event that a scheme is missing from a URL. Pass the URL string to the function like this:
$url = "example.com";
$url = prep_url($url);

CodeIgniter routing problem. (appends ajax route to existing url)

I'm trying to perform an AJAX-request in a view, the user gives some input which is sent to the server with AJAX and the function it's supposed to go to is routed with CodeIgniters routes.
This is the view I'm currently standing in while making the request.
http://localhost:8888/companies/list
In my route config I've set this route below to handle the AJAX-request, which should be able to come from any view and still be able to go to the route I've specified.
$route['test_ajax'] = "ajax/test_ajax";
So the request should go to the "ajax"-controller and use the function "test_ajax", which should make the POST-url look like this.
POST http://localhost:8888/test_ajax
But instead what I get is the current URL I'm standing at, and the route I've specified appended to the URL crashing my response from the AJAX-request completely since it didn't even go close to the function it's supposed to. The POST-url I get looks like this.
POST http://localhost:8888/companies/test_ajax
Notice how the parameter of /companies was removed. The argument /list was lost somewhere, al though if I add a trailing slash after the list I get the list argument in the URL as well.
So what just happened is that the POST tries to go to the companies-controller and look for the function test_ajax which is defined in the ajax-controller and not in the companies-controller. This error keeps occuring no matter what URL I'm at, and it always follows the same pattern. It keeps appending my route-URL to the existing URL instead of routing correctly.
So what could be causing the routing to behave this way, is there any setting that's accidently enabled or anything? Because I know I've got this to work hundreds of times in previous projects.
Thanks in advance.
It is because your Javascript is using the current directory as the base, and appending the AJAX URL to it. Because you are (to the client-side at least) in the companies directory, it appends your URL onto this.
The solution, if your Javascript is inline, is to just use the base_url() PHP function wihtin the code ...
var url = '<?= base_url(); ?>test_ajax/'
If your Javascript is not inline, you can declare a global variable at the top of your HTML document using the PHP function...
var BASE_URL = '<?= base_url(); ?>'
And use it everywhere else in your Javascript ...
var url = BASE_URL + 'test_ajax/'
Alternatively, you could just hardcode your base URL, but that could get real messy real quick.
Turns out, CodeIgniter interpreted this as a relative link due to the fact that there was no heading slash. CodeIgniter User-Guide states that no heading or trailing slashes should be written in the routes config.
What solved this though was adding a heading slash in the java-URL.
$.ajax({
url: "/test_ajax",
type: "POST",
data: data,
success: function(data){
console.log(data);
}
});
This forces CI to interpret this as a absolute URL and gives me the URL I was looking for.
POST http://localhost:8888/test_ajax

Resources