How to reference css and js files in laravel?
迷茫
迷茫 2017-05-16 16:51:27
0
3
621

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.

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
我想大声告诉你

Just write an absolute path

大家讲道理

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.

滿天的星座

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!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!