【问题标题】:Change the extension of a URI in CodeIgniter在 CodeIgniter 中更改 URI 的扩展名
【发布时间】:2018-04-26 23:56:54
【问题描述】:

我正在用 PHP 制作 API。

我想像这样将.json 添加到 URL:

www.example.com/data.json

我正在使用 CodeIgniter。 我试过在这样的路由配置文件中解决这个问题

$route['default_controller'] = 'data';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['/data.json'] = "/data/index";

【问题讨论】:

  • 您想将其添加到所有控制器还是仅添加到特定控制器?
  • 我想将它添加到特定的控制器中

标签: php codeigniter url-routing


【解决方案1】:

您可以通过转到config.php 文件并更改url_suffix 来在CodeIgniter 中添加一个url 后缀:

$config['url_suffix'] = '.json';

这将使您的网站可以通过www.example.com/datawww.example.com/data.json 访问

查看documentation

如果您只想在某些页面中启用它,您可以使用这样的路由:

$route['data.json'] = 'data/index';

如果是动态路径的页面,例如data/some-id.json,请使用:

$route['data/(:any).json'] = 'data/index/$1';

【讨论】:

  • 谢谢它对我有用...但是如果我想将它们添加到特定的控制器...
  • @SaadAmir 检查更新的答案。如果您发现答案有帮助,请随时accept it
  • -实际上第二种方法不起作用...但是第一种方法就像魅力一样...
  • @SaadAmir 已更新,从一开始就删除 /
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 2017-11-02
  • 1970-01-01
  • 2016-12-05
  • 2012-03-17
  • 2013-02-27
  • 1970-01-01
相关资源
最近更新 更多