Support the ongoing development of Laravel.io →
posted 7 years ago
Views
Last updated 2 years ago.
0

Although I'm not very experienced in laravel, i know what's probably happening.

When visiting /articles, the css and js files are being served from root "/" directory still, in a relative manner (files are in their correct spot. /css/styles.css, for example).

However, when you go two directories deep in the url structure, the css and js files are being served relative to the current directory, which is now /articles/. Now the Web server is looking for these files in locations such as /articles/css/style.css, which obviously does not exist.

Possible fixes:

  • use base tag in head of document with full root url as href attribute value (not best solution)

Example:

 ......  
<title>app name</title>
<base href="http://www.yourdomain.com">
 .......
  • alternatively, and perhaps better, use base tag with route helper in blade view to fetch root url (still not best solution)

Example :

 ......   
<title>app name</title>
<base href="{{ route('index.show') }}"
  ......  

With example of named route of the following in app/Http/routes.php

Route::get('/',  ['uses' => 'IndexController@showIndex',  'as'  => 'index.show']);

By using the route helper method above you are affording yourself being untied to your current routes and url structure. You could change the / to /newindex in routes and the base href in this template would remain the same provided the route name stays the same too.

  • setting rewritebase in Apache .htaccess (or similar server config file) - (it'll work, but still not best)
RewriteEngine On
RewriteBase /
  • use full url with domain name in link and script tags (still not best)
<script src="http://fulldomain.com/scripts/name.js">

And, finally :

  • use URL::asset() (this is the best solution)
<script src="{{ URL::asset('css/styles.css") }}
Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.