Laravel - how to access public folder from JS file - laravel

I am using the exact same Ajax call in multiple blade templates.
If Ajax fails, I show an error jpg located in public/images/danger.png.
When I had my JS in the blade file written inline in <script> tags, it was easily accessible as I had blade helpers to access public with:
{{asset('/images/danger.png')}}
Yet now, accessing /public with:
http://"+location.host +"/public/images/danger.png
is impossible.
It would be unDRY to put the exact same AJAX call in 12 blades, no need to elaborate why..
How do you guys counter this issue?

remove /public from your route,you can directly use /images/imgname.jpg

You can just use absolute routes in your javascript files. This will however not work if your Laravel application sits in a sub folder.
"/public/images/danger.png"

just the same as used in html (in blade)
like
$('.abc').attr("src", "{{asset('imgs/male_b.svg')}}");

Related

vue does not display .svg file in local env

I am making some project with Laravel, Vue2 now.
I deployed my project onto the test server, there .svg files display, but in my local environment they don't display.
I use :src="asset('images/ico_arrow_left1.svg')" in vue component.
My images and svg files are all in root/public/images folder.
For example; I wrote the following code to display svg in vue component.
<a #click="previousDay"><img class="ico_ico_arrow" :src="asset('images/ico_arrow_left1.svg')"></a>
The codes are sampe in test server and local.
show in test server, but not show in local, why?
If you want import a file from public folder you should check the documentation. You should use require when you import dynamic asset. Like this:
<img class="ico_ico_arrow" :src="require('images/ico_arrow_left1.svg')"/>
The asset() helper function is only for the Laravel Blade template. you can't use it inside of a regular Vue application.
In Vue, you should use require()in order to solve this problem.

laravel not picking file through asset()

In my laravel project i am trying to get js file though asset('js/app.js') which is inside public folder. it is working fine on localhost but on server it is not picking file. It is working on another server. I think there is any issue with htaccess file.
I try to use url() and URL::asset() but not working. I checked source of page link is perfect but still fail to pick file
I want to access that js file.
The question has been answered in the comments, and was solved with this code:
asset('public/js/app.js')

How is it possible to include a .blade file from outside of the Laravel project?

I have a Laravel 5.8 project and I'm using Blade to display the website.
However, I have .blade files outside of the Laravel project, that I'd like to #include() or display from the controller with return view(); with blade.
Here is a similar folder structure:
/var/www/examplesite1/
/var/www/examplesite2/
/var/www/laravel/
/var/www/storage/blade/
So for example the Laravel application is on the /var/www/laravel/ path. The blade file I'd like to get is from this path /var/www/storage/blade/.
What I tried was:
return view('/var/www/storage/blade/file.blade.php');
include('/var/www/storage/blade/file.blade.php');
None of them worked, becauase I got an error like this: View [.var.www.storage.blade.file.blade.php] not found.
I also tried it with omitting the 'blade.php' from the end, but it's the same.
Is it possible to include a blade file from outside the Laravel project? If yes, how?
You can add new directories to the views paths in config/views.php. https://laravel-news.com/laravel-view-path

need to add css,js files inside the modules folder CI hmvc

I am working on a CodeIgniter - HMVC
What I know:
is I should not create assets like css,js,images inside the application folder in codeigniter, but I have a necessity for it.
**What I am trying to achieve is : **
to create a standalone module and trying to use it in another project. In that case, I really need to add the necessary css,js,images files into the modules folder it self.
The Error I get is:
You don't have permission to access /v7-bitbucket/application/modules/some_modle/assets/bootstrap.min.css on this server.
How do I overcome this?
Please advice.
Thanks.
There are 2 ways I can think of for you to access this data.
The first one is using an htaccess and a custom rule, which redirects certain files to inside the other directory. This way, the user will still have access to only certain specific files outside of the public HTML folder.
The second option is to use PHP and render the content of the file in custom tags.
Ex: Instead of entering <link href="<?= $path ?>">, you would do <style><?= file_get_contents($path) ?></style>. This would be harder to cache, and a bit harder to implement for images.

Writing JS code in blade templating engine

I have a Laravel 5.0 project and inside of that I have a foo.blade.php file,in here I want to use some Javascript to get the pathname of the link the user currently in like so
window.location.pathname
Is it possible? Or blade templating allows only php to be written on the file?
First, Javascript runs client side, so whatever you write in <script> tag will be totally ignored by laravel.
But if you want to access path (route) in laravel, you can check this question.

Resources