0

Can anyone please guide me through to implement PHP as template for NodeJS application. I am planning to use : PHP to load only the front-end html template (security purpose) ExpressJs as my backend.

I got struck in implementing the same here i am attaching the code below :

var phpnode = require("php-node");
app.engine('php', phpnode);
app.set('view engine', 'php');

and I am trying to load template like this :

router.get('/index.php', function(req, res, next) {
    res.render('index.php');
});

1 Answer 1

1

Minimal example:

File structure:

  1. ./test.js - our nodejs file
  2. ./php/ - our view folder with php files
  3. ./php/phpinfo.php - our test php file

./test.js:

var express = require('express'),
    app = express(),
    //make sure that `bin` location is right for you system
    phpnode = require('php-node')({bin:"/usr/local/bin/php"});

app.set('views', __dirname+'/php/');
app.engine('php', phpnode);
app.set('view engine', 'php');

app.all('/phpinfo.php', function(req, res) {
   res.render('phpinfo');
})

var port = process.env.PORT || 8081;
app.listen(port, function() {
  console.log("Listening on " + port);
})

./php/phpinfo.php

<?php phpinfo(); ?>

The open http://127.0.0.1:8081/phpinfo.php in your browser, you should see phpinfo text output (not a HTML, plain text, because php we ran in a CLI mode, exactly like if you ran it in terminal)

Tell me if you need it not in a CLI mode, I'll try to find out how to switch it..

Sign up to request clarification or add additional context in comments.

5 Comments

I have tried the above settings Failed to lookup view "error" in views directory "/node_app/firstApp/templates/"
I got the above error I have all the template in templates directory
Ok, give me a minute, I'll write for you minimal sample
Ok, I think i am missing something in the settings.
Thanks dude it worked, I missed the path for php-node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.