Laravel CSS file link correction - php

I need to move a laravel setup to another server.
Before moving I am testing the same on my localhost.
Everything appears to work right except I am getting wrong link for CSS, js files like
GET
http://127.0.0.1/css/main.css
whereas my laravel phyiscal folders are like this
e:\wamp\www\test\server-migrate\megashopping_dk\css\main.css
Now I don't know where to define the path in laravel for CSS, Js.

if you have a layouts.blade.php file which you use on every page of your website, you should define the path there with this code
{{ URL::to('/') }}/css/main.css
this code goes to the homepage of your website and then goes to css/main.css
Let me know if it worked

You have to place your assets (js & css) somewhere under /public
You can include it like this:
<script src="{{ secure_asset('bower\jquery\dist\jquery.min.js') }}"></script>
<script src="{{ secure_asset('bower\materialize\dist\js\materialize.min.js') }}"></script>
The file in this example is located at project path
/public/bower/jquery/dist/jquery.min.js

Related

I am getting wrong assets url using Laravel helper "secure_asset"

I have a website on domain mydomain.space
I include in my blade file some js file in such way:
<script type="text/javascript" src="{!! secure_asset('js/app.js') !!}"></script>
But then, on production, I see in browser logs, that frontend try to load file by this URL:
http://mydomainspace/js/app.js
Yeah, for some reason it removes dot and as result - it is wrong URL...
But when I display APP_URL variable - it shows "mydomain.space" (with dot).
Why I lose dot in URL when use secure_asset helper?
I would try using mix instead:
<script type="text/javascript" src="{{ mix('js/app.js') }}"></script>
It provides a lot of benefits and may resolve your issue https://laravel.com/docs/8.x/mix
That being said I would ensure your APP_URL is not being changed in the app.php file
Setting ASSET_URL in .env was a fix for me:
ASSET_URL=http://localhost

Laravel js Error GET http://localhost:8000/admin/product/js/jquery-3.2.1.min.js 404 (Not Found)

Good morning I write because I have the following problem:
on my home page the js files are loaded correctly but when i enter another page and it is called via "#extends ('admin.layout.admin')" the nav bar will no longer load .js files ... please help me
enter image description here
Did you try
asset
method to load your js file in your code?
put your js file under "public" directory and
try
asset("your js file name")
Or try to use absolutely path
put jquery-3.2.1.min.js file in public/js folder.
link jquery-3.2.1.min.js in head tag.
<script src="{{ asset('js/jquery-3.2.1.min.js') }}" type="text/javascript"></script>

how to setup the assets folder in CodeIgniter like in Laravel

like the public folder given by laravel default i tried to create something like that for easy make up of my css, js, picture and so on. I tried configuring it like this. In my application/helpers/utility_helpers.php
<?php
function asset_url()
{
return base_url().'assets/';
}
?>
I have my assests folder along with application, system, user_guide etc.
i confugured the autoload function of application/config as :
$autoload['helper'] = array('url','utility');
when i hit any url in browser now, it says
An Error Was Encountered
Unable to load the requested file: helpers/utility_helper.php
how can i make it and can you explain me if i need to specify root for that helpers also.And if not can i simply access the assets like this:
<img src="assets/base_components/build/banner.png">
in my view
In fact the answer is very simple, you only need to put ../ before every file that is under assets folder, because the root of the codeigniter is in application folder, but how you can't put the assets folder there and for the best practices, let the structure of the project like below and apply this solution to the path of the files:
Website Folder structure
|---|application\
|-------(CodeIgniter application folders...)
|---|assets\
|-------css\
|-------images\
|-------js\
|-------robots.txt
|---|system
|-----(CodeIgniter system folder...)
|index.php
Path of the files
ex.:
CSS
<link href="../assets/css/bootstrap.min.css" rel="stylesheet">
<link href="../assets/css/plugins/font-awesome.css" rel="stylesheet">
IMAGES and others
<img src="../assets/images/build/banner.png">
JAVASCRIPT
<script src="../assets/js/jquery-2.1.4.js"></script>
<script src="../assets/js/plugins/jquery.scrollSpeed.js"></script>
Note.:
Always remember to applay the best practices to the code too (this will help for the performance of the the page too), like below:
<html>
<head>
meta tags
favicons
css files styles
</head>
<body>
page structure code
javascript files
</body>
</html>
Yo can do this instead:
<img src="<?= base_url('assets/base_components/build/banner.png') ?>">
Your code looks ok, it should work.
According to the error
Unable to load the requested file: helpers/utility_helper.php
there is no utility_helper.php file in application/helpers/ folder. Check if the file exists in that folder or if you don't have a typo in the file name.

Codeigniter not displaying stylsheet when clicking on links

I just built a sit in Codeigniter, but when I click on a menu item the stylesheet breaks and it displays all of the content broken out on the page.
http://marciabrownproductions.com/
I uploaded it to a test server and all the links works perfectly.But when I uploaded the finished site ot my clients server the css issue starts. Is there something wrong with my clients server? Or is there a problem with how I coded the site? Why does it work on one server, but not the other.
Thanks!
First, I recommend using absolute paths instead of relative paths when referencing your assets. At the moment, they are being picked up with the index.php included.
So, let's say your link is http://domain.tld/index.php/home/actors, you should be calling your assets from their actual URLs - http://domain.tld/index.php/_/css/style.css would not work, for example. http://domain.tld/_/css/style.css, however, would work. (That's just a sample URL.)
I'd also recommend removing the index.php from your links, simply by using the htaccess RewriteEngine, and removing index.php from your app's config file.
Have a look at how your current source is being generated in a browser (right-click, View Source) to see what I mean.
Update:
Based on your comment, that may be an htaccess issue. Check to see if your hosting provider supports it.
In the meantime, put the index.php back in the app's main config file. However, leave your CSS asset link as it is. Make sure all your asset links look like this:
<link href="http://marciabrownproductions.com/_/css/styles.css" rel="stylesheet" type="text/css" />
<script type='text/javascript' src='http://marciabrownproductions.com/_/js/jquery.validate.js'></script>
<script type='text/javascript' src='http://marciabrownproductions.com/_/js/effects.js'></script>
<link href="http://marciabrownproductions.com/_/css/bjqs.css" rel="stylesheet" type="text/css" media="screen" />
<script src="http://marciabrownproductions.com/_/js/bjqs-1.3.js"></script>
Your images are broken because their links are also including the index.php/home. You need to make sure that that is taken out. (What are you using to generate your links anyway?)

Which type of URL is better for Laravel?

If I use relative paths, like this:
<script src="../../../public/js/jquery-1.8.3.min.js"></script>
They don't work with Laravel!
When I use Root Relative Paths like this:
<script src="/sis_colgate/public/js/jquery-1.8.3.min.js"></script>
Everything works fine but if I have to change location, I have to modify all of them!
If I use Laravel URL::base()
<?php echo'<script src="'.URL::base().'/js/jquery-1.8.3.min.js"></script>'; ?>
It works fine but on client-side (Dreamweaver) I can't see the images or the links for my scripts.
Is there any other way to do this?
Thanks!
The most correct option is to use the URL::base().
But you should not use dreamweaver to see your front end.
You can use it to write the code, but remember that dreamweaver is not a browser and it will never be.
To see the result of your code you have to setup a apache server or upload the code to a server.
You can use URL::to_asset('js/jquery-1.8.3.min.js'); in Laravel 3 and URL::asset('js/jquery-1.8.3.min.js'); in Laravel 4.
The problem with relative URL's is it depends on what your current location is. You should consider using URL::to_asset() which is the best choice for your current situtation. It uses absolute path and the base path is configurable just incase you plan on hosting your assets elsewhere (say, amazon S3/Cloudfront).
Dear all
One could also do this in Laravel 4:
<script src="<?php echo asset('js/main'); ?>"></script>
or the blade templating equivalent
<script src="{{ asset('js/main.js') }}"></script>
Have a good one!
Dreamweaver allows you to set a base folder in its preferences, so you should be able to use absolute paths. Generally your base folder should be at the /sis_colgate/public/ directory, not anything above it. That way it doesn't depend on the directory structure of your current computer.

Categories