Laravel:图像未加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31805544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:06:51  来源:igfitidea点击:

Laravel: Image not loading

phpimagelaravel

提问by Saani

I am trying to load an image in Laravel. The image is loading in a header.blade.phpfile. It is working fine on other pages but when it comes to localhost:8000/edit/id, It just doesn't work. I am loading Image like this:

我正在尝试在 Laravel 中加载图像。图像正在加载到header.blade.php文件中。它在其他页面上运行良好,但是当涉及到时localhost:8000/edit/id,它就不起作用了。我正在加载这样的图像:

<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/aboutbut it doesn't work on localhost/edit/2, any help?

它在其他页面上工作正常,localhost:8000/about但在 上不起作用localhost/edit/2,有什么帮助吗?

回答by ThomasVdBerge

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') }}

回答by Saani

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

您也可以使用浏览器中的检查元素检查图像路径

回答by Saani

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

it will load your picture otherwise check the path to image and previlages of the folders you can load image only from a public path due to security reasons

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