How to use session_data in codeigniter throughout Website without including in particular view - codeigniter

Is there any other best way to use session_data in website.
How i set session in my project:
$sess_array = array('id' => $row->user_id,'name'=>$row->user_name,'email'=>$row->email,'condition'=>'','balance'=>$row->balance,'did_alloted'=>$row->did_alloted,'create_date'=>$row->create_date);
$this->session->set_userdata('logged_in', $sess_array);
when it comes to controller:
$data['id'] = $session_data['id'];
$data['name'] = $session_data['name'];
$data['email'] = $session_data['email'];
$data['balance']=$session_data['balance'];
$data['did_alloted']=$session_data['did_alloted'];
$data['create_date']=$session_data['create_date'];
$this->load->view('san-reception', $data);
and in my view. I use <?php echo $name ?> to get session_data.
So is there any method by which i can directly access session_data in view without including as $data.

$this->session->userdata('logged_in'); will be available directly in views and hence you just need to assigned this to a variable and then use that as array like bellow.
$userdata=$this->session->userdata('logged_in');
now variable $userdata contain all array field that you set in controller and hence you can use it as $userdata['id'], $userdata['name'] etc

public function __construct()
{
parent::__construct();
$data['session_data']=$this->session->user_data('logged_in');
}
public function some_function(){
$data['dummy']="";
$this->load->view('someview3',$data)
}
public function some_function1(){
$data['dummy']="";
$this->load->view('someview1',$data)
}
public function some_function2(){
$data['dummy']="";
$this->load->view('someview2',$data)
}
If you assign that into the constructor then it will call by automatically whenever a function calls in the controller.
you can view it in you view page by,
print_r($session_data);

Related

Pass a variable on multiple views

I need to pass a variable on multiple views in order to perform an UPDATE on multiple views ( editScadenza and elaborazioneScadenza).
Or do you know another simpler solution?
ScadenzaController.php
public function edit($id)
{
$data['scadenzaRecuperata'] = \App\Scadenza::find($id);
return view('scadenze.editScadenza', $data);
}
UPDATE
public function update(Request $request, $id)
{
$this->validate($request,[
'titolo'=>'required',
'termine_stimato'=>'required',
'responsabile'=>'required',
'tipologia_id'=>'required',
'giorni_avviso'=>'required',
],
[
'titolo.required'=>'Il titolo é obbligatorio',
'termine_stimato.required' => 'Il termine stimato é obbligatoria',
'responsabile.required' => 'Il responsabile é obbligatorio',
'tipologia_id.required' => 'Il tipo é obbligatorio',
'giorni_avviso.required'=> 'I giorni di avviso sono obbligatori',
]);
$scadenza = \App\Scadenza::find($id);
$now = Carbon::now();
$end = Carbon::parse($scadenza->termine_stimato);
$length = $end->diffInDays($now);
$scadenza->titolo = $request->input('titolo');
$scadenza->termine_stimato = date_create($request->input('termine_stimato'))->format('Y-m-d H:i');
$scadenza->responsabile = $request->input('responsabile');
$scadenza->tipologia_id = $request->input('tipologia_id');
$scadenza->processo_id = $request->input('processo_id');
$scadenza->stato = $request->input('stato');
$scadenza->giorni_avviso = $request->input('giorni_avviso');
$scadenza->osservazioni = $request->input('osservazioni');
$scadenza->save();
return redirect('scadenza');
}
The best recommended solution is to share variables from the controller function. If you need it just for 2, 3 views and all comes under the same controller then share it as usual:
public function function_nameX($id)
{
..
return view('scadenze.viewNameX')->withData($data);
}
...
public function function_nameY()
{
..
return view('scadenze.viewNameY')->withData($data);
}
Do it even if you have views returned from multiple controllers. Because this is the most convenient way.
To share a variable with all views in your project, share it from AppServiceProvider's boot() function like:
public function boot()
{
$data['scadenzaRecuperata'] = \App\Scadenza::find($id);
View::share('data', $data);
}
View Composer also help to bind specific data to view in different
ways. You can directly bind variable to specific view or to all views.
For Example you can create your own directory to store your view
composer file according to requirement. and these view composer file
through Service provide interact with view.
Here is the doc.
To share data with views you can set a view Composer
in app/Providers/AppServiceProvider.php into boot() method
public function boot() {
view()->composer('scadenze.editScadenza', function($view) {
$data = \App\Scadenza::find(request()->id);
$view->with('data', $data);
});
}
For more date, see Laravel View Composers
if you need to share only with one: composer('VIEW_NAME', ...)
If you need to share the data with more than one: composer(['VIEW_NAME_1', 'VIEW_NAME_2'], ...)
If you need to share with all views: composer('*', ...)

How to pass variable with a master layout in Codeiginiter

My master layout here's below and working fine. I just want little bit more passing a default variable with this master layout that I can get in every pages.
class MY_Controller extends CI_Controller {
public $layout;
function __construct() {
parent::__construct();
$this->layout='layout/master';
}
}
I need to pass variable like below:
function __construct() {
parent::__construct();
$data['msg'] = $this->session->flashdata('usermsg');
$this->layout=('layout/master',$data);
}
How do I get this.
If you are loading up the data dynamically from the controller with the help of $this->layout you can send the data like this.
Method 1:
If you are using the general method to load the data to the view you can use this method.
$this->load->view('profile_view', $data);
This will load the profile_view page along with the $data as you passs into it with the help of array()
Method 2:
If you have created a master Layout and you are passing the data from the controller to the Master Layout you need to do like this.
<?php
public function master_layout () {
$this->template['header'] = $this->load->view('include/header', $this->Front_End_data, true);
$this->template['navigation'] = $this->load->view('include/navigation', $this->Front_End_data, true);
$this->template['center'] = $this->load->view($this->middle, $this->Front_End_data, true);
$this->template['footer'] = $this->load->view('include/footer', $this->Front_End_data, true);
$this->load->view('include/index', $this->template);
?>
In this code the below line alone will be loaded dynamically based on the page which you call in the master Layout.
$this->template['center'] = $this->load->view($this->middle, $this->Front_End_data, true);
In order to pass the data to this center layout you can use the funciton like this.
$data['msg'] = 'Success';
$this->template['center'] = $this->load->view ($this->middle = 'pages/view_oage',$data, true);
$this->master_layout();
And in the page you can get the data to be printed using the foreach loop as follows.
foreach($msg as $value)
{
echo $value;
}

Passing values from Controller to Controller

I am new to laravel and I'm trying to pass a value from one controller to another controller.. In this case.. I want to pass the order_id generated in Order Controller to /pizza/create in Pizza Controller..
this is my code in OrderController
public function store()
{
$user = User::find(Auth::user()->id);
$order= new Order;
$order->user()->associate($user);
$order->status = 'unconfirmed';
$order->save();
return Redirect::to('/pizza/create')
->with('order', $order->order_id);
}
this is my code in /pizza/create in PizzaController
public function create()
{
$id = Session::get('order');
$order = Order::find($id);
return View::make('pizza.create')
->with('order', $order);
}
this somehow works.. but the value (order_id) disappears when i change views/routes..
if you want to pass a value from controller to controller use session, when you are using redirect and with method it is creating session, when you are using view and with method it is creating variable which you can use in view to display that value.
this create a session
return Redirect::to('/pizza/create')
->with('order', $order->order_id);
this statement create a variable which you can use in view
return View::make('pizza.create')
->with('order', $order);
above variable you can use in blade or view file
{{ $order }}
so in create method you need to create a session keep variable alive and available in other controllers
public function create()
{
$id = Session::get('order');
$order = Order::find($id);
Session::put('order',$id);
return View::make('pizza.create')
->with('order', $order);
}

codeigniter: using uri segment in model

I am using uri segment to delete info in my database:
anchor('site/delete_note/'.$row->id, 'Delete')
Model:
function delete_note()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');
}
It works fine, but I want to do the same for updating my info and can't get it work
So this is link in view:
anchor('site/edit_note/'.$row->id, 'Edit')
My controller:
function edit_note()
{
$note_id = $this->uri->segment(3);
$data['main_content'] = 'edit_note';
$this->load->view('includes/template', $data);
$this->load->library('form_validation');
$this->form_validation->set_rules('content', 'Message', 'trim|required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('Note_model');
$this->Note_model->edit_note($note_id);
redirect('site/members_area');
}
}
My model:
function edit_note($note_id)
{
$content = $this->input->post('content');
$data = array('content' => $content);
$this->db->where('id', $note_id);
$this->db->update('note', $data);
}
My view of edit_note:
<?php
echo form_open('site/edit_note');
echo form_textarea('content', set_value('content', 'Your message'));
echo form_submit('submit', 'Change');
echo anchor('site/members_area', 'Cancel');
echo validation_errors('<p class="error">'); ?>
Edit doesn't work as delete, when i am trying to get segment directly in edit model, as I used in delete model.
If I set $note_id to a number in my controller, instead of this '$this->uri->segment(3)', it updates my database. But if I use getting segment it doesn't work. I thought uri segments are available in controller as in model, but there is something I don't know.
Better yet, instead of manually reading the IDs via the segments, you could change your functions to be:
function delete_note($note_id)
and
function edit_note($note_id)
And remove the $note_id = $this->uri->segment(3); lines.
And as silly as it'll sound, the generated URL is definitely correct, right?
And last question, have you done anything with routes?
Edit
I've also noticed that in edit, you use this in your form:
echo form_open('site/edit_note');
So when the form submits, the URL it submits to is site/edit_note instead of site/edit_note/{SOME_ID}. So once you make your changes, and the form submits, there won't be a 3rd URL segment!
Well there are some logical errors in your code.
function edit_note()
{
$note_id = $this->uri->segment(3);
$data['main_content'] = 'edit_note';
$this->load->view('includes/template', $data);
//// What is the use of loadig a view when you are editing
$this->load->library('form_validation');
$this->form_validation->set_rules('content', 'Message', 'trim|required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('Note_model');
$this->Note_model->edit_note($note_id);
redirect('site/members_area');
}
}
Instead do it like this
function edit_note()
{
if($this->input->post()){
$this->load->library('form_validation');
$this->form_validation->set_rules('content', 'Message', 'trim|required');
if($this->form_validation->run() == TRUE)
{
$this->load->model('Note_model');
$this->Note_model->edit_note($note_id);
redirect('site/members_area');
}
}else{
$note_id = $this->uri->segment(3);
$data['main_content'] = 'edit_note';
$this->load->view('includes/template', $data);
}
}
MOST IMPORTANT
And the other thing you should note that you are using anchor to access edit note but not actually submitting a form so it is not getting any post data to update.
In my view it's a 'bad' approach to use uri segments in your models... you should pass an id as a parameter from your controller functions ..
function delete_note()
{
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');
}
What if you want to re-use this delete method? e.g. deleting notes from an admin panel, via a cron job etc then the above relies upon the uri segment and you will need to create additional delete methods to do the job. Also, if you were to continue with the same you don't even need a model then .. just call these lines in your controllers if you know what I mean ...
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('note');
so best is to change it to similar to your edit_note() model function.

How to change the value of a variable inside a controller from a view file (html)?

I followed this tutorial: http://codeigniter.com/wiki/Internationalization_and_the_Template_Parser_Class/
The controller that loads the language is this one:
<?php
class Example extends Controller {
function Example() {
parent::Controller();
# Load libraries
$this->load->library('parser');
# Load language
$this->lang->load('example', 'english');
}
function index() {
# Load variables into the template parser
$data = $this->lang->language;
# Display view
$this->parser->parse('example', $data);
}
}
?>
In order to change the language I have to manually change english to say spanish in the controller.
What's the best way the user can do this from the index.php file (view)?
The best thing to do is have the user select a supported language on some page, set it as a session variable and call it when ever you need to load a language
$language = $this->session->userdata("language");
$this->lang->load("example", $language);
$data = $this->lang->language;
$this->parser->parse("example", $data);
EDITED BELOW
If you're using CodeIgniter and you're new to this, I wouldn't suggest messing with the index.php file.
You want to do it inside your controller by loading a form where they can pick their language and storing it in the session. I'd also suggest autoloading your session library.
The controller:
<?php
class Home extends Controller {
function Home()
{
parent::Controller();
$this->load->library("session");
}
function index()
{
$language = $this->session->userdata("language");
$this->lang->load("example", $language);
$data = $this->lang->language;
$this->parser->parse("example", $data);
}
function set_lang()
{
if( ! $this->form_validation->run())
{
$this->load->view("select_language_form");
}
else
{
$language = $this->input->post('language', TRUE);
$this->session->set_userdata('language', $language);
redirect('home' 'location');
}
}
}

Resources