I'm developing a laravel website, which contains multi-page forms, so there are many session keys set and get in my code, and it's hard to track and maintain.
For example:
// there are many keys in middlewares, livewire components, requests, tests, etc.
session('trip');
session('route');
session('hotel');
session('traffic');
session('restaurant');
session('agreement');
//some stores multidimensional array, so there are keys like this:
session('trip.days.luggage');
session('route.maps');
session('hotel.days.room.special_request');
session('traffic.airplane.departure.delays');
session('restaurant.days.dinner.main_course.meat');
session('agreement.insurance.reciprocal_healthcare');
I've tried to use config, so I can easily to see all keys that has been used:
// config/session-keys.php
return [
'trip' => 'trip',
'trip_days_luggage' => 'trip.days.luggage',
'route' => 'route',
'route_maps' => 'route.maps',
'hotel' => 'hotel',
'hotel_days_room_has_booked' => 'hotel.days.room.has_booked'
'hotel_days_room_special_request' => 'hotel.days.room.special_request'
// etc
];
So I use this in my code:
//get session
session(config('session-keys.trip'));
session(config('session-keys.trip_days_luggage'));
session(config('session-keys.route'));
session(config('session-keys.route_maps'));
session(config('session-keys.hotel'));
session(config('session-keys.hotel_days_room_has_booked'));
session(config('session-keys.hotel_days_room_special_request'));
//set session
session([config('session-keys.trip') => $value]);
session([config('session-keys.trip_days_luggage') => $value]);
session([config('session-keys.route') => $value]);
session([config('session-keys.route_maps') => $value]);
session([config('session-keys.hotel') => $value]);
session([config('session-keys.hotel_days_room_has_booked') => $value]);
session([config('session-keys.hotel_days_room_special_request') => $value]);
then I wrote helpers:
// app/helpers.php
if (!function_exists('get_session')) {
function get_session($key)
{
return session(config('session-keys.' . $key));
}
}
if (!function_exists('set_session')) {
function set_session($key, $value)
{
return session([config('session-keys.' . $key) => $value]);
}
}
so it became like this:
//get session
get_session('trip');
get_session('trip_days_luggage');
get_session('route');
get_session('route_maps');
get_session('hotel');
get_session('hotel_days_room_has_booked');
get_session('hotel_days_room_special_request');
//set session
set_session('trip', $value);
set_session('trip_days_luggage', $value);
set_session('route', $value);
set_session('route_maps', $value);
set_session('hotel', $value);
set_session('hotel_days_room_has_booked', $value);
set_session('hotel_days_room_special_request', $value);
But it doesn't look good and maybe there are hidden problems.
If there is a tool to parse every session()
in my code and look for keys, and use a command like php artisan session-key:list
to show all keys. Or a better program design. Please share any ideas, thank you!
get_session
misses an .
here 'session-keys' . $key