我在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>
答案 0 :(得分:2)
第一。创建重写规则:将此代码设置为主文件夹中的.htaccess文件,其中是app / sys文件夹中的主要index.php。
这很重要,所以您的链接以其他方式工作,您只能加载主页面。
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA]
*请注意,您的服务器必须接受HTACCESS文件才能正常工作..
第二。从CI网址中删除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