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

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

Related

Laravel CSS file link correction

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

CSS doesn't load on one of my route/view

I have a sub-view that extend my master-view, and for some reasons all my css doesn't seem to load only on that page. I extend them as usual like I normally do.
view show.blade.php
#extends('layouts.internal.master')
#section('content')
TESTING ...
#stop
I notice, it only break on that route : /account/112
Any hints / suggestion on this will be much appreciated !
Without seeing your file paths or how you are loading your CSS, it's very difficult for us to say but it sounds like your file paths are wrong.
There is a difference between these two:
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" href="/css/app.css">
Note the forward slash.
The first one is relative to your current path. So, for example, if you're currently at http://localhost:8000/account/112, the first one is like saying http://localhost:8000/account/css/app.css, which is most likely unwanted behavior. The second one is relative to the root directory so it will always be http://localhost:8000/css/app.css, which is probably what you want to do.
Laravel also comes with helpers function like asset so if you decide to use that and do something like this:
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
It will generate an absolute URL / full path from your root URL. So, it'll generate something like this:
<link rel="stylesheet" href="http://localhost:8000/css/app.css">
/account and /account/112 can not access same path in same way. Like if /account access a image like 'images/img/img.jpg' , /account/112 need to access it like '../images/img/img.jpg'. Its all about folder structure. Now for your problem you need to use asset like below (laravel 5 specific)
// if you have save css file at as root/public/css/main.css
{!! HTML::style('css/app.css') !!}
remember to add to composer.json
"illuminate/html": "5.*"
And to aliases
'Form' => Illuminate\Html\FormFacade::class,
'HTML' => Illuminate\Html\HtmlFacade::class
Point to be noted:
{{asset('css/some.css')}}
this is for laravel4. you wont get it working in laravel 5.

Laravel JS file path when included in View it appends namespace in the path

I am using following code to call my js file that further calls all other files
{{ HTML::script('js/script.js') }}
i want following URI
pathToMyProject/js/jquery.ui.totop.js // as laravell loads its files
what i am getting is
pathToMyProject/employees/js/jquery.ui.totop.js"
just to mention i am calling the js files from Views/Employees/Layouts/Main and my js is in public/js/allFiles
Since you are using Laravel 4 I will suggest you to use below method to src the script
<script src="{{ URL::asset('js/script.js') }}"></script>
URL::asset will link to your project/public/js/script.js folder.
Make sure you are using php artisan serve command to serve your app.
If above not worked, I will suggest you to add a bese tag in header also
<header>
...
<base href="/" target="_top">
and then use your code
{{ HTML::script('js/script.js') }}
Note: You'll need to be using blade templates with .blade.php extension to use this.

Correct path to resources

I'm having trouble with the path when a file calls a css or a js file.
I'm using Codeigniter also Smarty template engine. My base URL is> http://local.project
My css and js files are located in> project/site/assets/css and project/site/assets/js
The thing is that when I call the resources from the view the path is a mess and not found error appears.
I have tried many ways but still can't make it. I begin to think that this can be a path problem from Codeigniter. I mean.. maybe I'm missing something that I can't figure what is.
When I do an inspection with the browser I can see that this is set by default>
http://local.project/
and then from the view I call it like>
<script type="text/javascript" src="{site_url()}assets/js/jquery.flot/jquery.flot.js"></script>
but the path that is built is>
http://local.project/%7Bsite_url()%7Dassets/js/jquery.flot/jquery.flot.js 400 (Bad Request)
I'm driving crazy here, any help would be very appreciated!
try:
<script type="text/javascript" src="/assets/js/jquery.flot/jquery.flot.js"></script>
or
<script type="text/javascript" src="/js/jquery.flot/jquery.flot.js"></script>
can you provide your ip,to view the page?
Try using base_url() instead of site_url().
try:
<?php
echo "<script type='text/javascript' src='" . base_url() . "assets/js/jquery.flot/jquery.flot.js'></script>" ;
?>
To use the base_url() you need to call in your controller the URL helper:
$this->load->helper('url');
Then in your view:
<script type="text/javascript" src="<?php echo base_url();?>assets/js/jquery.flot/jquery.flot.js"></script>
First of all load the url helper
$this->load->helper('url');
secondly get base address using this
echo base_url();
and finally your base address is "http://local.project", no slash at the end, so try this
<script type="text/javascript" src="<?php echo base_url();?>/assets/js/jquery.flot/jquery.flot.js"></script>
and make sure your base address do not have any extra characters, like space or enter
I could make it. Just opened my Smarty.php file and changed this line view/admin just to views
$this->setTemplateDir(APPPATH.'views');
After that the url I could use from the view is
<script type="text/javascript" src="assets/js/jquery.flot/jquery.flot.js"></script>

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