在我的Laravel 5应用程序中,我很难将我的样式表、脚本和图像链接到一个非基本路线。当我访问example.com/about脚本和样式表时,如果我访问example.com/about/东西,脚本和样式表链接到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时,基本URL是example.com/about。我也尝试过了,但是它仍然路由到example.com/about/image.jpg,而不是example.com/image.jpg。如有任何建议,将不胜感激。谢谢!
发布于 2015-06-13 11:05:01
您使用的是相对路径,您需要绝对路径。请参阅下面的示例,我添加的唯一内容是前面的/
{!! 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
https://stackoverflow.com/questions/30822367
复制相似问题