在Ruby上进行PHP风格的Web开发:为此创建微框架 [英] PHP-style web development on Ruby: creating microframework for that

查看:63
本文介绍了在Ruby上进行PHP风格的Web开发:为此创建微框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP世界中,您只需创建index.php文件,放入一些内联代码和原始HTML,运行Apache即可.

In PHP world you can just create index.php file, put some inline code and raw HTML, run Apache and it just works.

有很多关于使用内联代码的不良做法的演讲.因此,请不要在这里讨论这个主题.

There are a lot af talks about bad practices of using inline code an so on. So we won't discuss this theme here, please.

如何以相同的方式启动我的Ruby应用程序?我想使用ERB进行代码混乱,所以它看起来像这样

How can I start my Ruby application same way? I want to use ERB for code messing, so it will look this way

# index.rb
<h1>Hello world!</h1>
<div>
  1 + 1 = <%= 1 + 1 %>
</div>

所以我的问题是:

  1. 是什么使PHP能够正常工作.
    AFAIU(nderstand)PHP对HTTP具有本机支持,因此我必须使用 Rack 来支持Ruby.
  2. 在微框架"上创建我的一些基本知识:使用应用程序/http服务器(Mongrel,nginx,http端口上的绑定以及所有此类工作),使用HTTP请求:会话,参数,GET/POST等(机架?),发送响应(ERB模板).
  1. What makes PHP just work.
    AFAIU(nderstand) There is native support for HTTP in PHP, so I have to use Rack to support it with Ruby
  2. Some basic knowledge for creating my on "microframework": working with application/http servers (Mongrel, nginx, binding on http port and all such kind of job), working with HTTP requests: sessions, params, GET/POST etc (Rack?), sending responses (ERB templating).

因此,我可以为Ruby:D制作自己的(在教育方面)微框架,以进行PHP风格的Web开发

So I can make my own (in educational puprposes) microframework for PHP-style web development with Ruby :D

UPD

我真正想做的是一个应用程序,它将仅获取请求url,仅运行该文件并返回HTML作为响应.另外,应允许将此应用程序绑定到某些端口上

What really I want to do is an application wich will just get request url, run just that file and return HTML as a response. Also this application should be allowed to be binded on some port

index.rb
info/about.rb
info/contacts.rb
products/product.rb

因此它应该解析url localhost/index.rb并运行index.rblocalhost/products/product.rb?product_id=10并运行products/product.rb并将product_id=10作为参数哈希传递.

so It should parse url localhost/index.rb and run index.rb, localhost/products/product.rb?product_id=10 and run products/product.rb and pass product_id=10 as a params hash.

UPD 2

我认为开始的好点是深入研究Camping微框架源:

I think good point to start is to dig into Camping microframework source:

https://github.com/camping/camping

大约5Kb的重量,所以我不要感到困惑

It is about 5Kb weight so I shouldn't be confused in it

推荐答案

可以使用Ruby编写CGI脚本,但是由于我们有更好的解决方案,因此通常不会这样做.

It is possible to write CGI scripts with Ruby but this is generally not done because we have better solutions.

每页一个文件不是一个非常有用的抽象,它只是PHP支持的文件. Ruby有更好的抽象Web应用程序的方法(例如Sinatra,Rails甚至只是Rack),因此我们更喜欢使用它们.

One file per page is not a very useful abstraction, it's just the one that PHP supports. Ruby has better ways to abstract a web application (like Sinatra, Rails, or even just Rack) so we prefer to use them.

将文件名放在url中是PHP设计的另一个不幸的副作用.这是实现性的揭示,并且是不必要的(您没有得到Ruby页面,您得到了HTML页面),因此我们选择也不这样做.

Putting the file name in the url is another unfortunate side effect of PHP's design. It is implementation revealing and unnecessary (you're not getting a Ruby page, you're getting an HTML page), so we choose not to do that either.

Ruby中的CGI和FCGI也比其他解决方案慢.这并不是因为他们的表现有一定的限制.这主要是因为使Ruby Web应用程序更快的努力已花费在Rack和Rails等更有用的领域.没有人真正使用CGI,因此没有人真正在意它的快速发展.如果您真的想走这条路,mod_ruby可使CGI脚本更快一些,但同样:有更好的方法.

CGI and FCGI in Ruby are also slower than the other solutions. This is not because of some limit on how performant they can be; it's mostly just because the effort to make Ruby web applications faster has been spent in more useful areas like Rack and Rails. No one really uses CGI so no one really cares to make it fast. mod_ruby makes CGI scripts somewhat faster if you really want to go this route, but again: there are better ways.

这篇关于在Ruby上进行PHP风格的Web开发:为此创建微框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆