我在Laravel 5应用中为非基础路线链接样式表,脚本和图像时遇到问题。当我访问example.com/about时,脚本和样式表工作正常,但是如果我访问example.com/about/something,则脚本和样式表链接到example.com/about/css/style.css而不是example.com/css /style.css
我的代码如下:
Routes.php:
Route::get('about/{slug?}', ['as' => 'officerProfile', 'uses' => 'PagesController@about']);
PagesController.php:
public function about($slug = 'president')
{
$officers = Officer::all();
$currentOfficer = Officer::where('slug', $slug)->first();
return view("pages.about", ['title' => $this->makeTitle('Learn About Us')])->with('officers', $officers)->with('currentOfficer', $currentOfficer);
}
layout.blade.php:
{!! HTML::script('https://code.jquery.com/jquery-2.1.4.min.js') !!}
{!! HTML::script('js/skel.min.js') !!}
{!! HTML::script('js/skel-layers.min.js') !!}
{!! HTML::script('js/init.js') !!}
{!! HTML::script('js/scripts.js') !!}
<noscript>
{!! HTML::style('css/skel.css') !!}
{!! HTML::style('css/style.css') !!}
{!! HTML::style('css/style-desktop.css') !!}
</noscript>
由于某些原因,Laravel认为当加载的路由为example.com/about/something时,基本URL为example.com/about。我也尝试过,但是它仍然路由到example.com/about/images/image.jpg而不是example.com/images/image.jpg。任何建议将不胜感激。谢谢!
您正在使用相对路径,并且需要绝对路径。参见下面的示例,我添加的唯一内容是/
{!! HTML::script('https://code.jquery.com/jquery-2.1.4.min.js') !!}
{!! HTML::script('/js/skel.min.js') !!}
{!! HTML::script('/js/skel-layers.min.js') !!}
{!! HTML::script('/js/init.js') !!}
{!! HTML::script('/js/scripts.js') !!}
<noscript>
{!! HTML::style('/css/skel.css') !!}
{!! HTML::style('/css/style.css') !!}
{!! HTML::style('/css/style-desktop.css') !!}
</noscript>
相对路径将加载与当前路径相关的文件。例如,http://example.com/about/us
您可以从页面中相对加载images/logo.png
,从而导致对的实际请求http://example.com/about/images/logo.png
。
绝对路径始终来自主机名:与上述示例相同,但您将加载/images/logo.png
该主机名http://example.com/images/logo.png
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句