The static site generator that is perfectly grafted onto THINKPHP 5.0/5.1 can customize the generation rules, support dynamic parameters, and support parameter range settings. It is a solution to build a static station without changing the original development process. I'll do some performance testing when I have time. If you have any other questions during use, please leave a message. build static site, build html file for THINKPHP framework
This category is suitable for both TP5.0 and 5.1 versions. Because TP5.1 has more changes than 5.0, this project has been adapted to the version, so please feel free to use it.
public/dist
directory and press F5 to refresh the static page effect. You can also use the original TP path or routing to view the template effect. composer require jkbuildhtml/jkbuildhtml
dist_rules.php
file in the application directory. See the static rules below. // 静态站放置路径:
'dist_path' => 'public/',
// 静态页存放文件夹名 一般放置在public下;静态站点直接指向这个目录即可:
'dist_dir_name' => 'dist',
// 生成的静态页子页的存放目录,即匹配规则中没有@符号的页面的存放目录,注意例中路径中的'dist/site-pages'会进行目录匹配作为替换./或../的依据,所以这个名称在项目文件夹名中最好唯一:
'dist_sub_dir' => 'site-pages',
// 要生成静态页的模块名:
'dist_module_name' => 'index',
// 静态页文件名字中的参数分隔符:
'dist_file_dot' => '_',
// 静态资源路径替换 静态站点根目录下会替换成 `./` 其他会替换成 `../`
'dist_src_match' => '/public/static/',
$builder = new JKBuildHtmlBuilder()
Place a statement in any controller to generate all static pages in batches. All you need to do is put it somewhere in the background. The page display is displayed line by line by flushing. If you want to use ajax to code it yourself, of course you can.
$builder->buildAll();
It can also be generated on a single page. Generally, a生一个页面
button is added after each row of data on the list page:
$builder->buildOne($path, ['id' => 5]);
It should be noted that the paths generated by a single page are generally controller and method names, which must be declared in the static generation rules, otherwise an error will be prompted.
You can also encapsulate the fetch method of the TP controller, so that it can be generated while developing.
protected function fetchHtml()
{
$builder = new JKBuildHtmlBuilder();
$builder->buildFromFetch( $html = $this->fetch(), input('get.') );
return $html;
}
dist_dir_name
configuration folder. Static pages will access these resources. If they are placed outside this folder, they will not be accessible unless the site directory is not this directory.Originally, TP resources were placed anywhere under public, but with statically generated classes, you have to follow the rules. The following are suggestions:
dist_rules.php
description:Key-value pair description:
Original TP template file a link path:
<?php
// +----------------------------------------------------------------------
// | 生成静态页的规则文件
// +----------------------------------------------------------------------
return [
// 这个是首页 带@的会生成在dist目录下,否则生成在子文件夹里;生成的html文件不带@
'@index' => 'index/index',
'@news' => 'news/index',
// 这个是带db的,表示要查询article表的id列,循环生成静态页
'news_:id' => ['news/find', 'article'],
// 这个是带自定义方法的,表示要执行getjobis方法返回id为键的二维数组,循环生成静态页
'job_:id' => ['jobs/find', 'func:getjobids'],
// 这个是请求tp的模块/控制器/方法,返回一个二维数组
'job_:id_:code' => ['index/index', 'func:dist/index/test'],
];
dist/site-pages
directory. The value can be a "request path" in the form of控制器/方法
. The custom module name will be automatically added when requesting. If a route is defined, the route will be written.
The value can also be an array. The first one is the request path, which will pass the parameter request; the second one is the name of the db, that is, all the values in the column where the parameter field is located. The system will generate pages in batches based on the parameters: such as 'news_:id' => ['news/find', 'article'], is to query the id column in the article table.
If you want to add db query conditions, then put the third value in for example id < 100
This will be passed into the where condition of db and needs to comply with tp query syntax, which becomes 'news_:id' => ['news/find', 'article', ['id' => ['<',100]]],
or ..."id < 100"]
If you want to customize the function that generates the ID, you can set the second parameter to a global method, which can be placed in common.php (the function name does not need to include func:
:), or written in any controller: 'func:admin/index/getJobIds'
or 'func:getjobids'
If the func type is used, the return value must be a two-dimensional array with parameters as keys. Such as: ['id' => [2,3,4,5]]
The func type can have a third value, which is passed in as a parameter of func
The static generation controller will directly generate the abnormal page into the html file and will not stop the generation.
Leng Feng Cui [email protected]
Fully complies with the 996ICU protocol and is perfectly open source