Stefano Maglione
Stefano Maglione

Reputation: 4160

Laravel 5.4 include js and css issue

I have added in my public folder one folder called js and inside a script.js. In the view:

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

and all is working in local but on the server I receive a get error:

https://url/js/script.js 

How is it possible?

Upvotes: 0

Views: 958

Answers (4)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21691

I think you can try this for solve issue for http and https because your script and css no run in https url:

First you can create SchemalessUrlGenerator file in 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 '//';
    }
}

Then you can add SchemalessUrlGenerator related code in App\Providers\AppServiceProvider in register method

$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);

Hope this help for you!

Upvotes: 0

1st Simple Way To give the path: As Per Laravel 5.4 File Structure asset folder inside the resources folder So Suppose Your file inside that. ( resources/asset/ ) So You Can Use Like Below Example:

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

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

2nd Way You can just pass the path to the style sheet .

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

You can just pass the path to the javascript.

{!! HTML::script('js/script.js'); !!}
  • Add the following lines in the require section of composer.json file and run composer update "illuminate/html": "5.*"
  • Register the service provider in config/app.php by adding the following value into the providers array:

    'Illuminate\Html\HtmlServiceProvider'

  • Register facades by adding these two lines in the aliases array:

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

3rd Way Place your assets in public directory and use the following:

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

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

OR ( Use 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>

Upvotes: 1

jpcaparas
jpcaparas

Reputation: 181

and all is working in local but on the server I receive a get error:

By server, are you referring to a remote server (e.g. VPS)? or PHP's built-in web server?

Have you tried running php artisan cache:clear as well?

Upvotes: 0

Mohammad Fanni
Mohammad Fanni

Reputation: 4183

you should use {{ }} instead of {!! !!}

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

{!! !!} used for Displaying Unescaped Data

By default, Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Upvotes: 1

Related Questions