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
Related
I have setup a Laravel(8.83.23) with Jetstream(2.7.5) and Inertia laravel(0.3.6)
Im using Vuejs(2.7.3) with vue-router(3.6.4) and it seem inertia-vue(0.5.12)
Everything work fine except for the component that should be shown after a successful login which is my dashboard. I have setup vue-router to use /app/ as a base .
When I log in, the redirection to /app/dashboard work fine but my Dashboard.vue Page is not shown. If i access directly the url it work, any router-link to it work too. Accessing /login AFTER being logged work as it redirect correctly and the page is shown. The only time the page is not shown is when I'm authenticating. Any other component are shown (like a nav that show only when logged in)
After investigating $route.path, it seem to stuck for an unknown reason to /login.
This happen only when I do a successful login attempts.
I don't know where to look next to try to fix this. I'm suspecting something within the <router-view></router-view> as other component outside of it works well but how can I address this very specific situation?
EDIT:
Here is the code of the App.vue
<template>
<app-layout>
<v-row>
<v-col class="pa-0 mb-3">
<v-toolbar elevation="1" dense>
<v-toolbar-title>App</v-toolbar-title>
<template v-slot:extension>
<v-tabs show-arrows :hide-slider="!inAppLinks">
<v-tab v-for="link in appLinks" :to="link"
>Go to {{ link }}</v-tab
>
</v-tabs>
</template>
</v-toolbar>
</v-col>
</v-row>
<router-view></router-view>
</app-layout>
</template>
And the code of route.js:
paths : [
{ path:'/dashboard', component:Dashboard },
{ path:'/foo', component: Sample },
{ path:'/bar', component: Sample },
{ path:'/faz', component: Sample },
{ path:'/baz', component: Sample },
{ path:'/login', redirect: '/dashboard' }
]
vue-router use a /app/ as base url. As I don't have /app/login, making a redirect from /app/login to /app/dashboard work, but this does not solve the problem
I've setup a demo at demo.concept-net.net
You can login with demo#concept-net.net and password demodemo
The demo doesn't have the redirect
As I don't get an answer, and I didn't find a better solution, I'm gonna stick to the redirect for now as it kinda work.
So what I did was a redirect from /login to /dashboard inside vue-router as the login is not managed there it seem to cause no issues(all related things to vue-router use /app/ as a base URL and the login is manage by jetstream on root url).
I created a repo using sveltekit and successfully deployed it with Netlify, however the images will not load, their 'alt' attribute is all that shows up as seen in the image.
If I run the project locally using 'npm run dev' the images DO appear fine.
Here is the code where I have the img tags:
<ul>
<li><img src="/src/images/github.png" alt=GitHub></a></li>
<li><img src="/src/images/linkedin.png" alt=LinkedIn></a></li>
<li><img src="/src/images/email.png" alt=Email></a></li>
</ul>
Do I need to edit the paths somehow? Or maybe something in my svelte.config.js file:
import adapter from '#sveltejs/adapter-netlify';
/** #type {import('#sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter(),
}
};
export default config;
Put your image to this site https://cloudinary.com and get the link of the image and then try to use the link in src this will help you from the error.
Not very sure but SSR of sveltekit might be a problem in this case (Try vercel they seem to have better SSR support)! And yeah try putting your images in static folder .
I have blade file named index-game.blade.php. And in one line of it, a line that reaches a js file under laravel public folder.
<script type="text/javascript" src="{{asset('game/')}}/js/CPreloader.js"></script>
So far so good.
Also inside CPreloader.js, I have lines like this.
s_oSpriteLibrary.addSprite("bg_menu","./sprites/bg_menu.jpg");
s_oSpriteLibrary.addSprite("progress_bar","./sprites/progress_bar.png");
At this stage, I get the following error in the console of my browser's devtools screen:
bg_menu.jpg:1 GET http://127.0.0.1:8000/games/sprites/bg_menu.jpg 404 (Not Found)
progress_bar.png:1 GET http://127.0.0.1:8000/games/sprites/progress_bar.png 404 (Not Found)
This is the link where I called "index-game.blade.php" => http://127.0.0.1:8000/games/slot1 (Let's pay attention to the games word here)
It works if i change my javascript file to
s_oSpriteLibrary.addSprite("bg_menu","../game/sprites/bg_menu.jpg");
s_oSpriteLibrary.addSprite("progress_bar","../game/sprites/progress_bar.png");
Because the correct link is this instead
http://127.0.0.1:8000/games/sprites/bg_menu.jpg // wrong
http://127.0.0.1:8000/game/sprites/bg_menu.jpg // true
I don't want to change the paths in all my javascript files. Because this is a game and there are as many pictures as possible. How do I correctly identify the laravel public folder in my external js files.
There is a package for that: https://github.com/tighten/ziggy
Some copy/paste from docs:
Install Ziggy into your Laravel app with
composer require tightenco/ziggy
Generate javascript route file:
php artisan ziggy:generate
Import in js file and use:
// app.js
import route from 'ziggy';
import { Ziggy } from './ziggy';
// ...
route('home', undefined, undefined, Ziggy);
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
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