简体   繁体   English

Lumen 的 url() 函数执行异常

[英]Lumen's url() function performing strangely

I recently did a fresh install of Lumen framework and started building a site from it.我最近重新安装了 Lumen 框架并开始从它构建一个站点。 Yes I know lumen is designed only for APIs but please help me out with this.是的,我知道 lumen 仅适用于 API,但请帮我解决这个问题。

I have placed all my views inside /resources/views and my templates inside /resources/views/templates .我已将所有视图放在/resources/views 中,并将我的模板放在/resources/views/templates 中

Now since I had to place [css/js/images] somewhere, I thought placing all of that in public/assets/[css/js/images] would be nice.现在因为我不得不将[css/js/images]放在某个地方,我认为将所有这些放在public/assets/[css/js/images] 中会很好。

Now in my layout when I am trying to include css - I use something like this -现在在我的布局中,当我尝试包含 css 时 - 我使用这样的东西 -

<link href="{{ url('assets/css/something.css') }}">

and it works giving an output of something like this -它可以提供类似这样的输出 -

<link href="localhost/assets/css/something.css">

same thing works for js also but it gets strange when I try to include images.同样的事情也适用于 js,但是当我尝试包含图像时它变得很奇怪。 For including an image I write something like -为了包含一个图像,我写了一些类似的东西 -

<img src ="{{ url('assets/images/someimage.jpg') }}"

and when I view page source, output is as I expect it to be -当我查看页面源代码时,输​​出正如我所期望的那样 -

<img src="localhost/assets/images/someimage.jpg">

but my console fires 404 not found errors stating someimage.jpg not found.但我的控制台会触发 404 not found 错误,说明找不到 someimage.jpg。 And when I crosscheck by inspecting the image's parent, the Url is totally different soemthing like this -当我通过检查图像的父项进行交叉检查时,Url 完全不同,就像这样 -

<img src="localhost/images/someimage.jpg">

See, automatically omitting 'assets' from image url and resulting in 404, but I can see correct url when I view page source.看,自动从图像 url 中省略 'assets' 并导致 404,但是当我查看页面源时我可以看到正确的 url。

Things I tried to resolve the issue -我试图解决问题的事情 -

  1. Cleared cache and reloaded the page.清除缓存并重新加载页面。

  2. Tried using asset() instead of url() but prototype of that was removed from lumen.尝试使用 asset() 而不是 url() 但它的原型已从 lumen 中删除。

  3. Pull out [css/js/images] folder from assets and pasted them in parent ie public.从资产中拉出 [css/js/images] 文件夹并将它们粘贴到父级即公共中。 This worked but then the question is why did the previous setup worked find for both css and js and caused problem only with images ?这行得通,但问题是为什么以前的设置对 css 和 js 都有效,而只导致图像出现问题?

My other questions are -我的其他问题是——

1. How can the url in page source be different from the one being rendered ? 1.页面源中的url如何与渲染中的url不同? Just to mention in my case the url in page source worked and displayed image but since the url being renderred omitted 'assets' from path hence resulted in 404.只是在我的情况下,页面源中的 url 工作并显示图像,但由于渲染的 url 省略了路径中的“资产”,因此导致 404。

2. Is there any other good way to include these assets in views. 2. 有没有其他好的方法将这些资产包含在视图中。 If yes then please also mention where to put them ?如果是,那么还请提及将它们放在哪里?

Attaching some images/code for reference.附上一些图像/代码以供参考。

Output of rendered page showing 404 errors for images but none for css.渲染页面的输出显示图像 404 错误,但 css 没有。

Output of view page source windows showing asset included in image path查看页面源窗口的输出显示图像路径中包含的资产

No clue if this is right, but I believe you actually need to put an image stream inside that URL.不知道这是否正确,但我相信您实际上需要在该 URL 中放入一个图像流。 Now the server is trying to retrieve some byte encoded object that isn't there.现在服务器正在尝试检索一些不存在的字节编码对象。

I have no idea if it's the same case for you, but I've had many instances where I had to put in image streams instead of URLs, which I've solved this way using this library :我不知道对你来说是否是同样的情况,但我有很多例子,我不得不放入图像流而不是 URL,我已经使用这个库以这种方式解决了这个问题:

Routes.php路由.php

/**
* Image handling routes.
* These make sure images actually are images instead of bytecode
*/
Route::get('organization/logo/{logo}', function($logo) {
    $file = Image::make(Storage::disk('logo-image')->get($logo));
    return $file->response();
});

View看法

<img src="{{ asset('organization/logo/' . $organization->logo_path) }}" alt="">

I might be completely off, but I recognize what's happening with your app and this took care of the issues when I implemented it.我可能完全不知道,但我知道你的应用程序发生了什么,这在我实现它时解决了问题。

Check your web server configuration.检查您的 Web 服务器配置。 It sounds like you have some type of redirect setup that redirects assets/images/* to just images/* .听起来您有某种类型的重定向设置,可以将assets/images/*重定向到images/*

As a simple test, open your "Network" tab and navigate your browser to http://samplelumena.local/assets/images/footer1.jpg .作为一个简单的测试,打开您的“网络”选项卡并将浏览器导航到http://samplelumena.local/assets/images/footer1.jpg I'm guessing the Network trace will show a 30x (301, 302, etc.) to http://samplelumena.local/images/footer1.jpg , followed by the 404 for that url.我猜网络跟踪将向http://samplelumena.local/images/footer1.jpg显示 30x(301、302 等),然后是该网址的 404。

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

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