๐Ÿฐใ€CakePHP2ใ€‘ ํผ์˜ ์ž…๋ ฅ๊ฐ’์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ Ajax๋กœ ๋น„๋™๊ธฐ ํ†ต์‹ ํ•˜์—ฌ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ํ‘œ์‹œ์‹œํ‚จ๋‹ค

ํ™˜๊ฒฝ



PHP 7.2.21
CakePHP 2.10.18

ํ•˜๊ณ  ์‹ถ์€ ์ผ



์–‘์‹์˜ textbox์— ID๋ฅผ ์ž…๋ ฅํ•˜๋ฉด Ajax์—์„œ ๋น„๋™๊ธฐ ํ†ต์‹ ํ•˜๊ณ  ๊ทธ ๊ฐ’์„ ๋ฐ”ํƒ•์œผ๋กœ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์–ป์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ์‹œํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

ID ์ž…๋ ฅ

โ†“
ID์— ๋ฌถ๋Š” ๊ฐ’์„ ์˜†์— ํ‘œ์‹œ


ํ–ˆ๋˜ ์ผ



์ปจํŠธ๋กค๋Ÿฌ์—์„œ Ajax ์šฉ ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ณด์•ˆ ๋ณ€๊ฒฝ
View์—์„œ๋Š” ํ‘œ์‹œ ์˜์—ญ๊ณผ Ajax ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ ์ถ”๊ฐ€

Controller/HogeController.php
    /**
     * beforeFilter method
     *
     * @return void
     */
    public function beforeFilter(): void {
        parent::beforeFilter();

        // Ajax้€šไฟกใฎ็‚บใ‚ปใ‚ญใƒฅใƒชใƒ†ใ‚ฃใ‚’่งฃ้™ค
        $this->Security->unlockedActions = ['fetchDBData'];
    }

    /**
     * ๅ…ฅๅŠ›ใ•ใ‚ŒใŸIDใ‚’ๅ…ƒใซAjaxใงๅๅ‰ใ‚’ๅ–ๅพ—ใ™ใ‚‹public้–ขๆ•ฐ
     */
    public function fetchDBData() {
        // ่กจ็คบใฏใ›ใšใซใƒ‡ใƒผใ‚ฟใ‚’ใ‚„ใ‚Šใจใ‚Šใ™ใ‚‹้–ขๆ•ฐใชใฎใงViewไธ่ฆใฎ่จ˜่ฟฐ
        $this->autoRender = false;

        if ($this->RequestHandler->isAjax()) {
            if ($this->request->data) {
                $inputId = Hash::get($this->request->data, 'input_id');
            }

            $inputName = $this->Fuga->getFugaName($inputId);
            if (strlen($inputName) === 0) {
                return 'โ€ปๅญ˜ๅœจใ—ใพใ›ใ‚“';
            } else {
                return $inputName;
            }
        }
    }

View/Hoge/index.ctp
        <!-- ใƒ•ใ‚ฉใƒผใƒ  -->
        <?php echo $this->Form->create('Hoge'); ?>
        <?= $this->Form->input('modified_by', ['type' => 'hidden']) ?>
        <table class="formTable defaultTable">
            <colgroup>
                <col class="em8" />
            </colgroup>
            <tr>
                <th>ๅฏพ่ฑกID</th>
                <td>
                    <!-- IDๅ…ฅๅŠ› -->
                    <?php
                        echo $this->Form->input('input_id', [
                            'type'     => 'text',
                            'div'      => false,
                            'label'    => false,
                            'required' => false,
                        ]);
                    ?>
                    <!-- ๅ…ฅๅŠ›IDใซๅฟœใ˜ใฆๅๅ‰่กจ็คบ -->
                    <span id="input_name"></span>
                </td>
            </tr>
        </table>
        <div class="submitArea">
            <ul class="submitButtons">
                <li>
                    <label class="submitBase primary" for="submit">
                        <?php
                            echo $this->Form->submit('ไฟๅญ˜', ['id' => 'submit']);
                        ?>
                        ไฟๅญ˜
                    </label>
                </li>
                <li>
                    <?php echo $this->Html->link('ๆˆปใ‚‹', $this->Session->read('Hoge.lastIndexUrl'), ['class' => 'submitBase inverse']); ?>
                </li>
            </ul>
        </div>
        <?php echo $this->Form->end(); ?>

<!-- Javascript -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    // IDๅ†…ๅฎนๅค‰ๆ›ดๆ™‚
    $('#HogeInputId').change(function() {
        getNameWithAjax();
    });
    // ๅๅ‰ใ‚’้žๅŒๆœŸใซๅ–ๅพ—ใ—ใฆ่กจ็คบ
    function getNameWithAjax() {
        const inputId = $('#HogeInputId').val();
        $.ajax({
            url: '/Hoge/fetchDBData',
            type:'POST',
            data: {
                'input_id': inputId,
            },
        })
        .then(
            data => $('#input_name').text(data),
            error => alert('ๅๅ‰ใฎ่ชญใฟ่พผใฟใ‚จใƒฉใƒผ')
        );
    }
</script>

๊ฒฐ๊ณผ



ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค! !
success : ๋ผ๋“ ์ง€ error : ๋ผ๋“ ์ง€๋Š” ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‚ฌ์šฉํ•ด์„œ๋Š” ์•ˆ๋ฉ๋‹ˆ๋‹ค.

์˜ค๋ฅ˜ ๋ชจ์Œ



HTTP 400



ํฌ๋กฌ ์ฝ˜์†”
jquery.js:9659 POST https://{YOUR_DOMAIN}/Hoge/fetchDBDat 400 (Bad Request)

errorlog
2019-12-16 16:13:39 Error: [AuthSecurityException] The request has been black-holed
Request URL: /Hoge/fetchDBData
Stack Trace:
#0 /vagrant/Vendor/cakephp/cakephp/lib/Cake/Controller/Component/SecurityComponent.php(490): SecurityComponent->_validToken(Object(HogeController))

์ˆ˜์ • ๋ถ€๋ถ„

HogeController.php
        // beforeFilterใซใ‚ปใ‚ญใƒฅใƒชใƒ†ใ‚ฃใ‚’่งฃ้™คใŒๆŠœใ‘ใฆใ„ใ‚‹
        $this->Security->unlockedActions = ['fetchDBData'];

HTTP 404



ํฌ๋กฌ ์ฝ˜์†”
jquery.js:9659 POST https://{YOUR_DOMAIN}/Hoge/fetchDBData 404 (Not Found)

errorlog
2019-12-16 15:44:03 Error: [PrivateActionException] Private Action HogeController::fetchDBData() is not directly accessible.
Exception Attributes: array (
  'controller' => 'HogeController',
  'action' => 'fetchDBData',
)
Request URL: /Hoge/fetchDBData
Stack Trace:
#0 /vagrant/Vendor/cakephp/cakephp/lib/Cake/Routing/Dispatcher.php(193): Controller->invokeAction(Object(CakeRequest))

์ˆ˜์ • ๋ถ€๋ถ„

HogeController.php
private function fetchDBData() {
    // ๅ‡ฆ็†
}

// โ†“ ใ‚ขใ‚ฏใ‚ปใ‚นไฟฎ้ฃพๅญใ‚’privateโ†’publicใซๅค‰ๆ›ด

public function fetchDBData() {
    // ๅ‡ฆ็†
}

HTTP 500



ํฌ๋กฌ ์ฝ˜์†”
POST https://{YOUR_DOMAIN}/Hoge/fetchDBData 500 (Internal Server Error)

errorlog
2019-12-16 15:00:02 Error: [MissingViewException] View file "/vagrant/oauth/app/.../.../View/Hoge/fetch_d_b_data" is missing.
Exception Attributes: array (
  'file' => '/vagrant/oauth/app/.../.../View/Hoge/fetch_d_b_data.ctp',
)
Request URL: /Hoge/fetchDBData
Stack Trace:
#0 /vagrant/Vendor/cakephp/cakephp/lib/Cake/View/View.php(470): AppView->_getViewFileName('Hoge...')

์ˆ˜์ • ๋ถ€๋ถ„

HogeController.php
    public function fetchDBData() {
        // Viewไธ่ฆใฎ่จ˜่ฟฐใŒๆŠœใ‘ใฆใ„ใ‚‹
        $this->autoRender = false;
    }
}

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ