How to render a PHP page into as ASP.NET MVC view?
12 posts
• Page 1 of 2 • 1, 2
How to render a PHP page into as ASP.NET MVC view?
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:
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?
- 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?
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?
To render PHP script using Phalanger, you just redirect the response into your .NET Stream, and then do whatever you would like.
PHP.Core.RequestContext has to be initialized once during the request, and Disposed after you are done with PHP processing.
- 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. | @misekjakub │ jakub@devsense.com │
-
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?
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!
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?
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?
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?
Actually there is public Include() method on 'request_context.ScriptContext'. 'IncludeScript' is intended for internal use only.
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
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?
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?
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?
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. | @misekjakub │ jakub@devsense.com │
-
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?
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
12 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 20 guests