In Laravel controller function you can return view with array of variables you like to share with the current view. Something like this:
public function index() {
return view('index')->with(['name' => 'John doe']);
}
Then you can use the name inside the with() function as $name in your view.
Can someone explain how it been done please? I would like to do something similar by myself.
We can pass individual pieces of data to the view using the 'with' method.
When passing information in this manner, the data should be an array with key / value pairs. Inside your view, we can then access each value using its corresponding key, such as
<?php echo $key; ?>
Laravel include the view file inside a function which makes all defining variables scope within a function.
Just before including files laravel calls PHP's builtin function extract(array) which declare all the variable in the scope.
extract create variables out of array making key as variable name and value as variable value.
Related
I need to filter my datatable using nodoc which is the value of input in page before, it has read in my v_filterdoc function but can't be read in my datatable function and it becomes null when I use input->post so I need to pass the value to my datatable before the value is null
This is my v_filterdoc functionµ
And this is my Datatable function
you have many ways of passing value from one function in the same controller to another function.
the ability of any function like this code. $this->function_name($variable);
you can use session in the first function you must set the session of $variable and use it in another function also its available in all function.
I have a helper file, it has a variable that I want to pass across to a view, but it comes across as empty, so I am a bit unsure if I have the right code or I have overwritten it later on _ though I am sure I have not!
anyway say the variable in ther helper file is an array that contains a list of data, and I use:
$this->load->helper('helperfile_helper'); //contains the variable 'productList'
$data['productList'] = $productList;
$this->load->view('page', $data);
I would expect that the helper file works like an 'include' with the defined variables available once the helper has been called, is this the casee or have I missed something??
Helpers allow you to use function in your controller, have a look here: http://ellislab.com/codeigniter%20/user-guide/general/helpers.html
So you must create a function in your helper file that will return a value.
For example in your helper:
if( ! function_exists('random_number'))
{
function random_number()
{
return 4;
}
}
and in your controller you can use it:
$this->load->helper('helperfile_helper'); //contains the variable 'productList'
$data['random_number'] = random_number();
$this->load->view('page', $data);
So $data['random_number'] will contain 4
in my controller i have an public variable i want to use in my view.
public $header = array("show_menu"=>true);
Traditional i would pass variables as an array to the load->view("incl_header",$header) function, however i have noticed that in my view i can retrieve variables of my controller like so:
echo $this->header['show_menu'];
Are there any problems retrieving variables like this in my view file?
I am using codeigniter 2.1.3
It's possible to do it like that.
If you use var_dump($this) you can see all the variables that are availible in your view.
It's not the normal codeigniter way of retrieving variables in your view.
How ever this might change in futeure releases of codeigniter so you must keep that in mind when using this method.
I am trying to pass a variable from a view (of mobile model) to a different controller (of inventory model), using the chtml:button with this code
echo CHtml::button(
'Sell It',
array('submit' => array('inventory/create', array('id'=>$data->id)))
);
Now how do I access the $id variable in the Inventory controller, so that I can prepopulate the create view with details corresponding to the passed 'id' variable of the mobile model.
In your inventory/create controller action do checking before getting the id like this :-
if (isset($_REQUEST['id'])) {
$id = $_REQUEST['id'];
$this->render('create',array('model'=>$inventory, 'id'=>$id));
}
else{
$this->render('create',array('model'=>$inventory);
}
If you are trying to come up an update/edit form with the values prefilled based on the Id passed then you should have to go through the CRUD options available within YII.. This is much better way to handle record updation and its easy too . See this topic for furhter info..
http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app
In your inventory/create controller action do a test for $_GET['id'] something like:
$id = (#$_GET['id']) ? : DEFAULT_VALUE;
$this->render('create',array('model'=>$inventory, 'id'=>$id));
and then you pass the data through to the view by passing an array of variables you want to make available.
(you would want to filter the input better, this is just a sample -- using filter_input or some other method and define a default value and/or some test for it being null/invalid)
in your controller you can get the variable by giving an argument to your controller method like this:
public function actionCreate($id){
$id = isset($id)?$id:NULL; // Or whatever, you can access it like this.
}
You don't have to use $_GET, and yii has already done some security checks on the value.
I have in my view a partial containing a partialLoop.
But when I run the page I have the following error message:
Call to a member function countComments() on a non-object in ...'_loop.phtml'
This is how I call my partial from within my view:
echo $this->partial('_post.phtml',$this->post);
where $this->post is a DB retrieved row
This is my partial's content:
MY simplified Partial!
echo $post->countComments();//the count number is correctly output..
echo $this->partialLoop('_loop.phtml',$this->object);
This is my partialLoop's content:
echo $this->object->countComments();//no output!
In the bootstrap I have set:
$view->partial()->setObjectKey('object');
$view->partialLoop()->setObjectKey('object');
Is this the right way to call partialLoops from within partials??
P.s. I var_dumped $this->object inside my partial and it is a PostRow OBJECT.I var dumped $this->object into _loop.phtml and I have 5 NULLS (standing for id,title,text,author,datetime fields of my post)
thanks
Luca
I think that the reason is that when you pass $this->post into partial view helper like this:
$this->partial('_post.phtml',$this->post);
partial view helper will execute its toArray() method. Hence, your $this->object is an array and you are passing an array to your partialLoop. So, in your partialLoop you are trying to execute countComments() on an array representing your row post object, rather than actual row object.
To avoid this, I would recommend passing variables to partial and partialLoop view helpers using array notation, e.g:
$this->partial('_post.phtml',array('post' => $this->post));
Hope this helps.
This error is caused by the default behaviour of the partial and partialLoop view helpers as Marcin said above.
Although it is confusing the manual does explain this here
Object implementing toArray() method. If an object is passed an has a
toArray() method, the results of toArray() will be assigned to the
view object as view variables.
The solution is to explicitly tell the partial to pass the object. As the manual explains:
// Tell partial to pass objects as 'model' variable
$view->partial()->setObjectKey('model');
// Tell partial to pass objects from partialLoop as 'model' variable
// in final partial view script:
$view->partialLoop()->setObjectKey('model');
This technique is particularly useful when passing
Zend_Db_Table_Rowsets to partialLoop(), as you then have full access
to your row objects within the view scripts, allowing you to call
methods on them (such as retrieving values from parent or dependent
rows).