Understanding Laravel Session Tags and the @session Directive

Introduction
One of the things that every Laravel developer can’t run away from is the use of sessions which is part of Laravel as a framework. The has introduced a directive called @session directive has now proven to be a game changer, constantly evolving to enhance the developer experience and streamline code readability.
A recent addition to Laravel’s feature set is the Blade @session directive, introduced through a pull request (PR) in version 10.x.
The @session Directive
Why session?
Bring a cleaner syntax and improved usability to Blade views when dealing with sessions. The directive mirrors the concept of the `@error` Blade directive, offering a cleaner approach to session-related code within views.
Benefits and Changes
The key advantages of the @session directive include:
1. Cleaner Code: Simplifies the syntax for working with session data in Blade views. 2. Variable Access: Introduces a `$session` template variable within the directive for better control.
However, it's essential to note that there are breaking changes only if someone is using a custom @session directive before. The usage is straightforward, as illustrated in the examples below.
Before:
@if(session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endifAfter:
@session('status')
<div class="alert alert-success" role="alert">
{{ $value }}
</div>
@endsessionNested Calls
A noteworthy aspect raised during the PR discussion was handling nested @session calls. If you had nested @session calls, the challenge was ensuring proper restoration of the `$sessionOriginal` variable.
Nested Example:
@session('first')
@session('second')
@session('third')
<div class="alert alert-success" role="alert">
{{ $session }}
</div>
@endsession
<div class="alert alert-success" role="alert">
{{ $session }}
</div>
@endsession
<div class="alert alert-success" role="alert">
{{ $session }}
</div>
@endsessionDebugging and Refactoring
To address the issue of nested calls, The implementation can be refactored to use an array (`$sessionPrevious`) instead of a string. This allowed tracking the previous session in a nested call effectively.
Refactored Example:
@php
$sessionKeys = ['first', 'second', 'third'];
@endphp
@foreach ($sessionPrevious as $key)
@session($key)
<div class="alert alert-success" role="alert">
{{ $session }}
</div>
@endsession
@endforeachWith this refactoring, the issue of `$sessionOriginal` being from the previous layer's session in nested @session calls was resolved.
Conclusion
The @session directive in Laravel's Blade offers a cleaner syntax and improved readability when dealing with session data in views. The careful consideration of nested calls and the subsequent refactoring demonstrate the Laravel community's commitment to maintaining code integrity and developer-friendly features. As always, incorporating such improvements into your Laravel projects can enhance the overall development experience.
Stay tuned!!! I will be back with some more cool Laravel tutorials in the next article. I hope you liked the article. Don’t forget to follow me 😇 and give some claps 👏. And if you have any questions feel free to comment. Thank you.
Thanks a lot for reading till the end. Follow or contact me via: Email: [email protected] LinkedIn: https://www.linkedin.com/in/reuben-shumba-a72aaa157/ BuyMeCoffee: https://www.buymeacoffee.com/reubenshumba REUBEN SHUMBA
