【问题标题】:CakePHP page with no headers/footers没有页眉/页脚的 CakePHP 页面
【发布时间】:2011-07-28 12:06:20
【问题描述】:

在来自数据库的 blob 的下载页面中,我将如何制作它以便不发送其他输出?现在它正在发送页眉、调试信息和页脚。我怎样才能让它不发送,只是为了那个视图?

【问题讨论】:

    标签: cakephp download blob


    【解决方案1】:

    你可以在你的布局文件夹中创建一个清晰的布局(例如empty.ctp),只有

    <?php echo $content_for_layout ?>
    

    然后在您获取 blob 数据的操作中使用该布局

    $this->layout = 'empty.ctp';
    

    还要禁用调试,在你的控制器中使用

    Configure::write('debug',0);
    

    如果您无法创建新布局,您可以试试这个。

    $this->layout = null;
    $this->render("view_name");
    

    【讨论】:

      【解决方案2】:

      如果你使用它来下载文件,你应该使用 cakePHP 中的Media 视图

      http://book.cakephp.org/view/1094/Media-Views

          $this->view = 'Media';
          $params = array(
                'id' => 'example.zip',
                'name' => 'example',
                'download' => true,
                'extension' => 'zip',  // must be lower case
                'path' => APP . 'files' . DS   // don't forget terminal 'DS'
         );
      

      【讨论】:

        【解决方案3】:

        CakePhp 2.3 用户:

        CakePhp 2.x 用户:

        • 使用“$this->viewClass”代替“$this->view”

        复制粘贴完整的解决方案,就在任何控制器文件中:

        <?php
        
        public function download($file) {
        
            $fsTarget = APP.WEBROOT_DIR.DS.'files'.DS.$file; // files located in 'files' folder under webroot
            if (false == file_exists($fsTarget)){
                    throw new NotFoundException(__('Invalid file'));
            }
        
            $pathinfo = pathinfo($fsTarget);
        
            $this->viewClass = 'Media';
        
            $params = array(
                  'id' => $file,
                  'name' => $pathinfo['filename'], // without extension
                  'download' => true,
                  'extension' => $pathinfo['extension'],  // must be lower case
                  'path' => dirname($fsTarget) . DS // don't forget terminal 'DS'
           );
           $this->set($params);
        }
        

        希望这会有所帮助!

        【讨论】:

        • 媒体视图已被弃用 - 在 2.x 中,应按照文档说明对这些内容使用响应类。
        • 嗨,马克,我会更新答案。即使 2.3 甚至还没有在主页上发布。至于我的代码,这个方法是很久以前写的:)
        • 2.x != 2.3 (2.x = 2.0/2.1/2.2/2.3)。甚至在 2.0 中,您已经可以为此使用响应类。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多