i'm having issue getting session variable in a custom page like a controller.
For example if i create a controller like myController.php and i execute the #dd of data
$data = session::all();
dd($data);
I get no results.
But if i execute the same code in another page like homeController.php i retrieve everyhing the session have like user id etc. My question is how i can retrieve session variable in another page in laravel like i do in plain php with start_session() ?
Check that your route calling this controller you are working in, is under the web middleware, otherwise the session won't be shared.
Related
I am working on the CodeIgniter project. where I want to access image/pdf file by URL only if the user is logged in otherwise.it should be redirected to an error page like 404.
what I did till now.
I have created a route to call a method in my controller like below.
/*site_url() = http://siteurl.com/assets/pdf_docs/12456.pdf*/
$route['assets/pdf_docs/(:any)'] = 'tracking/pdfTracker/$1';
But it is not calling method in tracking controller.What i am thinking is that i can authenticate session in pdfTracker method like below.
public function pdfTracker($fileName){
if($this->session->userdata('userdata')){
//user can show file
}else{
redirect('show_404');
}
}
If any Query please drop a comment.
I'm trying to code the system in that things are separated using "userId". That means, products or services are separated on the basis that user who add them in the system.
For that I'm trying to pass the userId from user controller to service and product controller. But I got an error that undefined variable userID.
$data['id'] = $this->session->userdata('user_id');
pass this in create() function of department controller.
i'm loading user model in __construct() function also.
Thanks in advance.
You can do it using two ways.
1) By Helper - simple create a custom helper file and create a function inside helper file and inside function add get userid code and then using function name you can access this userid anywhere in whole project.
2) Using By Require Controller Class -- if you want to access user id in another controller so i will explain with you by example.
Ist Controller - Test.php
here i defined a variable as a public and this variable contains userid value.
2nd Controller - Test2.php
here we will require Test.php file
require('Test.php file path');
and then create object and then we can access Test.php file variables and methods inside Test2.php file.
But i will suggest you first way it`s very easy way and multiple advantages.
Hope it will work for you.
I have two Controller such as doc.php, user.php in controller folder and a registration.php in a view folder, which have a registration form.
i want to pass the registration info from registration.php to doc.php controller via user.php.
How can i do this? Need help.
What is your codeigniter version ? 2.x or 3.x ?
Try to use session to save your array.
To initialize the Session class manually in your controller constructor, use this method:
$this->load->library();
Once loaded, the Sessions library object will be available using:
$this->session
You can set you session name, using this code :
$this->session->your_session_name;
Use this to add an item in your session :
$this->session->your_session_name('item_one');
To retrieve your session, use this code :
$this->session->your_session_name;
Check this for CodeIgniter 3.X
Check this for CodeIgniter 2.X
In CodeIgnitor you typically use the session object to pass data between controllers. For example:
// initialise the session object
$this->load->library('session');
// save the array to the session
$this->session->set_userdata('reg_info', $reg_info);
// retrieve the array in the other controller:
$this->session->userdata('reg_info');
More info on CodeIgnitor's session object can be found in the docs.
I have an admin template which I builded with blade templates,including one another.The user request is extending the main blade and returning only the content.
However in the template I have stuff like - user messages(count),theme options etc.
These things must be saved in the user session.Cookies are not an option.
The question is how to do it in the best possible option.
I'm thinking of gettind the request in middleware and accessing the session there.After that I must pass the data to the blade templates (not the final extending one).
Whats your opinion ?! Thanks!
If I understand correctly, you have a main layout blade template that is later extended by the user view returned by the controller.
No additional code like you described is needed. Both user and layout templates are processed after controller action is executed and both have access to user session via session() helper and user object via Auth::user().
So the following sample code should work for you :
// SomeController
public function someAction() {
return return response()->view('user');
}
// main.blade.php
#if (Auth::check())
Show this text only to authenticated users
#endif
Value of session parameter is {{ session('parameter_name') }}
// user.blade.php
#extends('main')
I am using codeigniter and what i want to do is when user logs in, I am storing his first name and last name in session.
i have header file which i include in all the views
i want to show something like this
welcome "amit patil" | Logout
where "amit patil" is coming from session.
How can i access session value and display in header file
I know it can be possible with this
$data['admin_fname'] = $this->session->userdata("first_name");
$this->load->view("header",$data);
I dont want to repeat this process in all the controllers, Is there any easy way ??
You can create an helper to do it.
function getUserFirstName() {
$ci =& get_instance();
return $ci->session->userdata("first_name");
}
And then load the helper with $this->load->helper() in your controller. If something related to your user changes you will need to change only in the helper.