Style breaks on some Laravel pages - php

I'm using Laravel 5.3 and on some pages the css isn't loading. I tried the suggestions from this question, but they didn't help.
This is what I did so far:
I installed Laravel on wamp inside www/lblog directory so I access laravel's main page via http://localhost:8080/lblog/
To get rid of the "public" part from the URL I moved the index.php and htaccess files from the "public" directory to the root directory and changed two lines in index.php like this:
require DIR.'./bootstrap/autoload.php';
$app = require_once DIR.'./bootstrap/app.php';
In "resources/views/layouts/app.blade.php" I have the styles linked like this:
I tried to remove the / before the "css", it didn't work that way either.
The main page is ok, but if I go to http://localhost:8080/lblog/login for example, the style is broken.

Don't hack the core, use it as it is which is recommended.
Use laravel's built in helpers for generating URL for your assets that are on public directory
<link rel="stylesheet" href="{{ asset('css/styles.css') }}">
which will properly generate URL for your files that are on public directory

Related

symfony 5 - Path to the assets from subdirectories

I've got a problem with the path of assets on Symfony 5.
This is a structure of assets in the project:
app/
public/
css/
style.css
...
...
Everythings works fine when I do not have subdirectories in URL, but when URL looks like:
https://example.com/catalog/course
Symfony looking for file:
https://example.com/catalog/course/style/style.css
The properly address is:
https://example.com/css/style.css
That works like we can read in documentation:
https://symfony.com/doc/current/templates.html#linking-to-css-javascript-and-image-assets
Regardless of the subdirectories in URl, I have only one style.css. How can I determine to looking for the properly path of assets?
In Symfony, you can use the asset() function to generate the correct URL for an asset, regardless of the subdirectories in the URL.
This function takes the path to the asset, relative to the public directory, as its argument. For example, to link to the style.css file in the example you provided, you would use the following code in your Twig template:
<link rel="stylesheet" href="{{ asset('css/style.css') }}">

Css and Js files in my New Laravel Project are not loading

I was given a Laravel project and I manage to download it's configuration and get it started on php artisan serve.
When I open it on localhost:8000 It opens but only the html portion of it.
It seems like css files didn't load for it.
Is there some sort of package we need to install with composer in my environment.
To get the css working.
The public folder looks like this in the laravel project.
backend favicon.ico front images index.html index.php js robots.txt sql vendor
web.config
Laravel provides a helper function, asset(), which generates a URL for your assets. You can use this in blade syntax.
Put your css, js files on public folder. for example of a css file, put it on :
../public/bootstrap/css/bootstrap.min.css
In your blade you can access this file from header like this :
<link href="{{ asset('bootstrap/css/bootstrap.min.css') }}" rel="stylesheet" type="text/css" >
hope this help

Laravel URLs not working in subfolder

I've finished my laravel project and played it in a subfolder on my domain.
https://example.com/swap
So the above is the root of my directory and when I go there, I do get the index. But now every link I press, I'm redirected to the root of my domain. So I'm getting:
https://example.com/events/1
Instead of https://example.com/swap/events/1
Am I forced to change all URLs by hand in my files are is there a way my htaccess can always redirect to this 'swap' folder and place everything behind that?
I've tried using groups in my routes, without success.
Use url for all your links which are inside views folder:
For subfolder link use:
Sub-Folder Link
For root folder links use this syntax:
Index
There might arise issues with stylesheet and javascript in 'swap" sub-folder links with a 404 error. To fix that, use:
<script type="text/javascript" src="{{asset('js/app.js')}}">
<link rel="stylesheet" type="text/css" href="{{asset('css/app.css')}}">
You need to direct your VHOST to laravel's public folder. This is done for security purposes. Ideally your laravel install is outside the public folder, thus you would have yourdomain.com contain your laravel install and rename your public folder to swap.
IF you really want the name swap in your routes you could use route prefix with groups.
Open .env File and Change Your App Url to https://example.com/swap/ and use routes as links of whole application
All Url's Will Work fine.
When putting a laravel application in a subfolder. You need to modify your .htaccess for subfolder hosting. And add the following:
RewriteBase /swap
This way, the app knows when you go to route {{ url('/hello-world') }}, it will ultimately go to "/swap/hello-world"

External CSS in Laravel 5.2 project

I am currently using Laravel 5.2 framework for my web dev project and stuck on problem. In my dashboard.blade.php i want specify the link to an external css file which is in public/src/css/main.css.
I used URL::to() and URL::asset() methods to get the absolute path to the CSS file but still it is not working.
When i placed my CSS file just inside the public directory everything worked fine but when i place it inside any subdirectory in the public directory it doesnot work.
I am homestead along with the laravel as development environment.
dashboard.blade.php file
directory structure of the project
Please put your css in public folder
<link type="text/css" rel="stylesheet" href="{{ asset('css/style.css')}}">
No matter where you place your css file inside public folder you can get it using the code :
<link rel=stylesheet" href="{{asset('src/css/main.css')}}">
For this code place your css file inside public/src/css
In your image of directory there is no folder as css inside src

laravel 4 css files 404 not found

These are my folders:
app
--->views
------->css
----------->font-awesome.css
----------->index.css
------->layouts
----------->restaurants.blade.php
------->restaurants
----------->create.blade.php
in restaurants.blade.php
I tried this:
<link rel="stylesheet" href="{{ URL::asset('views/css/index.css') }}" />
{{ HTML::style('views/css/font-awesome.css') }}
in both way, I got this exception:
http://localhost:8082/ProjectName/public/views/css/index.css 404 (Not Found)
http://localhost:8082/ProjectName/public/views/css/font-awesome.css 404 file not found
Could you help please?
Your assets are stored under your views, but you are linking to public.
Put the assets that you need to load (css, imgs, js) under public. Then you can simply
{{asset('css/style.css')}}
for example.
Also make sure your config/app.php document root is setup right, and if using apache, make sure that your vhost is setup properly to where /public/ is your Document Root.
For using laravel with Vite
#vite("resources/css/app.css")

Categories