2

次のようにjavascriptに変数があり、正常に動作します。

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

上記のリンクは、後でjavascriptを使用して表示するjsonデータを返します。しかし、私が望むのは、コントローラーへの完全なパスと、次のような関数の名前を与えることです

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>
4

1 に答える 1

2

1位。書き換えルールの作成: app/sys フォルダーのメイン index.php であるメイン フォルダーの .htaccess ファイルにこのコードを設定します。

リンクが他の方法で機能し、メイン ページのみをロードできるようにするために、これは重要です。

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

*サーバーは HTACCESS ファイルを受け入れる必要があるので、それが機能することに注意してください..

2番目。CI URL から index.php を削除します。で

app/config/config.php 

検索して空に設定

$config['index_page'] = '';

3番目。正しい URL を生成するには、その CI php 関数を使用します

site_url("api/v1");

URL ヘルパー http://ellislab.com/codeigniter/user_guide/helpers/url_helper.htmlが必要です

于 2012-10-30T10:08:50.150 に答える