Laravel 5.4包括js和css问题

时间:2017-07-02 17:57:57

标签: laravel

我在我的公共文件夹中添加了一个名为js的文件夹,并在script.js中添加。在视图中:

<script type="text/javascript" src="{!! asset('/js/script.js') !!}"></script>

并且所有工作都在本地工作但在服务器上我收到错误:

https://url/js/script.js 

怎么可能?

4 个答案:

答案 0 :(得分:1)

你应该使用{{}}而不是{!! !!}

<script type="text/javascript" src="{{ asset('js/script.js') }}"></script>

{!! !!}用于显示未转义数据

  

默认情况下,会自动发送Blade {{}}语句   PHP的htmlspecialchars用于防止XSS攻击。如果你不   希望您的数据被转义,您可以使用以下语法:

答案 1 :(得分:0)

  

并且所有工作都在本地工作但在服务器上我收到错误:

通过服务器,您指的是远程服务器(例如VPS)吗?还是PHP's built-in web server

您是否尝试过运行php artisan cache:clear

答案 2 :(得分:0)

  

第一种简单方法提供路径:Laravel 5.4文件夹中的asset文件结构resources文件夹所以假设您的文件在其中。 (resources/asset/)所以你可以使用如下例:

<script type="text/javascript" src="{{ URL::asset('js/jquery.js') }}"></script>

<link rel="stylesheet" href="{{ URL::asset('css/somestylesheet.css') }}" />
  

第二种方式您可以将路径传递给样式表。

{!! HTML::style('css/style.css') !!}
  

您只需将路径传递给javascript。

{!! HTML::script('js/script.js'); !!}
  • composer.json文件的require部分添加以下行,然后运行composer update "illuminate/html": "5.*"
  • 通过将以下值添加到providers数组中,在service provider中注册config/app.php

    'Illuminate\Html\HtmlServiceProvider'

  • 通过在facades数组中添加以下两行来注册aliases

    'Form'=> 'Illuminate\Html\FormFacade', 'HTML'=> 'Illuminate\Html\HtmlFacade'

  

第三种方式将assets放入public目录并使用以下内容:

<script type="text/javascript" src="{{ URL::asset('js/jquery.js') }}"></script>

<link rel="stylesheet" href="{{ URL::asset('css/somestylesheet.css') }}" />

或(使用URL::to()

<link rel="stylesheet" type="text/css" href="{{ URL::to('css/style.css') }}">

<script type="text/javascript" src="{{ URL::to('js/jquery.min.js') }}"></script>

答案 3 :(得分:0)

我认为您可以尝试使用此功能来解决httphttps的问题,因为您的脚本和css没有在https网址中运行:

首先,您可以在SchemalessUrlGenerator中创建App\Libraries文件:

<?php

namespace App\Libraries;

use Illuminate\Http\Request;
use Illuminate\Routing\RouteCollection;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Str;

class SchemalessUrlGenerator extends UrlGenerator
{
    public function __construct(RouteCollection $routes, Request $request)
    {
        parent::__construct($routes, $request);
    }

    public function to($path, $extra = [], $secure = null)
    {
        // First we will check if the URL is already a valid URL. If it is we will not
        // try to generate a new one but will simply return the URL as is, which is
        // convenient since developers do not always have to check if it's valid.
        if ($this->isValidUrl($path)) {
            return $path;
        }
        $scheme = $this->getScheme($secure);
        $extra = $this->formatParameters($extra);
        $tail = implode('/', array_map(
            'rawurlencode', (array) $extra)
        );
        // Once we have the scheme we will compile the "tail" by collapsing the values
        // into a single string delimited by slashes. This just makes it convenient
        // for passing the array of parameters to this URL as a list of segments.
        $root = $this->getRootUrl($scheme);
        if (($queryPosition = strpos($path, '?')) !== false) {
            $query = mb_substr($path, $queryPosition);
            $path = mb_substr($path, 0, $queryPosition);
        } else {
            $query = '';
        }
        return '//' . $this->trimUrl($root, $path, $tail).$query;
    }

    /**
     * {@inheritdoc}
     */
    protected function getScheme($secure)
    {
        // Don't be smart Laravel... ask the browser?!?!
        // negotiate the schema to be the same as how page was served
        return '//';
    }
}

然后,您可以在SchemalessUrlGenerator方法

中的App\Providers\AppServiceProvider中添加register相关代码
$routes = $this->app['router']->getRoutes();

// // Replace UrlGenerator with SchemalessUrlGenerator that will serve content using "//" instead
// // of "http" or "https"
$schemalessUrlGenerator = new SchemalessUrlGenerator($routes, $this->app->make('request'));
$this->app->instance('url', $schemalessUrlGenerator);

希望对你有所帮助!