【问题标题】:how to give complete controller's path in codeigniter如何在codeigniter中给出完整的控制器路径
【发布时间】:2012-10-30 10:05:09
【问题描述】:

我在 javascript 中有一个变量,如下所示,效果很好。

var url = http://example.com/directory_name/

上面的链接返回 json 数据,我稍后会使用 javascript 显示它。 但我想要的是给出一个控制器的完整路径,然后是函数的名称,如下所示

http://example.com/directory_name/api/v1/ //whereas api is controller's name and v1 function inside this controller

我怎样才能做到这一点?

更新

这是我的控制器代码

class Api extends CI_Controller {
public function index() {
    //$this->load->view('api');
    $this->v1('api');
}

public function v1 () {
    //$this->load->view('api');
    echo site_url();
    $arr = array(
      array(
        'id' => 15642,
        'title' => 'title one',
        'description' => 'this is description for title one'
      ),
      array(
        'id' => 15643,
        'title' => 'title two',
        'description' => 'this is description for title two'
      ),
      array(
        'id' => 15644,
        'title' => 'title Three',
        'description' => 'this is description for title Three. this is description for title Three. this is description for title Three.'
      )
    );

    echo json_encode($arr);
}
}

更新 2

这里是JS

init: function (jokesContainer) {
            container = container;
            url       = 'http://example.com/directory/api/v1';    
            Get.get(url, container);
        }

更新 3

.htaccess 文件

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase chrome_web_apps

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>

【问题讨论】:

  • 您想在您的 php 文件中写入完整的站点/控制器/操作路径?
  • 我想在 javascript url 变量中给出一个完整的路径。
  • 所以 /directory/api/v1 不起作用?你有设置路由器吗
  • 你有没有在你的路由器上做任何路由? (复制它)
  • 这就是我在路线中的全部内容$route['default_controller'] = "api"; $route['404_override'] = '';

标签: php javascript jquery codeigniter


【解决方案1】:

第一。创建重写规则:将此代码设置到主文件夹中的 .htaccess 文件中,该文件夹是 app/sys 文件夹中的主 index.php。

这很重要,因此您的链接以其他方式工作,您将只能加载主页..

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA]

*请注意,您的服务器必须接受 HTACCESS 文件才能正常工作..

第二。从 CI url 中删除 index.php。 在

app/config/config.php 

查找并设置为空

$config['index_page'] = '';

第三。要生成正确的 url,请使用 CI php 函数

site_url("api/v1");

需要 url Helper http://ellislab.com/codeigniter/user_guide/helpers/url_helper.html

【讨论】:

  • 我不想在 javascript 中使用任何 php 代码。由于我将该链接公开,所以我希望用户复制整个链接并使用它。
  • 要向用户展示,您必须生成它。您可以通过您的 PHP 代码做到这一点..
  • @al0neevenings site_url .. 我在回答中添加了
  • 好的。我将在哪里添加 site_url() 方法?在控制器中还是在哪里?
  • 我没有你的代码时如何告诉你? .. 如果需要,请将其添加到您的视图文件中 ..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-17
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
相关资源
最近更新 更多