Access to images in the public directory with Symfony 4 - image

In two Symfony 4 instances, I get different results with the same code.
In a twig template, I have the line
<img src="{{ asset('images/DM_logo.jpg')}}"/>
In the online server, all works well: i get the image and the generated URL is /images/DM_logo.jpg for the absolute URL http://mysite.mydomain.com/images/DM_logo.jpg
But, locally, with the access to http://localhost/mysite/, the generated url is /mysite/images/DM_logo.jpg for the absolute url http://localhost/mysite/images/DM_logo.jpg which generate a NotFoundHttpException.
If i change the template to
<img src="{{ asset('public/images/DM_logo.jpg')}}"/>
it works locally but not online...
I doesn't find how to use the same code online and locally. Advices?

configure this
# config/services.yaml
parameters:
router.request_context.host: 'example.org'
router.request_context.base_url: 'my/path'
asset.request_context.base_path: '%router.request_context.base_url%'
https://symfony.com/doc/current/routing.html#routing-generating-urls

Related

What is best practice for image src in vue SFC?

I am using Laravel 5.8 and Vue 2.6 and trying to use a relative path for an image file in my single file component. I have reviewed the suggestions in How to import and use image in a Vue single file component?
but cannot get it to work.
When trying:
<template>
<div id="app">
<img src="./assets/logo.png">
</div>
</template>
<script>
export default {
}
</script>
<style lang="scss">
</style>
I get this:
This relative module was not found:
* ./assets/logo.png in ./node_modules/vue-loader/lib/loaders/templateLoader.js??vue-loader-options!./node_modules/vue-loader/lib?
?vue-loader-options!./resources/js/components/xxxx.vue?vue&type=template&id=abd0e146&scoped=true&
When I try:
<template>
<div id="app">
<img :src="image" />
</div>
</template>
<script>
import image from "./assets/logo.png"
export default {
data: function () {
return {
image: image
}
}
}
</script>
<style lang="scss">
</style>
I get:
This relative module was not found:
* ./assets/logo.png in ./node_modules/babel-loader/lib??ref--4-0!./node_modules/vue-loader/lib??vue-loader-options!./resources/js /components/xxxx.vue?vue&type=script&lang=js&
Am I missing something with webpack-mix or vue-loader?
Edit:
Using simple static reference (src="/assets/logo.png") works on local development server, but doesn't on production server.
I've seen numerous suggested solutions for this basic image file referencing, but puzzled that there are so many possibilities and not a single best practice. Again, hoping to find a Best Practice when using Laravel/Laravel Mix/Vue and SFC that works in development and production.
There are 2 ways of using an image in your setup:
Bundled with webpack
<img src="./assets/logo.png">
import image from "./assets/logo.png"
These bundle the image using webpack, and using this solution you get the benefits of:
Automatic inlinening with base64 for small images
Automatic "deep" compressing with any webpack loaders
Path works from every path in your app
Compile time checking if the file exists
Using your own webserver
const image = '/assets/logo.png'
const image = 'http://example.com/’
In these solutions, you need to setup your webserver yourself to serve those resources (or put them in the public directory)
Easy changing to a different image without rebuilding the front end
More flexible
The answer to this is that the wrong deployment for the production server was being used that tried pointing to the 'public' folder instead of locating the public files on public_html.
After deploying according to this post, of course relative paths work exactly the same on development and production servers.
The only difference is in Step 5 for Laravel 5.8 in the server.php file,
Change
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
To
if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
return false;
}
Relative paths like
<img src="img/logo.png">
on either server work the same.
It's unfortunate that Laravel docs don't go into more detail on this method of deployment since I suspect there are numerous developers wanting to deploy on shared servers at least for testing purposes.
Use
<img src="#/assets/logo.png">
This will use webpack and created a hashed version of your logo.png.
Or in JS use:
:src="require('#/assets/logo.png');
Provided that your image is stored in public folder and is accessible, you can link to it by absolute path just fine instead of using a relative path ./
<template>
<div id="app">
<img src="/public/img/widgets/logo.png" />
</div>
</template>
<script>
export default {};
</script>
<style lang="scss">
</style>
Given you have this structure
Hope this helps

Laravel Version Cache with Laravel Mix Not Working

I'm trying use laravel mix but in browser return error 404
Laravel 5.6.4
My .env APP_URL
APP_URL=http://localhost/base-site-template/public
My mix-manifest.json
{"/js/lib/echarts.js": "/js/lib/echarts.js?id=a24928e50f4d775d9e3e",
"/js/lib/moment.js": "/js/lib/moment.js?id=a3cde5d62118f1b1ac08",
"/js/lib/axios.js": "/js/lib/axios.js?id=1129cee8012a0ec7cb58",
"/css/lib/vuetify.css": "/css/lib/vuetify.css?id=c7533b5bc8f96cca4e90",
"/css/index.css": "/css/index.css?id=d41d8cd98f00b204e980",
"/css/template.css": "/css/template.css?id=c7533b5bc8f96cca4e90",
"/js/template.js": "/js/template.js?id=3213a53c38fb71543141",
"/js/lib/vue.js": "/js/lib/vue.js?id=e41f4d0b6a918ca9213d",
"/js/lib/lodash.js": "/js/lib/lodash.js?id=98dc43a5e921b41ef214",
"/js/lib/vee-validate.js": "/js/lib/vee-validate.js?id=bfc8600a0fc91c459f0e",
"/js/lib/vuetify.js": "/js/lib/vuetify.js?id=f0c3ad550b36770ee4c9",
"/js/index.js": "/js/index.js?id=5a79665b4d1331d942d7"}
I'm trying to get css and js files using {{ mix('js/template.js') }}
Can someone help me?
Your resources are trying to be loaded from the root directory:
You're accessing the site at /base-site-template/public not /. So you'd have to access the files at /base-site-template/public/js/index.js not /js/index.js.
If you have your APP_URL set properly, you can use the asset() helper to prefix your APP_URL.
{{ asset(mix('js/template.js')) }}
Assets are placed into the public/ directory when they're compiled, e.g:
public/js/lib/echarts.js
Your browser will treat relative requests beginning with / as relative to the base path which is http://localhost/. For example when you load the page http://localhost/base-site-template/public and it references /js/lib/echarts.js the browser will make this request:
http://localhost/js/lib/echarts.js
However that is not where your assets reside, your assets reside in:
http://localhost/base-site-template/public/js/lib/echarts.js
Therefore you will need to either:
Reference the assets using an absolute URL instead of a relative path
Tell clients browsers to use a different base path
Move your application to the base path
You can reference the assets with an absolute path using the asset helper:
asset(mix('public/js/lib/echarts.js'));
You can tell the client browser to use a different base path using the base element in your documents head:
<base href="http://localhost/base-site-template/public/">

display image from database in react in laravel

How can i display image form database ? in react ? i am using laravel as backend .this is my table with image
i am using {this.props.obj.image} and this is showing the file as shown below
as you can see it is showing the file it is not displying image .searched web but it says to import path but all the tutorial are importing statically
my image location is Assets/Admin/Image .
How can i resolve this
When you run a laravel project, you are already in the public folder so you don't have to specify the public folder in the path and "assets" is just a laravel-defined method for accessing the contents of the "public" directory so you shouldn't specify that in the path too.
So you should do something like this:
<img src="http://localhost:<your_port>/images/{this.props.obj.image}" alt="hello image" height="200" />
You are storing just the name of the file in the database, to load the image you need to complete the full path of the image.
If you are storing your files in:
{laravel_root_folder}/public/assets/images/
Then you should do something like this (btw I know nothing about react but this should kinda work). I'm asuming you are running your server in localhost in the 8000 port:
<img src="http://localhost:8000/assets/images/{this.props.obj.image}" alt="hello image" height="200"/>
You can not have directly access to public/storage/images from react (front end side ) for that you have to use following method to generate the URL of image and then just return that url to react side while requesting and use that url in < img src={url} /> to display the picture
https://laravel.com/docs/5.4/filesystem#file-urls

the server responded with a status of 404 (Not Found) Failed to load resource

I am facing issue in accessing public folder of laravel.
I am on ubuntu 17.10.
I am trying to run the app on localhost.
I am accessing like this:
<img src="{{URL::asset('public/assets/img/person.jpg')}}"/>
Also when checking the its source , its path is shown correct :
<img src="http://localhost:8000/public/assets/img/person.jpg"/>
It may seem duplicate but I've tried most of the solutions present on net.
You are calling the wrong directory
Instead of
<img src="{{URL::asset('public/assets/img/person.jpg')}}"/>
Use this instead
<img src="{{asset('assets/img/person.jpg')}}"/>
Considering the assets folder is existing in public folder.
I hope this will solve you :)
Just in case file had cached on your browser try to add query string on your file by doing this so browser will treat it as a new file
You shoud use Storage::get() to retrive the file url. See the docs
anyway, you don't need that public in the path:
Switch
<img src="{{URL::asset('public/assets/img/person.jpg')}}"/>
to
<img src="{{URL::asset('assets/img/person.jpg')}}"/>

image display in codeigniter shows error

I am relatively new to Codeigniter, basically I choose CakePHP to develope applications. Now I am editing a Codeigniter Project, build in version 2. I have some images in my table and I need to disply it.
I have created a helper file general_helper.php in which I have a function
function image_url(){
return base_url().'uploads/';
}
So in view page , I tried to display image by
<img src="http://localhost/myapp/uploads/vendors/images/Desert.jpg" />
My directory structure is :
myapp
-uploads
-vendors
-images
-logos
But its not showing the image ! When I tried to put the image path in the url, it shows 404 error :( whats wrong ?
Codeigniter base_url function requires folder path relative to site's base url passed as parameters. Please try use below code.
First in constructor, please load your library -
$this->load->library('general');
Then please pass your uploads directory as parameter to base_url
function image_url(){
return base_url('uploads/');
}
If images are not uploading please do check image type.
Also try use log_message('info','info_message') function to record base url.
Please refer http://www.codeigniter.com/userguide2/general/errors.html

Resources