Views is one of the best module of Drupal. It provide a way to map the data in the mysql database to the web interface. You can do that even without writing any SQL query, just some clicks. I do not want to cover all the aspet of Views, but only to show how to restrict the Veiws result based on Arguments.
Arguments are parts of URL, may be a node id in /node/xxxx, or user id in /user/xxxx. Views provide some build-in arguments: See Views arguments. You can restrict the views by Node type, User ID, Term ID, etc. But these build-in features can not meet all of our requirement.
So we can use PHP Code Validator.
For example, I want to show the view only in the node where a CCK field eqs 1. And only show in the content type blog.
$nid = $argument;
$node = node_load($nid);
return ($node->type == ‘blog’ && $node->field_Myfield_type[0]['value'] == 1);
Put the php code block in the views argument, then you will find the result.
对编程语言的选择的规则其实很简单,优先选用自己或者团队熟悉的语言,优先选择项目中用到开源产品稳定版本所用的语言。经常看到某 Java 程序员抱怨 PHP 的各种问题,或者 C++ 的程序员和 JAVA 的程序员的网上吵架,或者最近知名博客 dbanotes 的博主 Fenng 和知名技术社区 Javaeye 的创始人范凯在 Twitter 上关于 PHP 或者 ROR 技术选型的争论,其实编程语言无好坏,关键是使用的人。
为了提高开发效率,需要融合不同语言。回顾以前项目中用到的几种不同语言间通信的几种方式:
1. XML-RPC 可能是应用最广泛的方法
使用 HTTP 协议,通过向远程服务发送方法请求,获取处理结果。其中请求发送的内容为 XML 格式:
<?xml version="1.0"?>
<methodCall>
<methodName>examples.getStateName</methodName>
<params>
<param>
<value><i4>40</i4></value>
</param>
</params>
</methodCall>
和 XML-RPC 类型的还有 JSON-RPC ,顾名思义通信内容使用 JSON 格式。
发送 XML-RPC 或者 JSON-RPC 请求可以简单的用 HTTP 发送拼接的请求内容,比如 PHP 中可以用 Curl 发送请求,接收处理结果。
2. SOAP 也是使用 HTTP 协议进行通信
SOAP 请求的例子:
POST /InStock HTTP/1.1
Host: www.example.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: 299
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Header>
</soap:Header>
<soap:Body>
<m:GetStockPrice xmlns:m="http://www.example.org/stock">
<m:StockName>IBM</m:StockName>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
SOAP 和 XML-RPC 的主要区别在于可以通过调用远程 WSDL 文件获取远程服务的方法。而且扩展了对 HTTP HEADER 的使用。使用 .NET 可以轻松的创建一个 SOAP 服务以供调用。同 XML-RPC 一样,在 PHP 中可以用 CURL 直接发送 HTTP 请求。
3. Socket
用 Socket 进行通信可能是最古老的方法,也是最高效的方式。
以 PHP 为例发送 Socket 请求:
$s = @fsockopen('127.0.0.1', 1234);
fwrite($s, "addaiml\r\n");
$result= fgets($s, 10);
fclose($s);
可以用几百行 JAVA 或者其他语言轻松创建一个简单的 Socket 服务来接收 Socket 请求。这里值得提出的是 memcache 的协议设计堪称典范,以前项目的的 Socket 协议设计都参考了 memcahe 的方式。
4. Message Queue
通过消息队列来进行通信,可以用现有的 message queue 中间件产品比如支持异步处理的 gearman,或者更为强大的 Rabbit-MQ, Zero-MQ。
也可以在数据库中自己建立消息表,构建简单的生产者-消费者模式,服务端定时查询数据库中的未处理消息,处理完后对消息进行标定,请求端在表中插入新消息,并且定期查询消息的状态。
5. CLI
这可能是比较土的调用其他服务的方式,虽然只支持同步的方式,但是也很有效,比如获取服务器的CPU MEMORY 等指标可以用这种方式
以 PHP 为例,一切在命令行使用的命令都可以通过 exec() 函数来调用,并且获取处理结果。扩展思维,一切能在命令行人工执行的命令,都可以通过此方式来执行。
常见语言对于XML-RPC的支持库
Python: http://docs.python.org/library/xmlrpclib.html
C++: http://libiqxmlrpc.sourceforge.net/
Erlang: http://erlang.stacken.kth.se/contrib/xmlrpc-1.13.tgz
JAVA: http://ws.apache.org/xmlrpc/
.NET: http://www.xml-rpc.net/
PHP: http://phpxmlrpc.sourceforge.net/
Ruby: http://www.ruby-doc.org/stdlib/libdoc/xmlrpc/rdoc/index.html