一个“preFIX”控制器添加到URL中的codeIgniter [英] Add a 'prefix' controller to url in CodeIgniter

查看:149
本文介绍了一个“preFIX”控制器添加到URL中的codeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有哪些可以通过 HTTP访问codeIgniter运行的API:// mysite的。 COM /控制器/方法/变量。不过,我想将URL转换,加入一个API$ P $控制器之前PFIX,使URL看起来像的 http://mysite.com/api/controller/method/variable

I currently have an api running on CodeIgniter which can be accessed via http://mysite.com/controller/method/variable. However, I want to convert the url, adding an 'api' prefix before the controller, leaving the url to look like http://mysite.com/api/controller/method/variable.

我presumed,这是路由的事,所以我加了两条新航线的routes.php文件文件:

I presumed that this is to do with routing, so I added two new routes to the routes.php file:

$route['api/users/auth'] = 'users/auth';
$route['api/users/create'] = 'users/create';

这现在增加了APIpreFIX到以下网址。

This now adds the 'api' prefix to following urls.

http://mysite.com/api/users/auth
http://mysite.com/api/users/create

但现在,端点仍然可以通过没有APIpreFIX上述网址,一个电话访问。

But now, the endpoints can still be accessed via a call to the above urls without the 'api' prefix.

如何从发生或什么会做最好的办法prevent这是这样?

How can I prevent this from happening or what would the best way to do this be?

推荐答案

一个解决方案应该去除方法 AUTH 创建,并把它们放到一个新的控制器应用程序/控制器/ API / users.php命名用户。那么你也可以删除您的API的具体路线,它会工作。 (请参见这里

A solution should be to remove the method auth and create from your Users controller and put them into a new controller named Users in application/controllers/api/users.php. Then you can also remove the specific route for your api, it will work. (see here)

这篇关于一个“preFIX”控制器添加到URL中的codeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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