用PHP创建一个RESTful API和网站 [英] Creating a RESTful API and website with PHP

查看:127
本文介绍了用PHP创建一个RESTful API和网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我之前写过的PHP应用程序,我想添加一个RESTful API。我还想扩展这个站点,使其更像是一个Rails应用程序,用于调用系统中的项目的URL。



有没有办法以Railsy方式在PHP中调用项目而不创建各种文件夹和索引页面?如何在不使用GET查询标记的情况下在PHP中调用信息?

解决方案

如果你有某种形式的mod_rewrite,这很容易与.htaccess文件。



如果您有这样的事情:

  RewriteEngine On 
RewriteCond%{REQUEST_FILENAME}!-d
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^(。*)$ index.php?url = $ 1 [ QSA,L]

它会检查您尝试访问的文件是否已经存在。 (假设您有一个hello.html文件,您仍需要人员通过 http://yoursite.com/hello .html

然后,如果文件尚不存在,它会将您的index.php文件加载到url变量中存储的其余部分。



这意味着你可以调用这样的 http://yoursite.com/pages/edit/24 ,它会在url变量中加载/ pages / edit / 24中的index.php。



这应该让你开始,你不会需要各种文件夹和索引页面,只需要mod_rewrite,.htaccess文件和一个index.php文件,它将用于加载任何你需要的文件。


I've got a PHP application I wrote earlier that I'd like to add a RESTful API to. I'd also like to expand the site to behave more like a Rails application in terms of the URLs you call to get the items in the system.

Is there any way to call items in PHP in a Railsy way without creating all kinds of folders and index pages? How can I call information in PHP without using a GET query tag?

解决方案

If you have some form of mod_rewrite going you can do this quite easily with a .htaccess file.

If you have something like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

It will check that the file you are trying to access doesn't exist already. (Say you had a file hello.html that you still needed people to access via http://yoursite.com/hello.html)

Then if the file doesn't already exist it will load your index.php file with the rest of the URL stored in the url variable.

This means you can call something like this http://yoursite.com/pages/edit/24 and it will load index.php with /pages/edit/24 inside the url variable.

That should get you started and you won't need all kinds of folders and index pages, just mod_rewrite, .htaccess file and an index.php that will be used to load whatever you need.

这篇关于用PHP创建一个RESTful API和网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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