๐ฐใCakePHP2ใ ํผ์ ์ ๋ ฅ๊ฐ์ ์ค์๊ฐ์ผ๋ก Ajax๋ก ๋น๋๊ธฐ ํต์ ํ์ฌ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ํ์์ํจ๋ค
                                            
                                                
                                                
                                                
                                                
                                                
                                                 15467 ๋จ์ด  PHPCakePHP์๋ฐ์คํฌ๋ฆฝํธ์์ฝ์คcakephp2
                    
ํ๊ฒฝ
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;
    }
}
                Reference
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(๐ฐใCakePHP2ใ ํผ์ ์ ๋ ฅ๊ฐ์ ์ค์๊ฐ์ผ๋ก Ajax๋ก ๋น๋๊ธฐ ํต์ ํ์ฌ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ํ์์ํจ๋ค), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://qiita.com/eltociear/items/9e8442788dfa0e15c20aํ ์คํธ๋ฅผ ์์ ๋กญ๊ฒ ๊ณต์ ํ๊ฑฐ๋ ๋ณต์ฌํ ์ ์์ต๋๋ค.ํ์ง๋ง ์ด ๋ฌธ์์ URL์ ์ฐธ์กฐ URL๋ก ๋จ๊ฒจ ๋์ญ์์ค.
                                
                                
                                
                                
                                
                                ์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ
์ธ  ๋ฐ๊ฒฌ์ ์ ๋
                                (Collection and Share based on the CC Protocol.)
                            
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค