Home >PHP Framework >ThinkPHP >How to use CSS files in different directories in ThinkPHP5
ThinkPHP is an open source PHP framework and one of the more popular frameworks at present. It can be used to quickly build efficient web application systems. However, when developing with ThinkPHP, we sometimes need to put CSS files in different directories. So how to use CSS files in different directories in ThinkPHP5?
1. Define the static file path in the configuration file
In ThinkPHP5, you can find the following definition in the configuration file config.php:
'view_replace_str' => [ '__PUBLIC__'=>'/public', '__STATIC__'=>'/static', '__ROOT__' => '', ],
By modifying the view_replace_str array The values in define static file paths in different directories. Among them, __PUBLIC__ defines the path to the public directory, which is a default public directory where static resource files such as CSS and JS can be placed. __STATIC__ can define any static file directory, while ROOT defines the project root path.
After defining the static file path, we can use the path alias in the template file to call it, as shown below:
<link rel="stylesheet" type="text/css" href="__STATIC__/css/style.css">
2. Define the static file path in the controller
In the controller, you can also define a static file path. The specific implementation method is to define an alias in the initialization method of the controller. The code is as follows:
public function _initialize() { parent::_initialize(); $this->assign('public', '/public'); $this->assign('custom', '/custom'); // 自定义目录 }
PUBLIC is defined here and CUSTOM two aliases, and pass the corresponding path to the front-end view. When calling static files in templates, you can use these aliases, for example:
<link rel="stylesheet" type="text/css" href="{$public}/css/bootstrap.css"> <link rel="stylesheet" type="text/css" href="{$custom}/css/style.css">
If you need to define different static file paths in different controllers, you can define them separately in each controller.
3. Use dynamic methods for path configuration
In addition to defining paths in configuration files and controllers, you can also use dynamic methods for path configuration in template files. The specific method is to use the asset() method provided by ThinkPHP in the template and pass in the relative path, for example:
<link rel="stylesheet" type="text/css" href="{:asset('css/style.css')}">
In this way, the template file will automatically generate the corresponding static file path according to the configuration. Of course, this method is only suitable for situations where the front-end developer is invisible to the controller and configuration files.
Summary
The above is the method of using CSS files in different directories in ThinkPHP5. Whether in the configuration file, controller or dynamic mode, the definition of path alias can be achieved. Let us manage static resource files more conveniently.
The above is the detailed content of How to use CSS files in different directories in ThinkPHP5. For more information, please follow other related articles on the PHP Chinese website!