banner

Rajesh Saha 2nd Feb 2017

Sessions provide a way to store information across multiple requests. In laravel, the session configuration file is stored in 'app/config/session.php'. First of all, review the options available to you in this file. You will get some important documented information about where or how laravel store or manage session data. If you don’t specified your session driver, then the default session “driver” will be ‘file’. In that case, sessions are stored in storage/framework/sessions.

Retrieving Value from Session

There are 2 ways of working with session data in Laravel: the global session() helper function and via type-hinted Request instance on a controller method.
Via The Request instance
For, Request instance, you need to adduse Illuminate\Http\Request;
https://gist.github.com/manchumahara/7677692f7e34dede3c19db65a456bd0b
2nd argument of the above get() method is the default value which will be returned if your specified key does not exist in the session.

WordPress Custom Development

Wordpress Bespoke Development

We are long time WordPress geek and we mean it. To convince you, our plugin author page in WordPress.org plugin directory is enough. So we are keeping it short and let’s see what type of custom development services we provide for wordpress.[Click here to learn about our WordPress Custom Services]

Via The Global Session Helper:
https://gist.github.com/manchumahara/a216ab3f3d08983547957471d20f3b78

Retrieving All Session Data

If you like to retrieve all the available data in the session, you can use the all method:
https://gist.github.com/manchumahara/d88467c86ef3a3f83754a6c95b0d7429

Checking If an Item Exists In Session

To determine if a value is present in the session, you may use the hasmethod. The hasmethod returns true if the value is present and if doesn’t present then returns null:
https://gist.github.com/manchumahara/1adb1301f49d70d635232e91f42f0223

Storing Data in the Session

To store data in the session, you will typically use the session helper method or the put method of Request instance:
https://gist.github.com/manchumahara/48a91bf9fdd224218d0ce9b22da6dac5

Deleting Data from session

Deleting an item from session
The forget method will remove the specifieditem from the session
https://gist.github.com/manchumahara/d63b3d747e21530b4c0917896461ca98
If you first want to get the item value and then delete that item from the session, you can use the pull method:
https://gist.github.com/manchumahara/98e8bdfd54586edec289802bf1bf6172
The difference between the forget() and the pull() method is that, forget() method will not return the value of the session but pull() method will return it and delete that value from session.

Removing all items from session

If you would like to remove all items from the session, you may use the flush method:
https://gist.github.com/manchumahara/cc8e9766507579162c84dbb0a5ec77cf
Flash Data
Sometimes you may wish to store items in the session only for the next request. You may do so using the flash method. Data stored in the session using this method will only be available during the subsequent HTTP request, and then will be deleted. Flash data is primarily useful for short-lived status messages:
https://gist.github.com/manchumahara/e811e5edee6cda27eaa8af48f97d01fc

Important Note(Session not save problem):

Session will be persisted at the proper end of the request cycle because laravel write session item to the specified driver at the end of request lifecycle.
It will not be saved when quitting the application(e.g. dd() in application development) in the midway or an exception is occurred.

If you need store session item on the above situation or instantly after set the item in session, you need to call save method explicitly after put item in the session.
https://gist.github.com/manchumahara/0ef78a85964cfb1e62a591b2c7e25192

Joomla Custom Development

Joomla! Plugin, Module and Component Development We have more than 100 extensions approved in official Joomla extension directory, which makes us one of the leading Joomla extension developer in the world. We are expert in developing system plugins, module and components. [Click here to learn about our Joomla Extension Services]