HTMLHelper link() added unintended path - cakephp

I believe this is very simple question. Maybe that's why I can't find it on Google.
When I do this inside View/Product/view.ctp
echo $this->Html->link('Download PDF', 'app/files/product/1/manual.pdf');
The resulting URL is like this:
app/products/app/files/product/1/manual.pdf
It automatically added app/products since this is inside Product's view.
How to nullify that automatic addition?
Thanks

You're specifying a relative url, causing your browser to append the url to the current url.
echo $this->Html->link('Download PDF', '/app/files/product/1/manual.pdf');
(note the leading slash /)
Should result in a link to http://example.com/app/files/product/1/manual.pdf

Related

Href opening link in http://localhost:3000/LINK rather than opening the link seprately [duplicate]

I just have created primitive html page. Here it is: example
And here is its markup:
www.google.com
<br/>
http://www.google.com
As you can see it contains two links. The first one's href doesn't have 'http'-prefix and when I click this link browser redirects me to non-existing page https://fiddle.jshell.net/_display/www.google.com. The second one's href has this prefix and browser produces correct url http://www.google.com/. Is it possible to use hrefs such as www.something.com, without http(s) prefixes?
It's possible, and indeed you're doing it right now. It just doesn't do what you think it does.
Consider what the browser does when you link to this:
href="index.html"
What then would it do when you link to this?:
href="index.com"
Or this?:
href="www.html"
Or?:
href="www.index.com.html"
The browser doesn't know what you meant, it only knows what you told it. Without the prefix, it's going to follow the standard for the current HTTP address. The prefix is what tells it that it needs to start at a new root address entirely.
Note that you don't need the http: part, you can do this:
href="//www.google.com"
The browser will use whatever the current protocol is (http, https, etc.) but the // tells it that this is a new root address.
You can omit the protocol by using // in front of the path. Here is an example:
Google
By using //, you can tell the browser that this is actually a new (full) link, and not a relative one (relative to your current link).
I've created a little function in React project that could help you:
const getClickableLink = link => {
return link.startsWith("http://") || link.startsWith("https://") ?
link
: `http://${link}`;
};
And you can implement it like this:
const link = "google.com";
<a href={getClickableLink(link)}>{link}</a>
Omitting the the protocol by just using // in front of the path is a very bad idea in term of SEO.
Ok, most of the modern browsers will work fine. On the other hand, most of the robots will get in trouble scanning your site. Masjestic will not count the flow from those links. Audit tools, like SEMrush, will not be able to perform their jobs

AngularJS $location get path before hash

I need to extract the path before the # in my AngularJS app, so for example if my app url is:
http://www.domain.com/folder/app/#/home
I need to get only this portion:
http://www.domain.com/folder/app/
I've tried using $location.path, $location.abspath but couldn't find any function that can get me the first portion before the #. Can someone please help me by telling me what I am missing here? and if there is a way to get the first portion of the url (before the #)? Thanks
You could split the path into the pathname and hash parts, and then use the pathname only:
$location.absUrl().split('#')[0]
// "http://www.example.com/folder/app/"
split is just a Javascript string operation returning an array:
'http://www.example.com/folder/app/#/home'.split('#')
["http://www.example.com/folder/app/", "/home"]

Drupal absolute path for everything for one page

There is one page (content type => basic page) where all links must be absolute, not relative.
It is a template used by a 3rd party.
If relative, for ex. the css is not loaded.
Howto?
You can use
global $base_url
drupal_get_path('theme', 'THEME_NAME');
It returns the path without slashes in front or after.
That's the type first, then the project name
drupal_get_path('module', 'name_of_module');
I reverse that enough (and then get confused as to why it isn't working) that i felt it worth documenting here, so the 'doh!' moment of realization comes sooner...
The valid types are as listed:
module
theme
profile
theme_engine
I can't think of any others and i can't even think of a reason to use the last one.

angular.js get slash-separated path from url

I need to take as a parameter in my site's urls a file path (that will contain some number of '/' characters). How can I parse such a parameter from a url? Something like http://localhost/path/to/file would be preferable, but if that doesn't work, http://localhost/?path=/path/to/file or something could work as well.
to use location in your controller. first turn html5mode to true like this
Then you can use
location.search and it will return string without any error.
for more use of location go here https://docs.angularjs.org/api/ng/service/$location
Take a look at URI.js, it looks like what you need.

Encoding url's the Play! Framework

Is there a way to make the Play! Framework ignore slashes and ? in parts of the URL?
Typically, if I have the following:
www.123.com/api/link/http:www.bla.com/?contenId=123&User=test
It won't work. In that case, what I would want to do is simply have the link in the last part of the URL in a String variable to save it. I suppose I can force the client to replace the / and ? by something else, but I would rather keep it simple.
My first thought was that maybe there is a way to configure the routing such that we have something like:
/api/link/{data}
where data would hold whatever remains of the URL. Can't find out how to do that though.
You can't have : / ? except your main URL. You should encode your parameter to append it to main URL. See URLEncoder for Java.
This is not a valid URL:
http://www.123.com/api/link/http://www.bla.com/?contenId=123&User=test
It must be:
http://www.123.com/api/link/http%3a%2f%2fwww.bla.com%2f%3fcontenId%3d123%26User%3dtest
Then you can pass it to {data} parameter and decode it in your handler method.

Resources