How do I pass properly the models data to the view from controller in joomla 3.1. On start I initialize one of my sub controllers method to gather data on item which should fill up my form layout. Is accessed with the following url ?option=com_unis&task=unis.edit&layout=edit&id=1 than my controllers method looks like
public function edit()
{
$input = JFactory::getApplication()->input;
$model = $this->getModel ( 'item');
$view = $this->getView('item', 'html');
$view->setModel($model);
$view->setLayout('edit');
// Display the view
$view->display();
return $this;
}
than if I try to access the model in my view is returning null
Found it! But maybe is not the best workaround
in the view I init my model like
$model = $this->getModel('mymodel');
$data = $model->my_method($args);
than associate to the layout with a public variable
$this->data = $data;
After all I found out the workaround. In the view I call my model as it follows
$model = $this->getModel('mymodel');
$data = $model->my_method($args);
than I created a public variable which holds the layout data
$this->data = $data;
The controller picks up the view to be used. The model functions can be called in the views/somefolder/view.html.php from there the assignes variables can be viewed in the template default file.
class MyPageViewMyPage extends JViewLegacy {
/**
* Display the Hello World view
*
* #param string $tpl The name of the template file to parse; automatically searches through the template paths.
*
* #return void
*/
public $data;
function display($tpl = null) {
// Assign data to the view
$this->msg = $this->get('Msg'); // call a method msg in the model defined
$model = $this->getModel(); // creating an object for the model
$this->data = $model->getFilter(1); // call a method defined in model by passing the arguments
// Check for errors.
if (count($errors = $this->get('Errors'))) {
JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');
return false;
}
// Display the view
parent::display($tpl);
}
}
in the default template file
echo $this->msg;
print_r($this->data);
Related
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();
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'd like to reuse my templates and would like to return only one rendered section as an ajax response (html table) which belongs to the "content" section (index.blade.php).
#section('content')
html...
#endsection
I've created another layout called ajax (ajax.blade.php) which contains only:
#yield('content')
My controller:
class Some_Controller extends Base_Controller {
public $restful = true;
public $layout = 'layouts.main';
public function get_index (){
if ( Request::ajax() )
$this->layout = 'layouts.ajax';
$view = View::make('some.index')->with('data', 'shtg');
$this->layout->content = $view;
}
}
It works when I request the route via normal GET request... but when I request it via ajax I get an error:
Attempt to assign property of non-object
on the line containing
$this->layout->content = $view;
I've also tried
return Section::yield('content');
Which returns empty document.
Is there a way to return rendered section? I've searched over the forums and couldn't find anything apart from:
http://forums.laravel.io/viewtopic.php?id=2942
Which uses the same principle and doesn't work for me (I've tried all the variations mentioned on the link above).
Thanks!
You appear to be mixing blade templates with controller templates. If you wish to use controller layouts (my preference) then remove the #section('content') and #endsection, and replace #yield('content') with $content.
However, that is not your entire problem. The following line is picked up by the layout method and converted into a real view...
public $layout = 'layouts.main';
You could easily extend the layout function in your controller, adding a layout_ajax attribute like this...
/**
* The layout used by the controller for AJAX requests.
*
* #var string
*/
public $layout_ajax = 'layouts.ajax';
/**
* Create the layout that is assigned to the controller.
*
* #return View
*/
public function layout()
{
if ( ! empty($this->layout_ajax) and Request::ajax() )
{
$this->layout = $this->layout_ajax;
}
return parent::layout();
}
I am developing a joomla 2.5 component where I need to pass data from controller to model. The controller is receiving data from url. I find that controller is getting the value properly. Now I need to move that value to model from controller. From different post I have found a snippet of code for controller like below.
$datevalue = JRequest::getVar('day',$day); //receiving value from view
$item = JRequest::setVar('day',$datevalue); //setting variable
$model =& $this->getModel('WeeklyProgram'); //assign model
$model->setState('dayVar', $item); // assign value for model
The problem is that I don't know how to receive this value 'dayVar' from model. Can anybody help me on this issue? Thanks.
Use following things
In Modal
class CommunityModelCevent extends JCCModel
{
var $membersCount = null;
function getMembersCount($value) {
$this->membersCount = $value // set your value here 15
// Now you can access this variable into model
}
}
In controller
$ceventModel = CFactory::getModel( 'cevent' );
$membersCount = $ceventModel->getMembersCount(15);
You can do like this . First you make get and set function in the model.Second load the model in the controller and simply pass the values to setter function.Example as follows:
updateratings.php---this is my model
class RatingManagerModelUpdateRatings extends JModelLegacy
{
public $data;
public function get_data(){
$data=$this->data;
return $data;
}
public function set_data($data){
$this->data=$data;
}
}
Controller.php
class RatingManagerController extends JControllerLegacy
{
public function save_ratings(){
$tips = JRequest::getVar('tips'); //get data from front end form
$model = $this->getModel('UpdateRatings'); //load UpdateRatings model
$model->set_data($tips); //update setter function of model
$res=$model->get_data(); // retrieve getter function
//print_r($res);
}
}
i want to send some data from Action Helper to view Partial and i am unable to do it, to get the clear picture. here is all the related code i am using.
in my layout.phtml i am using this placeholder. to generate onDemand navigation menu.
<?php echo $this->placeholder('action-navigation'); ?>
so when i need it in my controller or action method i can simply place use this code.
$this->_helper->navigation()->renderActionNavigation();
The Action helper i am using is.
class Zend_Controller_Action_Helper_Navigation extends Zend_Controller_Action_Helper_Abstract
{
private $_view = null;
public function direct()
{
$this->_view = $view = Zend_Layout::getMvcInstance()->getView();
$this->_view->placeholder('action-navigation');
return $this;
}
public function renderActionNavigation()
{
$config = new Zend_Config_Xml(
APPLICATION_PATH.'/configs/navigation.xml', strtolower(
$this->getRequest()->getControllerName().
$this->getRequest()->getActionName()
)
);
$container = new Zend_Navigation($config);
// here i want to send $container to _action-navigation.phtml.
$this->_view->addScriptPath(APPLICATION_PATH.'/layouts/')->render('partials/_action-navigation.phtml');
}
}
this is my view partial _action-navigation.phtml
$this->placeholder('action-navigation')->captureStart();
//i want to get zend_navigation instance. from the above action helper here.
$this->placeholder('action-navigation')->captureEnd();
i have problem sending data from action helper to partial view _action-navigation.phtml how do i do it?
Thank you.
Use partial() instead of render():
$this->_view->partial('partials/_action-navigation.phtml', array('nav' => $container));
And in your partial:
$this->nav // to get your container