Using phalanger as part of an MVC application view

To provide better compatibility between some user interface user who knows HTML and a backend that knows .NET, we are thinking of an architecture in which we use the MVC web application and use phalanger as a viewer.

Integrating phalanager as a view model looks pretty easy, except for one point. I am not sure how to pass the model to the script page. Any ideas how this can be achieved?

One idea would be to call a static .NET method from a php script, but it is a bit hacked. I would like to just pass the parameter to a script and the script to get it in a variable.

+3
source share
2 answers

.NET PHP ( Phalanger) . , - .NET PHP ( , PHP).

.NET- PHP, Phalanger, , PHP- .NET. , DLL #, DemoDataLayer :

public class Data {
  public List<Category> GetCategories() {
    var ret = new List<Category>();
    // Some code to load the data
    return ret;
  }
}

# Phalanger ( web.config) PHP, Phalanger, Data, PHP:

<?
  import namespace DemoDataLayer;
  $dl = new Data;
  $categories = $dl->GetCategories();
?>
<ul>
<? foreach($categories as $c) { ?>
    <li><a href="products.php?id=<? echo $c->ID ?>"><? echo $c->Name ?></a></li>
<? } ?>
</ul>

, DLL # bin classLibrary. import namespace, , Phalanger ( .NET), PhpClr:

<?xml version="1.0"?>
<configuration>
  <phpNet>
    <compiler>
      <set name="LanguageFeatures">
        <add value="PhpClr" />
      </set>
    </compiler>
    <classLibrary>
      <add assembly="DemoDataLayer" />
    </classLibrary>
  </phpNet>
</configuration>
+4

http://phpviewengine.codeplex.com/

CLR , PHP:

    object PhpSafeType(object o)
    {
        // PHP can handle bool, int, double, and long
        if ((o is int) || (o is double) || (o is long) || (o is bool))
        {
            return o;
        }
        // but PHP cannot handle float - convert them to double
        else if (o is float)
        {
            return (double) (float) o;
        }
        // Strings and byte arrays require special handling
        else if (o is string)
        {
            return new PhpString((string) o);
        }
        else if (o is byte[])
        {
            return new PhpBytes((byte[]) o);
        }
        // Convert .NET collections into PHP arrays
        else if (o is ICollection)
        {
            var ca = new PhpArray();
            if (o is IDictionary)
            {
                var dict = o as IDictionary;
                foreach(var key in dict.Keys)
                {
                    var val = PhpSafeType(dict[key]);
                    ca.SetArrayItem(PhpSafeType(key), val);
                }
            }
            else
            {
                foreach(var item in (ICollection) o)
                {
                    ca.Add(PhpSafeType(item));
                }
            }
            return ca;
        }
        // PHP types are obviously ok and can just move along
        if (o is DObject)
        {
            return o;
        }
        // Wrap all remaining CLR types so that PHP can handle tham
        return PHP.Core.Reflection.ClrObject.WrapRealObject(o);
    }

...

// Get setup
var sc = new ScriptContext.CurrentContext;
var clrObject = /* Some CLR object */
string code = /* PHP code that you want to execute */

// Pass your CLR object(s) into the PHP context
Operators.SetVariable(sc,null,"desiredPhpVariableName",PhpSafeType(clrObject));

// Execute your PHP (the PHP code will be able to see the CLR object)
var result =            return DynamicCode.Eval(
                code,
                false,
                sc,
                null,
                null,
                null,
                "default",
                1,1,
                -1,
                null
                );

. , .

var clrObject = new { Name = "Fred Smith" };
Operators.SetVariable(sc,null,"person",PhpSafeType(clrObject));

PHP:

echo $person->Name;

, , Fred Smith

+1

All Articles