0

基本的に、Ajax_Controller で URL をハードコーディングすればすべてが機能しますが、作成した CMS フィールドから URL にアクセスできるようにしたいと考えています。

前もって感謝します。(中括弧を閉じていない場合は無視してください-効率的にコピー/貼り付けしようとしています)

/mysite/_config.php で、カスタム構成を作成しました。

Object::add_extension('SiteConfig', 'CustomSiteConfig');

/mysite/code/CustomSiteConfig.php に、URL を保存するフィールドを追加しました。

class CustomSiteConfig extends DataObjectDecorator {

function extraStatics() {
    return array(
        'db' => array(
            'COJsonPath' => 'Text'
        )
    );
}

public function updateCMSFields(FieldSet &$fields) {
    $fields->addFieldToTab("Root.CO", new TextField("COJsonPath", "CO JSON URL"));
}

public function getCOJsonPath(){
    return $SiteConfig.COJsonPath;
}

これにより、CMS のメインの親に「CO」という名前のタブと「CO JSON URL」という名前のフィールドが正常に作成されます。CMS にログインし、http://api.localhost/mymethod/をそのフィールドに保存しました。

ここで、Ajax ページ タイプを作成して、Web サイトのユーザーに API の場所を見つけさせずに Ajax コマンドを実行しやすくしました。これは、jQuery Ajax が XSS (クロス サイト スクリプティング) のようなものではないためです。

/mysite/code/Ajax.php 内:

class Ajax extends Page {

static $db = array(
);
static $has_one = array(
);

function getCMSFields()
{
    $fields = parent::getCMSFields();
    return $fields;
}

}

class Ajax_Controller extends Page_Controller {
public function getCO()
{
    $buffer = self::createHttpRequest("http://api.localhost/mymethod/");
    //$buffer = self::createHttpRequest($CustomSiteConfig::getCOJsonPath());        
    return $buffer;
}

このコードは機能しますが、コメントアウトされている行で createHttpRequest() を実行しようとすると失敗します。構文が間違っていることはわかっていますが、どうあるべきかわかりません。助けてくれてありがとう - 私はそれを理解できない前にこれをやった - その金曜日。

4

1 に答える 1

1

あなたのコードでいくつかの構文エラーを見つけました:

public function getCOJsonPath(){
    return $SiteConfig.COJsonPath;
}

次のようにする必要があります。

public function getCOJsonPath(){
    return $this->owner->COJsonPath;
}

1) $SiteConfig はその時点で定義されていません。2) 通常は $this を使用しますが、あなたの場合は DataObjectDecorator 内にいるため、$this->owner を使用する必要があります 3).オブジェクトのプロパティにアクセスするために使用することはできません。php では使用する必要があります->


クラス Ajax_Controller に移ると、getCO 内に次のエラーがあります。

1) $CustomSiteConfig が定義されていないため、使用できません 2) getCOJsonPath は静的関数ではありませんが、静的関数として呼び出そうとします (再度使用する必要があります)->

したがって、コードは次のようになります。

public function getCO() {
    $siteConfig = SiteConfig::current_site_config();
    $buffer = self::createHttpRequest($siteConfig->getCOJsonPath());        
    return $buffer;
}


それはうまくいくはずですが、改善できる別の考えがあります。私が理解しているように、あなたは ajax ページを作成しています。それを CMS で一度作成し、Web サイトのコンテンツ作成者に ajax ページに触れないように伝えますか? これはかなり醜く、やりたいことを実行するための優れた方法がいくつかあります。

Ajax コントローラーを作成する方法は次のとおりです。

_config.php

// tell SilverStripe what URL your AjaxController should have, 
// here we set it to AjaxController::$URLSegment which is 'myAjaxController'
// so the url to the controller is mysite.com/myAjaxController
Director::addRules(100, array(
    AjaxController::$URLSegment => 'AjaxController',
));

AjaxController.php

<?php
class EventAssetsController extends Controller {
    public static $URLSegment = 'myAjaxController';
    // tell SilverStripe what URL should call what function (action)
    // for example, mysite.com/myAjaxController/foo should call the function foo
    public static $url_handlers = array(
        'foo' => 'foo',
        'bar/$ID/$OtherID' => 'bar',
        'co' => 'getCO'
    );
    public function Link($action = null) {
        // this function is just a helper, in case you evern need $this->Link()
        return Controller::join_links(self::$URLSegment, $action);
    }
    public function AbsoluteLink($action = null) {
        return Director::absoluteURL($this->Link($action));
    }
    public function foo(SS_HTTPRequest $request) {
         // do something here
         // this method is an action, the url to this action is:
         // mysite.com/myAjaxController/foo
    }
    public function bar(SS_HTTPRequest $request) {
         // do something here
         // this method is an action, the url to this action is:
         // mysite.com/myAjaxController/bar
         // you notice that the $url_handlers has "bar/$ID/$OtherID", 
         // that means you cann call mysite.com/myAjaxController/bar/21/42
         // and silverstripe will make 21 the ID, and 42 the OtherID
         // you can access ID and OtherID like this:
         // $ID = $request->param('ID'); // which is 21
         // $OtherID = $request->param('OtherID'); // which is 42
    }
    public function getCO() {
        // this method is an action, the url to this action is:
        // mysite.com/myAjaxController/co
        $siteConfig = SiteConfig::current_site_config();
        $buffer = self::createHttpRequest($siteConfig->getCOJsonPath());        
        return $buffer;
    }
}
于 2012-10-06T08:03:06.503 に答える