Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assets problem #12

Open
kmuharam opened this issue Dec 10, 2017 · 2 comments
Open

Assets problem #12

kmuharam opened this issue Dec 10, 2017 · 2 comments

Comments

@kmuharam
Copy link

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/.....

@keevitaja
Copy link
Owner

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">

@lscortesc
Copy link
Contributor

lscortesc commented Jun 22, 2018

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants