Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering controller to a different view in CakePHP

Tags:

php

cakephp

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

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

I have tried everything from here with no luck.

like image 989
devmonster Avatar asked Jul 29 '12 17:07

devmonster


People also ask

How can I redirect to another page in CakePHP?

You can use: $this->redirect(array("controller" => "myController", "action" => "myAction", "param1" => "val1", "param2" => "val2", $data_can_be_passed_here), $status, $exit); Hope it helps!

What is beforeFilter in CakePHP?

beforeFilter() executes functions that you NEED to be executed before any other action. Controller::beforeFilter() This function is executed before every action in the controller. It's a handy place to check for an active session or inspect user permissions. http://api.cakephp.org/2.3/class-Controller.html#_ ...

Which function of controller render () manually before the end of a given action?

This callback is not used often, but may be needed if you are calling Controller\Controller::render() manually before the end of a given action. Called during the Controller. shutdown event which is triggered after every controller action, and after rendering is complete. This is the last controller method to run.

How can I get current controller name in CakePHP 3?

Use $this->params['controller'] to get the current controller.


1 Answers

The right way:

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

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

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).

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.

like image 78
40pro Avatar answered Sep 19 '22 13:09

40pro