keevitaja/linguist

Assets problem

Opened this issue · 2 comments

Hello,

Using Laravel 5.5, and Linguist ^2.1, when using asset to link a style sheet or a javascript file I get 404 and the URL becomes localhost:8000/ar/css/.....

Indeed there is a problem... i do not use the asset myself and because of this i haven't come face to face with this bug.

i'll look into this asap to find a fix.

until then, a quick fix would be a url rewrite or just do not use asset.

<script src="/css/app.js">

I've solved this problem creating another function helper (If I can, then I will add a PR)

Steps I've follow:

  • Create a file in app folder called helpers.php
  • Add to composer.json in autoload node:
"autoload": {
        "files": [
            "app/helpers.php"
        ],
}
  • Add follow code to app/helpers.php
if (! function_exists('linguist_asset')) {
    /**
     * Generate a linguist asset path for the application.
     * @param $path
     * @param null $secure
     * @return string
     */
    function linguist_asset($path, $secure = null)
    {
        $urlGenerator = app('url');

        if ($urlGenerator->isValidUrl($path)) {
            return $path;
        }

        $root = $urlGenerator->formatRoot(
            $urlGenerator->formatScheme($secure)
        );

        $urlSegments = explode('/', $root);

        if (in_array(end($urlSegments), config('linguist.enabled'))) {
            $root = str_replace('/' . end($urlSegments), '', $root);
        }

        $index = 'index.php';

        return (
            str_contains($root, $index) ?
                str_replace('/'.$index, '', $root) : $root
            ).'/'.trim($path, '/');
    }
}

if (! function_exists('secure_linguist_asset')) {
    /**
     * Generate a linguist asset path for the application.
     *
     * @param  string  $path
     * @return string
     */
    function secure_linguist_asset($path)
    {
        return linguist_asset($path, true);
    }
}
  • Run composer dumpautoload -o in terminal to load the file

Note: Always publish the config with php artisan vendor:publish --provider="Keevitaja\Linguist\LinguistServiceProvider"

How to use?

Examples:

<link rel="stylesheet" href="{{ linguist_asset('css/style.css') }}">
<script type="text/javascript" src="{{ linguist_asset('js/my_js.js') }}"></script>

With HTTPS:

<link rel="stylesheet" href="{{ secure_linguist_asset('css/style.css') }}">
<script type="text/javascript" src="{{ secure_linguist_asset('js/my_js.js') }}"></script>