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.
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.
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.
In CakePHP 3.x use:
$this->viewBuilder()->template('file');
Deprecated in CakePHP 3.7.
Use this instead (as Kuldeep Choudhary suggested in comments)
ViewBuilder::setTemplate('file');
Try to put the name of the view without .ctp extension.
$this->render('file');
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' ].
$this->view = '/TestView/index';
$this->set('stocksRT', $this->StockRealtime->find('all'));
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');
}
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`);
}
}
Related
I'm running Laravel 7, and I wonder if it is possible to return a rendered Blade component from a controller, just like you would with a view. I can return the view of the component like the following.
return View::make('components.some-view');
However, I do not have access to any of the data or methods inside the SomeView component class. If I try to use a variable defined in the component, I receive an undefined variable error.
Try this, it works for me in Laravel 8
for example I have App\View\Components\Form\Button component
<?php
class Button extends Component{
public $variable1;
public $variable2;
public function __construct($variable1, $variable2){
$this->variable1 = $variable1;
$this->variable2 = $variable2;
}
public function render(){
// return view('view_component_name');
}
}
?>
& I wants to render that component in controller without view layout
then I can do like this
<?php
use App\View\Components\Form\Button;
class TestController extends Controller{
public function index(Request $request)
$obj = new Button($variable1, $variable2);
$obj->render()->with($obj->data());
}
}
?>
** data function include methods, properties and attributes of Component.
I hope this one helps to you
You can render a blade component from your controller using view function:
//first parameter = Path of your component
//second argument = All your variables that your component receive
$html = view('components.yourComponentName', ['x-variable' => $value]);
return $html;
Create object of component in controller like
$com = new SomeComponent($variable1, $variable2);
//call the render function
$comHtml = $com->render();
Hopefully this will help.
Use YourDirectory\some-view;
$controllerObject = new some-view;
$controllerObject -> variable;
Basically what I need to do is:
$x = 'Admin';
$model = new \ReflectionClass($x);
$model->getFieldList();
Where I have Admin model inside app folder.
Obviously, this doesn't work. Does anyone have any idea? Can it be done?
Firstly you need to add the namespace to your model. By default this is App\. So your string would have to be "\App\Admin". Now you can simply create a class instance using this string.
$x = '\\App\\Admin';
$model = new $x();
you can do this, but you need to use the fully qualified class name.for example My models are in the Model directory :
$model = 'App\Model\User';
$user=$model::where('id', $id)->first();
i use something like that in the controller constructor. here what i use:
1)first declare a variable in parent controller lets say:
$route_model_name and $model_location.
then add this to each child controller:
function __construct() {
parent::__construct();
$this->setRouteModelName( 'model_route_name' );
$this->setModelLocation( 'App\Models\ModelName' );
}
then you add a method in the parent controller to initialize the model class in order to be ready for making queries:
/**
* #return mixed
*/
protected function getModelClass() {
return app( $this->model_class );
}
then you can change the model location and route in each of the child controllers and still use the same method in the parent controller:
public function edit( $id ) {
return $this->getModelClass()::where( 'id', $id )->first();
}
$model = 'App?Models?'.$model;
$model = str_replace('?','\\',$model);
return $model::products()->paginate();
Like superficial descriped in SilverStripe Docs, I'm trying to set a custom controller for my homepage.
I changed the default homepage link to 'custom-home' and added those two routes.
The second one, with the path in it works and directs me to my controller. The first (empty) one just sends me to an 404-error page.
Couldn't figure out how to fix that. Any suggestions?
routes.yml
Director:
rules:
'': 'MyHome_Controller'
'custom-home': 'MyHome_Controller
_config.php
RootURLController::set_default_homepage_link('custom-home');
MyHome_Controller.php
<?php
class MyHome_Controller extends Page_Controller {
private static $allowed_actions = [];
private static $url_handlers = [];
public function init() {
parent::init();
}
public function Link($action = null) {
return Director::baseURL() . 'custom-home';
}
public function index() {
$data = [
'Title' => 'Hello World',
'ClassName' => __CLASS__,
];
return $this
->customise($data)
->renderWith([__CLASS__, 'Page']);
}
}
I believe the way the empty route (RootURLController) works is that you're telling it the URLSegment of a page in the CMS that should resolve to the root URL. So I think what you need to do is go into the CMS and change the URLSegment of your CustomHomePage to 'custom-home'.
I've tried many solutions that had the same questions like mine. But didn't found a working solution.
I have a controller:
event.php
And two views:
event.phtml
eventList.phtml
I use eventList to get data via ajax call so I want to populate both views with a variable named "eventlist" for example.
Normally I use this code for sending a variable to the view:
$this->view->eventList = $events;
But this variable is only available in event.phtml.
How can I make this available for eventlist.phtml? (without adding a second controller)
Edit:
I get this error now
Call to undefined method Page_Event::render()
Function:
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
$this->eventList = $this->_event;
$this->render();
$this->render('eventlist');
}
If I use $this->view->render('event.phtml') and eventlist.phtml it won't pass the data
I'm using zend version 1
You can pass variables to other views using render()
public function fooAction()
{
// Renders my/foo.phtml
$this->render();
// Renders my/bar.phtml
$this->render('bar');
}
Copy and paste this in your controller and rename your controller from event.php to EventController.php
class EventController extends Zend_Controller_Action
{
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
// You're calling the index.phtml here.
$this->eventList = $this->_event;
$this->render('event');
$this->render('eventlist');
}
}
To specify that only written #Daan
In your action:
$this->view->eventList= $events;
$this->render('eventList'); // for call eventList.phtml
In you View use : $this->eventList
You could render it within the view itself (eventList.phtml), rather than within the controller, using the same line of code you used above:
$this->render('event[.phtml]');
I have a controller which, within a method, I want to specify a different layout for my view. I would have thought the below would do the trick, however, I am getting:
Attempt to assign property of non-object
on the line where I am trying to reset the layout.
class My_controller extends Base_Controller {
public $layout = "cms::layouts.default";
............
public function get_list($status = "open")
{
$this->layout = 'cms::layouts.nowrap';
$this->layout->strContent = View::make('cms::partials.orderdetails')
->with('xxxx', \CMS\XXX::method($xxx));
}
.................
}
Any ideas? I'm using Laravel 3 for this one
I think that you have to do that :
$this->layout = View::make('cms::layouts.nowrap');
$this->layout->strContent = ...;
It's the L4 solution but, after a small check, it seems to be the same pattern in L3.