简体   繁体   English

Laravel:图像未加载

[英]Laravel: Image not loading

I am trying to load an image in Laravel.我正在尝试在 Laravel 中加载图像。 The image is loading in a header.blade.php file.图像正在加载到header.blade.php文件中。 It is working fine on other pages but when it comes to localhost:8000/edit/id , It just doesn't work.它在其他页面上运行良好,但是当涉及到localhost:8000/edit/id ,它就不起作用了。 I am loading Image like this:我正在加载这样的图像:

<a href="{!! URL::to('/') !!}">
   <img src="images/logo.png" />
</a>

My Route:我的路线:

Route::get('edit/{id}', array('uses' => 'HomeController@edit'));

My Controller:我的控制器:

 public function edit($id) {
    $reg = Register::find($id);
    $user = User::where('email', Session::get('email'))->first();
    if ($reg->user_id == $user->id) {
        return View::make('edit')
                        ->with('id', $id);
    } else {
        return Redirect::to('dashboard')->with('wrong', 'Sorry! Something Went Wrong!');
    }
}

My image directory: public/images/logo.png我的图片目录: public/images/logo.png

It is working fine on other pages like localhost:8000/about but it doesn't work on localhost/edit/2 , any help?它在localhost:8000/about等其他页面上工作正常,但在localhost/edit/2上不起作用,有什么帮助吗?

The problem is that when you are on your url, it is not looking for the image in the root folder of your application, but in the current location.问题是当你在你的 url 上时,它不是在你的应用程序的根文件夹中寻找图像,而是在当前位置。

So when you are visiting localhost/edit/2, it is looking for the image in localhost/edit, instead of in localhost.因此,当您访问 localhost/edit/2 时,它会在 localhost/edit 中查找图像,而不是在 localhost 中。

Try changing your code by adding a slash in the beginning of the image尝试通过在图像开头添加斜杠来更改代码

<a href="{!! URL::to('/') !!}">
   <img src="images/logo.png" />
</a>

to

<a href="{!! URL::to('/') !!}">
   <img src="/images/logo.png" />
</a>

Additionally, you should actually try the laravel way, like this:此外,您实际上应该尝试使用 laravel 的方式,如下所示:

{{ HTML::image('images/logo.png', 'logo') }}

try尝试

{{ HTML::image('images/logo.png', 'logo') }}

output will be like输出会像

<img src="http://your.url/images/logo.png" alt="logo">

also you can check image path using inspect element in browser您也可以使用浏览器中的检查元素检查图像路径

<img src="public/storage/cover_images/image_name.format"  class="img-resposive">

它将加载您的图片,否则请检查图像的路径和文件夹的预览,出于安全原因,您只能从公共路径加载图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM