Passing data from Views laravel 4.2 - php

I do some calculations in the controller, and pass the data to my view like this.
return View::make('fend.clist', compact('detail_'));
From this view controller "fend.clist" I'd like to open another view (blade) along with the 'detail_' variable which basically has some data that I need to show there. I tried POSTing it, but I wasn't able to recieve the data completely.
How else can I pass data from one view to another view?

By using the sessions, first put:
Session::put(['name'=>'value']);
Then get:
Session::get('name');

You must nest sub-view to view and pass data to sub-view.
return View::make('fend.clist')->nest('fend.detail', compact('detail_'));
More info in docs

Related

How to pass parameters from one view to another in laravel?

I have a string inside a view that I need to pass to a second view.
Inside view1 I have
route('admin.olt.add', $id, $resp)
where $resp is the parameter I need to pass to the second view. My route file calls the controller that returns the second view
Route::get('/olt/{id}/add', 'OLTController#config_parameters_onu')->name('olt.add');
Is there any way where I can pass this parameter without adding it to the url?
I don't know if I fully understand your issue but as I got it, you can add this variable to a session and then fetch it whenever you want in any view for instance
to add the variable to the session
session(['resp' => $resp]);
to fetch it back
session('resp')
and here is the full docs for better guidance
Hope that is what you looking for
you should use an array of more than one parameter
route('admin.olt.add', ['id'=>$id, 'resp'=>$resp]);

Passing data to layout in Laravel 5.2 on each request

Hi I am building an application using laravel 5.2 I have a messaging system and I would like to populate some sections in my layout like 'Current Unread Messages Count' and a summary of the last 5 messages.
The way i was gonna go about it is to call a method get the data that I need and then pass the data to the layout and then render the view.
I understand this can be done with a view composer but I have no idea how. Any input would be greatly appreicated. Thanks in advance
Yes, you can do that with a view composer
Let's suppose you have a my_menu.blade.php view file and you want to pass some data to it. In a service provider's boot method do:
//every time the my_menu view is rendered, this callback will be called
View::composer('my_menu', function( $view )
{
$data = //get your data here
//pass the data to the view
$view->with( 'data', $data );
} );
Now, everytime the my_menu view will be rendered, Laravel will call your callback, fetch the data and pass the data to the view. So, in your view file, you can access the data with $data

codeigniter - retrieve controller variables in view (using $this)

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.

how to pass a variable from one controller to the other in Code igniter

I have just started learning Code Igniter .
I want to know how can I pass a variable from one controller(first_cont.php) to other controller (second_cont.php) ?
Any help would be appreciated .
Thanks in Advance :)
It will depend on the circumstances. If you want to retain the data for some time, then session data would be the way to go. However, if you only need to use it once, flash data might be more appropriate.
First step would be to initialise the session library:
$this->load->library('session');
Then store the information in flash data:
$this->session->set_flashdata('item', $myVar);
Finally, in the second controller, fetch the data:
$myVar = $this->session->flashdata('item');
Obviously this would mean you'd have to either initialise the session library again from the second controller, or create your own base controller that loads the session library and have both of your controllers inherit from that one.
I think in codeigniter you can't pass variable, between two different controller. One obvious mechanism is to use session data.
Ok, here is something about MVC most will readily quote:
A Controller is for taking input, a model is for your logic, and, a view is for displaying.
Now, strictly speaking you shouldn't want to send data from a controller to another. I can't think of any cases where that is required.
But, if it is absolutely needed, then you could simply use redirect to just redirect to the other controller.
Something like:
// some first_cont.php code here
redirect('/second_cont/valuereciever/value1')
// some second_cont.php code here
public function valureciever($value){
echo $value; // will output value1
}
In Codeigniter there are many way to pass the value from one controller to other.
You can use codeigniter Session to pass the data from one controller to another controller.
For that you have to first include the library for session
$this->load->library('session');
Then You can set the flash data value using variable name.
// Set flash data
$this->session->set_flashdata('variable_name', 'Value');
Them you can get the value where you want by using the codeigniter session flashdata
// Get flash data
$this->session->flashdata('variable_name');
Second Option codeigniter allow you to redirect the url from controll with controller name, method name and value and then you can get the value in another controller.
// Passing the value
redirect('/another_controller_name/method_name/variable');
Then you can get the value in another controller
public function method_name($variable)
{
echo $variable;
}
That all....
If you are using session in the first controller then dont unset that session in first controller, instead store the value which you want in the other controller like,
$sess_array = array('value_name1' => 'value1', 'value_name2' => 'value2');
$this->session->set_userdata('session_name', $sess_array);
then reload this session in the other controller as
$session_data= $this->session->userdata('session_name');
$any_var_name = $session_data['value1'];
$any_var_name = $session_data['value2'];
this is how you can pass values from one controller to another....
Stick to sessions where you can. But there's an alternative (for Codeigniter3) that I do not highly recommend. You can also pass the data through the url. You use the url helper and the url segment method in the receiving controller.
sending controller method
redirect("controller2/method/datastring", 'refresh');
receiving controller method
$this->load->helper('url');
$data = $this->uri->segment(3);
This should work for the default url structure. For a url: website.com/controller/method/data
To get controller $this->uri->segment(1)
To get method $this->uri->segment(2)
The limitation of this technique is you can only send strings that are allowed in the url so you cannot use special characters (eg. %#$)

How to get data from controller to view?

I need to pass data from controller to view. I used loop in controller which run more than 1 minute.
for($i=0;$i<$count;$i++){
$peicedata = getdata();
$ar=explode(",",$peicedata);
$data['firstname']=$ar[0];
$data['lastname']=$ar[1];
$data['email']=$ar[2];
$data['website']=$ar[3];
}
above function getdata() take around 10 second to get data back. when data get from getdata() i want to pass that data immidiate to view.
Are you passing the data to a view?
$this->load->view('view', $data);
http://codeigniter.com/user_guide/general/views.html
It seems you use CodeIgniter. You can pass data to template like this:
$this->load->view('show', $data);
show is the template. And please read User Guide for more details.
BTW, if your script runs too slow, you should check your code.

Categories