如何让CakePHP使用换行符渲染纯文本?

Sov*_*iut 2 content-type cakephp views render plaintext

我需要我的一个控制器操作,以每行的名称返回一个名称列表,作为纯文本.这样做的原因是它可以被期望这种格式的JQuery自动完成插件使用.不幸的是,当页面呈现时,\n字符不会呈现为换行符.

调节器

function UserController extends AppController {
    var $components = array('RequestHandler');

    function users_ajax() {
        $users = $this->User->find('all');
        $this->set('users', $users);

        $this->layout = false;
        Configure::write('debug', 0);
        $this->RequestHandler->respondAs('text');
    }
}
Run Code Online (Sandbox Code Playgroud)

视图

foreach($users as $user) {
    echo $user['User']['name'] . '\n';
}
Run Code Online (Sandbox Code Playgroud)

结果

第一个用户\nSECOND用户\nTHIRD用户\n

据我所知,在视图返回纯文本,但是,\n被直译出来.我怎么能阻止这个?

Nik*_*kov 7

It's not the Cake it's just the PHP. :)

Using single quotes, the characters between them are threated as string, while double quotes are interpret the \n to a new line. SO in your case:

foreach($users as $user) {
    echo $user['User']['name'] . "\n";
}
Run Code Online (Sandbox Code Playgroud)

should do the magic :)