3

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.

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

Inside my index file head:

<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">

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\"     
  }]
}
";
?>

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?

Trob Frank
  • 363
  • 3
  • 11
  • 1
    This should work just fine. Are you getting valid JSON from manifest.php? You could look at building out an array and using `json_encode` - might be easier to catch issues. What exactly are you stuck on? – ficuscr Aug 22 '17 at 19:14
  • I am getting valid JSON, but with missing values (where the php should be). Going to use json_encode and see if results vary – Trob Frank Aug 22 '17 at 19:23
  • Well, where is `$SiteName` assigned a value? Your code "works" as is. – ficuscr Aug 22 '17 at 19:30

1 Answers1

3

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);
ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • The variables are just strings inside of the index.php file, before the manifest.php is called. What you've posted here works, however when I move the `$siteName` to my index file it is registered as `null` – Trob Frank Aug 22 '17 at 19:43
  • 1
    Scope. Luckily [register globals](http://php.net/manual/en/security.globals.php) is a thing of the past... May you never know the pain of variable collisions! Take a look at how you [define a constant](http://php.net/manual/en/function.define.php) or... we'll explore options once you have better understanding of scope. – ficuscr Aug 22 '17 at 19:47