Scheme-less URL's in Laravel - laravel

I'm using the following code in a Laravel blade template to output a URL:
<script src="{{ URL::asset('js/jquery.js') }}"></script>
This outputs the URL like this:
<script src="http://example.com/js/jquery.js"></script>
I want it to output like this:
<script src="//example.com/js/jquery.js"></script>
I know that Laravel provides a way to output the protocol as https, but I'd prefer to use scheme-less URL's when linking to assets.
Is this possible?

URL::asset will only give http or https:
https://github.com/laravel/framework/blob/4.2/src/Illuminate/Routing/UrlGenerator.php#L146
https://github.com/laravel/framework/blob/4.2/src/Illuminate/Routing/UrlGenerator.php#L188
There are probably some better asset management solutions out there than Laravel's default solution. Laravel does a lot of things VERY well, but in some cases like this, in order to minimize bloat, it provides only basic functionality and expects the developer to use a better 3rd party Composer package for more advanced features.

Related

Laravel mix - 404 error on routes with two levels

I have a small issue with my current configuration using Laravel for the backend, and Vue in the frontend, while my application is built using Laravel mix (6.0).
I am using the following simple mix configuration:
mix.js("resources/js/app.js", "public/js/app.js")
.vue()
.version()
.extract();
Everything works fine when running npm run watch, and when I launch the production build, I get as expected three files in my public folder (manifest.js, app.js and vendor.js). I included these three files in my app.blade.php file in the following way:
<script src="{{ asset(mix('js/manifest.js')) }}" defer></script>
<script src="{{ asset(mix('js/vendor.js')) }}" defer></script>
<script src="{{ asset(mix('js/app.js')) }}" defer></script>
The mix-manifest.json that comes out looks like this:
{
"/js/app.js": "/js/app.js?id=55e00bb7adfe7cc8d33c",
"/js/vendor.js": "/js/vendor.js?id=3cc2a9d83cabdff07b38",
"/js/manifest.js": "/js/manifest.js?id=7d6950016e73d672d079",
}
Most of the routes are working just fine with such a configuration.
However, the problem I am facing is a generic 404 error that shows up when trying to access particular routes having at least two levels, such as <my_website>/read/<post_id>. In this case, the browser tries to resolve something like <my_website>/read/js/app/1.js which obviously doesn't exist, as it should search for <my_website>/js/app/1.js instead.
Am I missing something obvious here? Is there any way to include a full path in the manifest file to avoid this, and making sure that the browser resolves the correct files? Or any other work around to make this work? Thank you!
You can force a base URL in the browser. This rewrites the URLs for you.
Use the <base> tag
See documentation of the base tag here:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
Example:
<base href="https://www.example.com/">

How i can translate article in codeigniter? the localization work on the app but not on thes artclles

I would like to know how I can translate the articles according to the language of the user ! the language set on the app when the user changes the language, the article must be translated also, localization is already set up in the app but not on the article! i should use an API for that or i can do it without? please tell me!
NB: I got only access via FTP so, I can install packages via composer!
please show me some example or link!
how I can use packages in Codeigniter without use composer, like download it directly from GitHub an put it in my project!
Put the following code in your website structure:
HTML Code:
<div id="google_translate_element"></div>
Javascript Code:
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
This is for your whole website will be translated, I hope it works for you.

What's the difference on using `mix` and `asset` when including js file in routing?

A minor question. I was trying to learn routing using Vue.JS in laravel, so I went to this website and I see :
<script src="{{ mix('js/app.js') }}"></script>
And this differs from what I know on including app.js into my website like this:
<script src="{{ asset('js/app.js') }}"></script>
My question is:
Is there any difference on using one of them?
When to use mix or asset?
Does using asset affect routing in Vue.JS?
I tried using either of them, but as I said, I'm new to routing using laravel & Vue.JS so I don't know where I did wrong. I can't get it to work.
I've also tried googling, but what they show isn't related to my question,
.
.
PS: Additional Notes.
In my "website" I already implemented Authentication. Could be a factor or not that cause my routing to fail. If so, how to handle this?
The mix() function will bring you the versioned file of that asset(with a unique id) While the asset function will not take into affect changes made to that asset while running npm run dev or npm run watch The mix function is for cache busting.

Laravel MIX not working, using CDN does work

I'm a bit lost here. I'm NOT new to Laravel Mix so this is really confusing.
I have a layout blade file and it looks something like this
<html> ....
<script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.9.0/katex.min.js"></script>
<script src="{{ mix('/js/myjs.js") }}></script>
</body>
</html>
The myjs.js is compiled using Laravel Mix and contains dozen of libraries and all of them compile and work properly.
However if I include this
require('katex'); //so it's above everything else to simulate situation from above, when it's included in the layout file
...
and drop the CDN script from the layout file - the Katex library won't work.
I tried using require('katex/dist/katex.min.js') but still nothing. The katex.min.js file in the library and the one in CDN are the same.
I'm not expert on npm and I only use it to fetch libraries and then merge and minify them in my Laravel app.
Can anyone help me pin point the problem here or point me to what am I doing/getting wrong here?
Shouldn't the
<script src="lib1.js"></script>
<script src="lib2.js"></script>
included in the html, and
require('lib1')
require('lib2')
compiled to
<script src="{{ mix('compiled.js') }}"></script>
produce the same thing?
Thanks!
UPDATE:
I'm using Katex to implement this summernote plugin.
Katex should be installed as:
window.katex = require('katex');

Laravel 3.x Assets: how to include an external url

I'm trying to include a google CDN jquery link into the Laravel Assets container but the link is being concatenated to my site address, like this:
<script src="http://mybeautifulsite.com/<script src=" http:="" ajax.googleapis.com="" ajax="" libs="" jquery="" 1.8.3="" jquery.min.js"=""></script>
Is there any way to include external links into the assets container?
EDIT
Here's the code I'm using to include the asset:
Asset::add('jquery', '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>');
Should just be:
Asset::add('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js');
Whilst the documentation does not explicitly talk about absolute URLs, the code is written in such a way to handle them.

Resources