Linking to css file using asset in laravel - php

Instead of using localhost to access the file, I've used domain name lsapp.com
When I try to link to css file using asset, it links to lsapp.com/css/app.css but doesn't work. Same index.php file in public folder opens while using lsapp.com.
I've tried many different methods but none worked.Please help.
Currently using this but doesn't work
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
But if I use localhost to open the file, css works fine.
http://localhost/lsapp/public/index.php

Laravel using in latest version Ithink that would be more conventional.
mix.styles([
'asset/css/app.css',
]);
Please refer :
https://laracasts.com/discuss/channels/laravel/best-way-to-use-custom-css-in-laravel
https://laravel.com/docs/5.7/mix

I would recommend to watch the free lesson from laracast over laravel-mix.
https://laracasts.com/series/learn-laravel-mix/episodes/1
You will learn a lot about compiling css & js.

Make sure you have your .env file set up correctly.
"i've set url in config/app.php as 'url' => env( 'http:// www. lsapp.com', http:// localhost')" is wrong.
env() takes a variable name from your .env file, not an URL. Revert those changes back to the default:
'url' => env('APP_URL', 'http://localhost'),
This basically says, "url is either the value of APP_URL from your environment (.env file) or, if that doesn't exist, use http://localhost"
In your .env file, set APP_URL to http://www.lsapp.com, run php artisan config:cache to load and cache the values from .env and you should be good to go.

I think it would be work.
Maybe you are assuming that you have your CSS folder inside your public directory, I think you need to require illuminate/html to your project.

Related

how i create relative path alway begining root

I create my web app with php follow mvc model.
When I set path css,img,js,... i use <link rel="stylesheet" href="./public/css/style.css">
in default controller I got css for my page. But when I call action like http://localhost/assignment/controller/action i lost my css. relative path become http://localhost/assignment/controller/action/public/css/style.css or http://localhost/assignment/controller/action/param/public/css/style.css.
Sounds like you might have a configuration or bundler issue. The quickest - not best fix - for developing would be to move your css file to where the public folder is located. You need to find the root folder where that link is calling from and link there.
If you are using a bundler or transpiler, you might need to look in the configs for what is being bundled.
Also make sure to follow this format for local files.
The right way of setting <a href=""> when it's a local file

Laravel 5.5 APP_URL not changing

I am trying to change the path output of {{ url('images/image.png') }} and {{ asset('images/image.png') }} Based on my reading, it sounds like I just need to set APP_URL in my .env file and off we go. Well, unfortunately that is not the case. I have set APP_URL=http://localhost in my .env file. I also set 'url' => env('APP_URL', 'http://broken.af') in my config/app.php file. Additionally, I have also tried setting ASSET_URL in both locations with the same result. I have also run artisan cache:clear, artisan config:clear and artisan config:cache. I also tried restarting the server just to be extra sure. For some reason Laravel keeps using the servers configured hostname for these values.
Not a dupe, we are not talking about the route paths, we are talking about the url and asset function and APP_URL and ASSET_URL not doing anything.
I am going to go ahead and post the answer to my question. If UrlGenerator::forceRootUrl is not used, it will inevitably fallback to Request::root() which is implemented as follows: return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/');
I am not sure what ASSET_URL or SITE_URL do, if anything. These env variables are definitely not used in the url or asset functions. This does not appear to have changed in later versions of Laravel.
The only way I was able to find to get these variables to work was to manually read them in RouteServiceProvider::boot() and then call forceRootUrl.
public function boot()
{
parent::boot();
/** #var UrlGenerator $url */
$url = $this->app['url'];
// Force the application URL
$url->forceRootUrl(config('app.url'));
}
The trace:
https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L204
https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L492
https://github.com/illuminate/routing/blob/master/UrlGenerator.php#L494
https://github.com/illuminate/http/blob/master/Request.php#L89
As you are trying to get an asset, in your case it's an image it will be right to use asset() laravel helper,
for more information, please check laravel method-asset helper
If you have all your assets in the default public folder, use the Fully Qualified Domain Name (including security protocol) for your APP_URL in your .env file. Example:
APP_URL=https://broken.af
Also use the same for your config/app.php file:
'url' => env('APP_URL', 'https://broken.af'),
If this is helpful for someone, this code worked for me:
$url = env('APP_URL').Storage::url($file->path);
This is how I'm saving the file previously:
$file->path = $request->file("file")->store("public");
I found this answer here
p.d. Don't forget to do the symlink like documentation mention here
php artisan storage:link

Laravel 5.4 reference to Mix CSS file

I'm trying to set up a SASS to CSS compiler via Mix (previously Elixer) in Laravel 5.4 (using https://laravel.com/docs/5.4/mix). Everything is working fine, it compiles and I can set up a watcher via npm, but referencing it via the views/layout/app.blade.php file, like:
<link rel="stylesheet" href="{{ mix('css/app.css') }}">
doesn't work. In my browser, it is shown of course as:
<link rel="stylesheet" href="/css/app.css">
It is the wrong path. It should be referencing to http://localhost/website/public/css/app.css, but changing anything in the Mix code above, generates a Laravel error.
Unable to locate Mix file: /website/public/css/app.css.
Please check your webpack.mix.js output paths and try again.
What am I doing wrong? Do I need to set a base path somewhere, is my .htaccess wrong, or..? Thank you for any answers.
The path generated by the mix() helper is correct. Your domain should point to directly to "public" directory. Everything above "public" directory should not be accessible via HTTP (it's not safe).
So you need to create new domain (e.g. project.local) and point it to Laravel's public directory (e.g. /home/user/website/public/) or just change default root path in your webserver configuration (DocumentRoot if Apache). Then the URL http://project.local/css/app.css will work.
in webpack.mix.js file configure that file u need like this mix.js('resources/js/app.js', 'public/js') .postCss('resources/css/app.css', 'public/css', []);
after do command npm run watch for mix .
And then u can add your css file anywhere u want like
<link rel="stylesheet" href="{{ asset('css/app.css') }}"/>

Assets not referencing to public folder (Laravel)

I have the public folder inside my laravel project and I have some js and css files inside it.
I'm using the asset function and even though it's referencing to the public folder, my files aren't loaded on the page.
I'm using this code to load (it's only one example, there are more files):
<link href="{{ asset('css/style.css') }}" rel="stylesheet">
And on the browser's console, I'm geting something like this:
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8000/css/style.css
Well, I tried to revert the last commit, but no success. Tried to change to URL::asset() function, nothing. Tried everything from the following link: http://laravel.io/forum/09-17-2014-problem-asset-not-point-to-public-folder?page=1 and success.
Please, a little help?
Thanks!
I was having same problem. This is due to moving of .htaccess file from public to root of the project in order to serve localhost/project rather than localhost/project/laravel. And needed to use public as well in the asset:
<link href="{{ asset('public/css/app.css') }}" rel="stylesheet">
Or, modify the asset function from /Illuminate/Foundation/helpers.php
if (! function_exists('asset')) {
/**
* Generate an asset path for the application.
*
* #param string $path
* #param bool $secure
* #return string
*/
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
}
The preceding method is not good way. Anyway this would have been easier if there was config for setting asset path.
Add in your env:
ASSET_URL=public
or
ASSET_URL=public/
or
ASSET_URL=/public
which one u need u can do it...
In my case in .env I tried
ASSET_URL=public
with both side slash and alone, nothing helped along with all related SO answers, then opened my old project
and saw in. This worked for me finally (next to clearing views&caches, of course)
ASSET_URL="${APP_URL}"
im talking about localhost side
After you've savely and succesfully setup your .htaccess (like this), your method will work as you would expect it to work:
<link href="{{ asset('css/style.css') }}" rel="stylesheet">
Be aware that all client-side URL requests (requests to images, JavaScript/CSS files, JSON) will be affected by the Rewrite Rules after you've setup your .htaccess.
Also be aware that it might take a while after you see the changes. Cache might interfere with these changes. So, make sure you clear your Laravel cache:
In your Shell run the following commands individually:
php artisan cache:clear
php artisan route:cache
php artisan config:cache
php artisan view:clear
And make sure to disable caching temporarily in your browser settings:
In Google Chrome: Open Dev Tools > Open the Network tab > Check "Disable cache".
Other browsers: search online how to do that.
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
Try this
Add this is in your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
It Will solve your problem. SURE
Since you have moved your .htaccess file to the root folder you have to change the assets url wherever you are using. The URL should be like below,
<link href="{{ asset('public/css/style.css') }}" rel="stylesheet">
Or, You can just set the ASSET_URL as 'public' in the .env file,
ASSET_URL=http://localhost/projectname/public
sorry for my english, I have same problem in my case working with mike42/escpos-php was because in xampp
I move to zlib.output_compression=on
the correct way is
zlib.output_compression=off
and assets work again
stlye.css is loading and working for me from public folder. Don't forget to add rel="stylesheet"
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
And make sure to disable caching temporarily in your browser settings:
In Google Chrome:
Open Dev Tools > Open the Network tab > Check "Disable cache".
Other browsers: search online how to do that.
This worked for me. Seems the previous load of CSS and JS is in cache and until it is released the new ones do not load

Change default URL or path for whole application?

On my local development I face the issue that my applicaton uses relative path, to the domain, e.g. /css/style.css.
This works generally good, as I can configure virutal hosts on my develop machine, e.g. localhost.foo, so that the relative path is resolved to localhost.foo/css/style.css.
In my current situation, I cannot edit the hosts file to setup a development domain, so that the relative path to the domain does not work anymore. My develop url looks like localhost/projectfoo/public.
So that the app works, it should reference to localhost/projectfoo/public/css/style.css. Based on the relative path in the code it now reference to locahost/css/style.css, and there it will - obviously - not find the requested files.
Is there a way to configure laravel, to use at one case localhost/projectfoo/public as URL and on the other case the standard?
You can always add a new entry in your config files, lets say in the config/app.php
// this is the one that comes with laravel
'url' => 'http://localhost',
// this is the one that you can define
'url_public' => 'http://localhost/something_else/public'
and then use it in your view like:
<link href="{{ config('app.url_public') }}/style/style.css" rel="stylesheet">
Laravel has a helper function asset which generates a full URL for a relative path, based on your config app.url value.
For example, if app.url is http://localhost:
In your view put {{ asset("css/style.css") }} and Laravel will convert this to http://localhost/css/style.css
So in your case all you need to do is change app.url to http://localhost/public and then start using that asset method in your views instead of the relative path.
Better yet, add a new environment variable to your .env file (make sure to put it in .env.example as well) called APP_URL, e.g.
APP_URL=http://localhost
Then in config/app.php set it to:
'url' => env('APP_URL'),
That way you only have to set it once per environment

Categories