使用phalanger作为MVC应用程序的视图部分

时间:2011-05-26 09:13:28

标签: asp.net-mvc phalanger

为了让知道HTML的UI用户和知道.NET的后端人员之间更好地协作,我们正在考虑使用MVC Web应用程序并使用phalanger作为视图引擎的架构。

将phalanager作为视图模型进行集成看起来相当容易。我不确定如何将模型传递给页面脚本。任何想法如何实现这一目标?

一个想法是从php脚本调用静态.NET方法,但感觉有点hacky。我希望能够将参数传递给脚本并让脚本能够在变量中获取它。

2 个答案:

答案 0 :(得分:4)

在后端使用.NET,在前端使用PHP(使用Phalanger)看起来很不错。我认为最好的选择是在.NET中实现模型并使用PHP来实现控制器和视图(直接或使用一些PHP框架)。

从使用Phalanger编译的PHP调用.NET模型非常简单,因为PHP代码可以访问.NET对象。假设您有一个包含名称空间DemoDataLayer的C#DLL,其类型如下:

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

然后你可以从Phalanger网站引用C#库(使用web.config)并使用Phalanger提供的PHP扩展来使用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>

要配置引用,您需要将C#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>

答案 1 :(得分:1)

查看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