Laravel Stylesheets and JavaScript Don't Load for Non-Base Routes

Laravel stylesheets and javascript don't load for non-base routes

For Laravel 4 & 5:

<link rel="stylesheet" href="{{ URL::asset('assets/css/bootstrap.min.css') }}">

URL::asset will link to your project/public/ folder, so chuck your scripts in there.


Note: For this, you need to use the "Blade templating engine". Blade files use the .blade.php extension.

Laravel 5 stylesheets and scripts not loading for non-base routes

You are using relative paths and you need absolute paths. See the example below, the only thing I added is the prepended /

{!! HTML::script('https://code.jquery.com/jquery-2.1.4.min.js') !!}
{!! HTML::script('/js/skel.min.js') !!}
{!! HTML::script('/js/skel-layers.min.js') !!}
{!! HTML::script('/js/init.js') !!}
{!! HTML::script('/js/scripts.js') !!}
<noscript>
{!! HTML::style('/css/skel.css') !!}
{!! HTML::style('/css/style.css') !!}
{!! HTML::style('/css/style-desktop.css') !!}
</noscript>

A relative path loads files related to the current path. Eg from the page http://example.com/about/us you can relatively load images/logo.png which results in the actual request to http://example.com/about/images/logo.png.

An absolute path is always from the hostname: same example as above but you'd load /images/logo.png which results in http://example.com/images/logo.png

Laravel CSS stylesheets stop working in certain routes

You should change the path/url of your asset to root folder of your site. Maybe just need / at beginning. change

    <link href="css/normalize.css" rel="stylesheet" type="text/css">
<link href="css/webflow.css" rel="stylesheet" type="text/css">
<link href="css/cascade-ui.webflow.css" rel="stylesheet" type="text/css">

to

    <link href="/css/normalize.css" rel="stylesheet" type="text/css">
<link href="/css/webflow.css" rel="stylesheet" type="text/css">
<link href="/css/cascade-ui.webflow.css" rel="stylesheet" type="text/css">

or you can also change with asset helper function of laravel to add your full URL of the current host. like

    <link href="{{asset('css/normalize.css')}}" rel="stylesheet" type="text/css">

{{asset('css/normalize.css')}} will be https://yourhost.com/css/normalize.css

Laravel 5.7 - CSS and JS not loading using URL::asset

The asset() helper prepends the base URL to the path you supply.

The base URL is the location of index.php (in this case: http://localhost/mysite/).

If you don't want index.php inside /public you will need to use public/ inside your asset path, like this:

asset("public/css/style.css")

Same for the js files, I hope you understand.

Phalcon stylesheets and javascript don't load for non-base routes

Refer to this stack overflow question

Essentially you need an assets manager. You can then load this assets manager in your main layout (if you have one) and have your other views extend your main layout.



Related Topics



Leave a reply



Submit