How to render a PHP page into as ASP.NET MVC view?

Discussion about the open-source Phalanger [?] project.

How to render a PHP page into as ASP.NET MVC view?

Postby KendallB » March 2nd, 2012, 5:42 am

I need to set up ASP.NET MVC for our main web site, so that we can have our current index.php page handle the / URL when the PHP files doesn't exist on the web site. So I need to somehow render the PHP page in Phalanger and send all the output to the browser from an ASP.NET MVC controller action. So something like this:

Code: Select all
public ActionResult Index()
{
    return Content(RenderPHPPage('index.php'));
}


or something to that effect. Can someone show me the code I would use to execute the index.php script using Phalanger and capture the output so it can be sent to the browser through the Content() function in ASP.NET MVC?
- Kendall Bennett
KendallB
 
Posts: 340
Joined: January 7th, 2012, 8:36 pm

Re: How to render a PHP page into as ASP.NET MVC view?

Postby KendallB » March 7th, 2012, 4:17 pm

Any help with this question?
- Kendall Bennett
KendallB
 
Posts: 340
Joined: January 7th, 2012, 8:36 pm

Re: How to render a PHP page into as ASP.NET MVC view?

Postby Jakub Misek » March 7th, 2012, 5:07 pm

To render PHP script using Phalanger, you just redirect the response into your .NET Stream, and then do whatever you would like.

Code: Select all
var context = PHP.Core.ScriptContext.CurrentContext;
using (var request_context = PHP.Core.RequestContext.Initialize(
                 PHP.Core.ApplicationContext.Default,
                 HttpContext.Current))
{
    var output = new System.IO.MemoryStream();
    context.Output = new System.IO.StreamWriter(output);
    using (context.OutputStream = output)
    {
         context.Include("index.php", false);
    }
                //
    output.Position = 0;
    string content = new System.IO.StreamReader(output).ReadToEnd();
}


PHP.Core.RequestContext has to be initialized once during the request, and Disposed after you are done with PHP processing.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: How to render a PHP page into as ASP.NET MVC view?

Postby KendallB » March 8th, 2012, 6:57 pm

Ok thanks. I will have to figure out how to return from a MVC view without returning any content (or maybe if the output already has the content, I can just return Content() and it will work? I will also need to completely turn off all the views as well so it won't render the content in the master view, but I think that is easy enough to do.

Thanks for the help!
- Kendall Bennett
KendallB
 
Posts: 340
Joined: January 7th, 2012, 8:36 pm

Re: How to render a PHP page into as ASP.NET MVC view?

Postby KendallB » April 2nd, 2012, 2:03 am

Ok, I tried the above code but it is not working right now as I need to get the script from the script library, not from the disk. When I use the include() function it seems to be looking for the index.php file on disk, but I need it to look it up inside my script library. How do I include the script from the script library without expecting it to be on disk?
- Kendall Bennett
KendallB
 
Posts: 340
Joined: January 7th, 2012, 8:36 pm

Re: How to render a PHP page into as ASP.NET MVC view?

Postby KendallB » April 2nd, 2012, 2:28 am

Ok,I got it to work using the following code after I looked at how the request handler for real PHP pages is written. But I had to modify Phalanger to give me access to the RequestContext.GetCompiledScript() function which was marked internal. I changed it to public and now it works. Can you make that public in the official source code?

Code: Select all
public ActionResult Index()
{
    // Get the PHP source file we need to render for the index.php script
    var requestPath = Request.PhysicalPath + @"\index.php";
    var requestFile = new PhpSourceFile(new FullPath(HttpRuntime.AppDomainAppPath), new FullPath(requestPath));

    // Now render the page using Phalanger directly into the output stream
    using (var request_context = RequestContext.Initialize(ApplicationContext.Default, System.Web.HttpContext.Current)) {
        var script = request_context.GetCompiledScript(requestFile);
        request_context.IncludeScript(requestPath, script);
        return Content("");
    }
}
- Kendall Bennett
KendallB
 
Posts: 340
Joined: January 7th, 2012, 8:36 pm

Re: How to render a PHP page into as ASP.NET MVC view?

Postby Jakub Misek » April 2nd, 2012, 11:29 am

Actually there is public Include() method on 'request_context.ScriptContext'. 'IncludeScript' is intended for internal use only.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: How to render a PHP page into as ASP.NET MVC view?

Postby KendallB » April 2nd, 2012, 9:26 pm

Right, but the public one will fail if the actual script is not present. So I cloned the one used by the handler, which does things differently.

Maybe the other option is to fix the Include() function that is public such that it will work with a pre-compiled script from a script DLL, as clearly the current one does not?
- Kendall Bennett
KendallB
 
Posts: 340
Joined: January 7th, 2012, 8:36 pm

Re: How to render a PHP page into as ASP.NET MVC view?

Postby Jakub Misek » April 2nd, 2012, 9:31 pm

Public Include() should work in the same way. It does the same thing internally. Maybe the given path has to be relative?
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: How to render a PHP page into as ASP.NET MVC view?

Postby KendallB » April 2nd, 2012, 9:33 pm

Well I called it with just 'index.php'. Perhaps we need to include the full path then? I can try that.
- Kendall Bennett
KendallB
 
Posts: 340
Joined: January 7th, 2012, 8:36 pm

Next

Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 20 guests

User Control Panel

Login

Who is online

In total there are 20 users online :: 0 registered, 0 hidden and 20 guests (based on users active over the past 5 minutes)
Most users ever online was 511 on April 27th, 2024, 10:11 pm

Users browsing this forum: No registered users and 20 guests