【问题标题】:Laravel 5 stylesheets and scripts not loading for non-base routesLaravel 5 样式表和脚本未为非基本路由加载
【发布时间】:2015-08-29 14:28:00
【问题描述】:

我无法在我的 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>

由于某种原因,当加载的路由是 example.com/about/something 时,Laravel 认为基本 URL 是 example.com/about。我也尝试过,但它仍然路由到 example.com/about/images/image.jpg 而不是 example.com/images/image.jpg。任何建议将不胜感激。谢谢!

【问题讨论】:

    标签: laravel model-view-controller laravel-5


    【解决方案1】:

    您正在使用相对路径并且需要绝对路径。看下面的例子,我唯一添加的是前面的/

    {!! 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

    【讨论】:

    • 谢谢!调试时肯定错过了。
    猜你喜欢
    • 2013-02-20
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 2021-02-02
    • 1970-01-01
    相关资源
    最近更新 更多