Skip to content

Commit 716335c

Browse files
authored
Merge pull request #16 from iMi-digital/15-livewire
#15 bind RouteCollectionInterface to fix livewire
2 parents 0220f52 + f72e96c commit 716335c

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ If you would like to generate URLs without a session ID, add a `NO_ADD_SID` para
3535

3636
{{ URL::to('/', ['NO_ADD_SID' => true]) }}
3737

38+
Livewire Support
39+
----------------
40+
To include session ids when using Livewire, add this script to your page:
41+
42+
<script type="text/javascript">
43+
document.addEventListener('livewire:load', function(event) {
44+
Livewire.addHeaders({
45+
'X-session': '{{ Session::getId() }}',
46+
})
47+
});
48+
</script>
49+
3850
Warning
3951
-------
4052

src/StartSessionMiddleware.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public function getSession(\Illuminate\Http\Request $request)
4949
{
5050
$session = parent::getSession($request);
5151

52-
if ($request->has($session->getName())) {
53-
$session->setId($request->input($session->getName()));
52+
if ($id=$this->resolveSessionParameter($request, $session)) {
53+
$session->setId($id);
5454

5555
if (!$session->has(self::LOCKED_FIELD)) {
5656
$this->lockToUser($session, $request);
@@ -76,4 +76,14 @@ protected function addCookieToResponse(Response $response, Session $session)
7676

7777
parent::addCookieToResponse($response, $session);
7878
}
79+
80+
protected function resolveSessionParameter($request, $session)
81+
{
82+
if ($request->has($session->getName())) {
83+
return $request->input($session->getName());
84+
}
85+
if ($request->hasHeader('x-session')) {
86+
return $request->header('x-session');
87+
}
88+
}
7989
}

src/UrlServiceProvider.php

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace iMi\LaravelTransSid;
44

55
use Illuminate\Foundation\Application;
6+
use Illuminate\Routing\RouteCollection;
7+
use Illuminate\Routing\RouteCollectionInterface;
68
use Illuminate\Routing\UrlGenerator;
79
use Illuminate\Support\ServiceProvider;
810

@@ -17,6 +19,9 @@ public function registerUrlGenerator()
1719
{
1820
$this->app->bind(UrlGenerator::class, UrlGeneratorService::class);
1921

22+
// explicitly bind RouteCollectionInterface to avoid problems with LiveWire
23+
$this->app->bind(RouteCollectionInterface::class, RouteCollection::class);
24+
2025
$this->app->singleton('url', function (Application $app) {
2126
$routes = $app['router']->getRoutes();
2227

0 commit comments

Comments
 (0)