php 在 CakePHP 中将控制器渲染到不同的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11711385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:55:56  来源:igfitidea点击:

Rendering controller to a different view in CakePHP

phpcakephp

提问by devmonster

Is there a way to render a controller to a different view then normal? I'm trying to pass some data from the controller to a non-default view. Meaning my controller is called:

有没有办法将控制器渲染到不同的视图然后正常?我正在尝试将一些数据从控制器传递到非默认视图。这意味着我的控制器被称为:

class StocksRealtimeController extends AppController {
    var $uses               = 'StockRealtime';
    function index(){
        $action = '/TestView';
        $this->set('stocksRT', $this->StockRealtime->find('all'));
        //$this -> viewPath = 'Pages';
        $this -> render('/TestView/index');
    }
}

... and My view is in views->TestView->index.ctp

...我的观点是在 views->TestView->index.ctp

Another question I have is, how to pass that value to a PHP and not a ctp file outside of the CakePHP framework?

我的另一个问题是,如何将该值传递给 PHP 而不是 CakePHP 框架之外的 ctp 文件?

I have tried everything from herewith no luck.

我从这里尝试了一切,但没有运气。

回答by 40Plot

The right way:

正确的方式:

$this -> render('TestView/index');

$this -> render('TestView/index');

As the answer above mentions, you can use $this -> setto pass a variable to the View.

正如上面提到的答案,您可以使用$this -> set将变量传递给视图。

However, if that doesn't give you what you want. I'm guessing that you also want the action to display another layout (non-default layout). You can try doing $this -> layout = 'layoutname';(Layouts are in the layout folder, default on is default.ctp).

但是,如果这没有给你你想要的。我猜您还希望该操作显示另一个布局(非默认布局)。您可以尝试这样做$this -> layout = 'layoutname';(布局在布局文件夹中,默认为default.ctp)。

Note:CakePHP's controller isn't designed to pass data to a non-view file (like .php). CakePHP's views should be ending with .ctp.

注意:CakePHP 的控制器并非设计用于将数据传递到非视图文件(如 .php)。CakePHP 的视图应该以.ctp.

回答by Sabin Neagu

I would rather use:

我宁愿使用:

$this->view = 'file';

because any $this->set('var', $val)you'll have after $this->render('file')will not reach your view.

因为$this->set('var', $val)你所拥有的任何东西都$this->render('file')不会达到你的观点。

In CakePHP 3.x use:

在 CakePHP 3.x 中使用:

$this->viewBuilder()->template('file');

Deprecated in CakePHP 3.7. Use this instead (as Kuldeep Choudhary suggested in comments)

在 CakePHP 3.7 中弃用。改用这个(正如 Kuldeep Choudhary 在评论中建议的那样)

ViewBuilder::setTemplate('file');

回答by devmonster

Try to put the name of the view without .ctp extension.

尝试放置不带 .ctp 扩展名的视图名称。

$this->render('file');

回答by Parvesh Kumar Tandon

class StocksRealtimeController extends AppController
{
   var $uses = 'StockRealtime';

   function index( )
   {
     $this->layout     = NULL;
     $this->autoRender = false;

     $this->set('stocksRT', $this->StockRealtime->find('all'));

     return $this -> render('/TestView/index');
     /*
        $this -> render('/TestView/index');
        Here 'TestView' must be a Folder named same as "public $name" variable value        
        in Controller and an "index.ctp" must be situated under TestView Folder.
       'index'
     */
   }
}

Give it a try, return 'KEYWORD' must be there to render view page successfully. Sorry about 2nd question as i didn't get it. According to CakePHP, variable [stocksTR] which is set using $this -> set( ) , also will be available at manually render view page [ 'index.ctp' ].

试一试,返回 'KEYWORD' 必须存在才能成功呈现视图页面。抱歉第二个问题,因为我没听懂。根据 CakePHP,使用 $this -> set( ) 设置的变量 [stocksTR] 也可以在手动渲染视图页面 ['index.ctp'] 中使用。

回答by chetanspeed511987

class StocksRealtimeController extends AppController {
var $uses               = 'StockRealtime';

    function index(){

       $this->layout = NULL;
       $this->autoRender = false;

       $this->set('stocksRT', $this->StockRealtime->find('all'));

       $this -> render(`/TestView/index`);

    }
}

回答by giuseppe

 $this->view  = '/TestView/index';
 $this->set('stocksRT', $this->StockRealtime->find('all'));

回答by Ajay Kori

public function admin_index() { 

    $this->layout = 'admin/table';

    $action = '/Vendors';

    $this->Prg->commonProcess('Vendor');

    $this->paginate = array('conditions' => array($this->Vendor->parseCriteria($this->passedArgs)), 'order' => 'Vendor.created_on DESC', 'limit' => 15);

    $this->set('vendor', $this->paginate('Vendor'));

    $this->render('/vendors/admin_items');
}