在Rails中服务PHP(Service PHP in Rails)

编程入门 行业动态 更新时间:2024-10-24 09:19:17
Rails中服务PHP(Service PHP in Rails)

我有一个小的PHP服务,由AJAX在JavaScript文件中调用:

$.ajax({ type: "GET", url: "getDate.php", dataType:"json", data :{ fromDate:fromDate, toDate:toDate }, success: function(data) { ...... } });

该服务包含:

$fromDate = $_GET['fromDate']; $toDate = $_GET['toDate']; $fromDate=date_create($fromDate); $fromdate = date_format($fromDate,"Y-m-d")."T".date_format($fromDate,"H:i:s")."Z"; $fromdate = urlencode($fromdate); $toDate=date_create($toDate); $todate = date_format($toDate,"Y-m-d")."T23:00:00Z"; $todate = urlencode($todate); $url = "http://194.209.53.19:8086/query?db=Bellevue&q=select+*+from+measures+where+time%3E%3D%27".$fromdate."%27+and+time%3C%3D%27".$todate."%27"; $data = file_get_contents($url, false); echo $data;

我需要在我的Rails应用程序中使用它。 我想知道我是否可以将.php文件放在Rails文件夹中,然后简单地调用它。 或者,如果有办法在Rails中执行类似的服务?

I have a small PHP service that is being called in a JavaScript file by AJAX :

$.ajax({ type: "GET", url: "getDate.php", dataType:"json", data :{ fromDate:fromDate, toDate:toDate }, success: function(data) { ...... } });

This service contains :

$fromDate = $_GET['fromDate']; $toDate = $_GET['toDate']; $fromDate=date_create($fromDate); $fromdate = date_format($fromDate,"Y-m-d")."T".date_format($fromDate,"H:i:s")."Z"; $fromdate = urlencode($fromdate); $toDate=date_create($toDate); $todate = date_format($toDate,"Y-m-d")."T23:00:00Z"; $todate = urlencode($todate); $url = "http://194.209.53.19:8086/query?db=Bellevue&q=select+*+from+measures+where+time%3E%3D%27".$fromdate."%27+and+time%3C%3D%27".$todate."%27"; $data = file_get_contents($url, false); echo $data;

I need to use this in my Rails application. I was wondering if I could put the .php file in a Rails folder, and simply call it. Or if there's a way to do a similar service in Rails?

最满意答案

当然,你可以在Rails中做到这一点。 首先,您需要为您的请求定义路线。 在routes.rb文件中写下以下内容,位于config / routes.rb:

get "/getDate" => "generals#get_date"

这意味着:每当有/getDate的GET请求时,转到控制器GeneralController ,并调用其名为get_date函数。 如果你写了users#get_date ,Rails会尝试在UsersController找到get_date方法。

通过以下命令生成您选择的控制器:

rails generate controller General

它将在app / controller和其他文件中生成一个名为general_controller.rb的文件。

现在,您需要在那里定义所有逻辑。 其他的事情:在PHP中,您使用$_GET['formDate']获取发送的数据,但在Rails中,您将通过执行params[:formDate]来完成相同的功能。

def get_date form_date = params[:formDate] # and now here you can write all your logic, and can finally return the response respond_to do |format| format.json { render json: data_to_be_returned } end end

编辑:

对于PHP中的date_create或date_format函数,您可以在此处和此处查找它们的等价物。

Sure, you can do this in Rails. First, you need to define the route for your request. Write the following in your routes.rb file, located at config/routes.rb:

get "/getDate" => "generals#get_date"

What that means is: whenever there is a GET request for /getDate, go to controller GeneralController, and invoke its function named get_date. If you wrote users#get_date, Rails would try to find the get_date method in UsersController.

Generate the controller of your choice by following command:

rails generate controller General

It will generate a file named general_controller.rb in app/controller among other files.

Now, you need to define all your logic there. Other thing: in PHP, you get the sent data using $_GET['formDate'], but in Rails, you will accomplish the same functionality by doing params[:formDate].

def get_date form_date = params[:formDate] # and now here you can write all your logic, and can finally return the response respond_to do |format| format.json { render json: data_to_be_returned } end end

Edit:

For date_create or date_format functions in PHP, you can look up their equivalents at here and here.

更多推荐

本文发布于:2023-04-27 16:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rails   PHP   Service

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!