Laravel asset vs relative paths - laravel

In laravel you can do one of these:
<link rel="stylesheet" href="/css/app.css">
<img src="/storage/img/logo.svg">
<script src="/js/app.js"></script>
<!-- same as the following -->
<link rel="stylesheet" href="{{asset('css/app.css')}}">
<img src="{{asset('storage/img/logo.svg')}}">
<script src="{{asset('js/app.js')}}"></script>
The first is a relative path (relative to the public dir) and the second generates an absolute path.
Besides that, is there any difference in the results? At least in Chrome, Opera, and Firefox I could not perceive any difference.
Is there any advantage of using one over another? Maybe in performance or compability? Does one loads faster than the other?

There are potentially major differences.
The asset helper is CDN-aware. Setting the app.asset_url config value causes asset() to append that URL to every link it generates, which is very useful if you're using a CDN.
In addition, it'll save you a lot of work if your app winds up hosted in a subdirectory - all you have to do is set app.url, and asset will spit out the right URLs to js/app.js (i.e. /a/sub/folder/js/app.js).

Related

Is there any advantage of using asset helper function to instead of absolute URL

Is understand there is an advantage of using asset helper instead of relative URL like when visiting /blog/posts relative won't fetch the styles but using asset helper will. But I currently see no difference of using absolute and asset helper. Do we have situations where asset will work and absolute won't?
<link rel="stylesheet" href="/style.css" />
<link rel="stylesheet" href="{{ asset('style.css') }}" />

why is blade making a head tag?

I'm using a Laravel app with an external public directory, e.g. root/Laravel, and root/html/public.
I need this app to load from a require on an php file that already has another framework(root/html/this-section.php), hence that other fw has its own head, and body tag. This app will load between the header and footer of that index.
In my blade layout.app file, i have
#section('stylesheets')
<link rel="stylesheet" href="/this-section/css/vendors.css">
<link rel="stylesheet" href="/this-section/css/app.css">
#show
<div id="main">
#include('layouts.sidebar')
#include('layouts.header')
<section>
#yield('content')
</section>
</div>
The issue I'm having is if no my app layout, when I delete the head and body tags during testing, which is what i need, the blade system, or what i dont know, is still creating an empty head tag set, <head></head>, then when i enable the stylesheets section, it ends up in that <head> tag.
Expected: The head tag should not be there. I don't want a head tag. What in laravel can i adjust to remove this auto creation of head (and body)?
It sounds like your using tags that belong in the <head> section is causing this. While your source may be pristine:
browsers will add in the missing-but-required tags as appropriate, resulting in you seeing them in the browser's web inspector:

Google code prettify thinks css #id's are line comment. What am I doing wrong?

Example CSS
#wrap{margin:20px}
Code prettify wraps the whole line in .com
<span class="com">#wrap{margin:20px}</span>
Somebody has a similar issue here.
Where someone answers "Are you loading lang-css.js?".
Here's what I'm loading in the footer.
<script src="/js/google-code-prettify/lang-css.js"></script>
<script src="/js/google-code-prettify/prettify.js"></script>
I can see both of them with web inspector. I tried changing the order and loading them from the header. I'm using the latest version.
All help is greatly appreciated :)
Thanks!
The order you link to the javascript files matters. You need to call the base code (prettify.js) first followed by the css specific code (lang-css.js). You can place the script tags either in the head section or at the end of the document... both work but placing at the end of the document will speed up the page load.
<script src="/js/google-code-prettify/prettify.js"></script>
<script src="/js/google-code-prettify/lang-css.js"></script>
You will also need to ensure that you are linking the stylesheet in the head of your document.
<link rel="stylesheet" type="text/css" href="/css/prettify.css">
You also need to add the correct classes your pre tag(s). The syntax-highlighting functions contained in lang-css.js will not be called without adding the class "lang-css" to the <pre> tag.
<pre class="prettyprint lang-css linenums">
Finally, make sure you call the "prettyPrint()" function on page load.
<body onload="prettyPrint()">

Image Pathing and Absolute Paths?

I'm developing a website in ASP .NET MVC 2 using C#.
I have a partial view, Header.ascx. In there I have an image for my website, MainImage.png.
When I use one of the primary Views I've created, the image shows up fine. For instance, I hit the Index ActionResult of my News Controller. (site.com/News)
However, when I dig deeper, I seem to lose my image, even though the Partial view is being displayed from the Master page. i.e., if I try going to site.com/News/Article/1
Are there any suggestions for keeping my image fully intact, such as a way to do absolute pathing?
The code I currently have in my partial view is here:
<div align="center"><img src="../Content/images/MainImage.png" style="border:none" /></div>
I've tried changing the src to ~/Content/images/MainImage.png but that breaks it all over the site.
Use the Url.Content helper method.
<img src="<%: Url.Content("~/content/images/imagename.png") %>" />
Same applies for when you want to include javascript files or css
<link rel="stylesheet" href="<%= Url.Content("~/content/site.css") %>" type="text/css" />
<script type="text/javascript" src="<%= Url.Content("~/content/scripts.js") %>"></script>
Whenever possible, make the path (href or src) to resource files, like images, CSS and JS relative to the web server root. That is, your URLs should begin with a slash:
<img src="/images/imagename.png" />
That format retains the current server address (which may be an IP address, an internal network address or any of a number of public web addresses), protocol and port, and doesn't depend on the apparent path of the page the user is looking at (which can change, depending on whether the user is accessing the page by its canonical location or by a URL rewrite).

Base Href vs. .htaccess RewriteBase

Normally I use
<base href="http://domain.com/" /><!--[if ie]></base><![endif]-->
I haven't tried much with RewriteBase, I normally get confused and keep changing it till it works. Which method would be best, I obviously find the best solution because the links stay the same so that no links are broken most of the time when attaching a css file, e.g.
http://domain.com/css/main.css
It just always stay the same when accessing to sub-directories. Although, when I don't use the tag, and I access to a sub directory, it breaks the css links when I use
<link href="css/main.css" rel="stylesheet" type="text/css" />
As my PHP documents would include the header,
<?php include("include/global_header.php"); ?>
If I do that without the I would have to use:
<link href="../css/main.css" rel="stylesheet" type="text/css" />
Which can break when accessing to a sub-directory.
So... does the RewriteBase work the same as the ?
Your thoughts.
Basehref works at the HTML level on rendering of the webpage
RewriteBase is in the .htaccess file which is processed by Apache before the HTML is rendered.
As a result they perform different functions and cannot be comapared
Actually, you just compared them. I still think it's an open question as to whether they perform the same function, despite the fact that they do it at different times in the page lifecycle. Voted up though, for pointing out the fundamental difference.

Resources