如何使用php填充manifest.json文件? [英] How can I use php to populate a manifest.json file?

查看:142
本文介绍了如何使用php填充manifest.json文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个manifest.json文件,当将其放置在wesite的根目录中时可以正确加载.与其将其作为静态文件,不如从站点内部使用php变量来填充清单中的值.

I have a manifest.json file that loads correctly when placed in the root directory of my wesite. Instead of it being a static file, I would like to use php variables from within my site to populate the values inside of the manifest.

我在规范中找不到有关此的任何信息,而我我不确定是否有可能.

I can't find any information on this in the spec, and I'm not sure if it's possible.

我尝试将清单的名称切换为manifest.php,并在其中使用了header('Content-Type: application/json').

I've tried switching the name of my manifest to manifest.php and used header('Content-Type: application/json') within it.

在我的索引文件头内:

<script src="<?php echo $SITE_URL;?>/main.js"></script>
<script src="<?php echo $SITE_URL;?>/sw.js"></script>
<link rel="manifest" href="<?php echo $SITE_URL;?>/manifest.php">

在我的manifest.php中:

Inside my manifest.php:

<?php header('Content-Type: application/json');
echo "
{
  \"name\": \"$SiteName\",
  \"gcm_user_visible_only\": true,
  \"short_name\": \"$Name\",
  \"description\": \"$PageDescription.\",
  \"start_url\": \"/index.php\",
  \"display\": \"standalone\",
  \"orientation\": \"portrait\",
  \"background_color\": \"$darkblue\",
  \"theme_color\": \"#f0f0f0\",
  \"icons\": [{
    \"src\": \"logo-load.png\",
    \"sizes\": \"96x96 128x128 144x144\",
    \"type\": \"image/png\"
  },{
    \"src\": \"logo-icon.png\",
    \"sizes\": \"48x48 72x72\",
    \"type\": \"image/png\"     
  }]
}
";
?>

变量$SiteName, $Name, $PageDescription, $darkblue, etc都在加载manifest.php之前在我的文档头中定义.

The variables $SiteName, $Name, $PageDescription, $darkblue, etc are all defined within my document head before the manifest.php is loaded.

我正在尝试的可能吗?

Is what I'm trying possible?

推荐答案

主要问题是这些常量从何处提取?有时人们在服务器主机级别上设置它们并导入它们(环境变量).另一个选择(对您正在做的事情有些模糊)是解析INI文件.

Main question I have is where are these constants pulled from? Sometimes people set them at the server host level and import them (environment variables). Another option (sort of blurs with what you are doing) is to parse an INI file.

我会做类似以下的事情:

I would do something like the following:

<?php

$siteName = 'foo';
$name = 'bar';
$pageDescription = 'baz';

$manifest = [
    "name" => $siteName,
    "gcm_user_visible_only" => true,
    "short_name" => $name,
    "description" => $pageDescription,
    "start_url" => "/index.php",
    "display" => "standalone",
    "orientation" => "portrait",
    "background_color" => $darkblue,
    "theme_color" => "#f0f0f0",
    "icons" => [
        "src" => "logo-load.png",
        "sizes"=> "96x96 128x128 144x144",
        "type" => "image/png"
    ],
    "src" => "logo-icon.png",
    "sizes" => "48x48 72x72",
    "type" => "image/png"
];

header('Content-Type: application/json');
echo json_encode($manifest);

这篇关于如何使用php填充manifest.json文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆