search

Home  >  Q&A  >  body text

How to reference css and js files in laravel?

The location of static files such as css and js in laravel is in the root directory of the website, public, but I want to put the files in other directories, such as: resoures/assets, added in the entry file public/index.php The following code:

$app = require_once __DIR__.'/../bootstrap/app.php';
/*
 * 设置静态文件目录
 */
 $app->bind('path.public', function() {
    $path_public = base_path() . DIRECTORY_SEPARATOR . env('PATH_PUBLIC', 'public');
    return $path_public;
});

I set the environment variables in the .env file:

PATH_PUBLIC=resources/assets

The result printed using the helper function public_path() has indeed changed, to: website root directory/resources/assets. Next, I created a new styles directory in this directory and quoted it in the view:

    <link rel="stylesheet" href="{{ asset('styles/login.css') }}">
    <link rel="stylesheet" href="<?php echo asset('styles/login.css') ?>">

After parsing, the source code of the web page looks like this:

    <link rel="stylesheet" href="{{ asset('styles/login.css') }}">
    <link rel="stylesheet" href="http://www.cx.com/styles/login.css">

Why is {{}} not parsed? Want to add a smarty extension? Why has the static file link not changed to /resources/assets?
Is it so difficult to introduce a css? . . I want to jump into the Yellow River. . . Ask for answers.

迷茫迷茫2838 days ago683

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 16:53:27

    Just write an absolute path

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 16:53:27

    Thank you for the invitation. You can also use an absolute path. You can define a constant to define the path address of what environment. When using a relative path, what is the location of the root directory for opening the server configuration? You can use a relative path.

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 16:53:27

    For example, if your css file is placed under public/css, then the path to /css/style.css on the page
    The introduction of js is the same as above!

    The prerequisite for this method is that you need to set the public directory as the site root directory!

    reply
    0
  • Cancelreply