Skip to content

Commit

Permalink
Merge pull request #7 from iMi-digital/fix/tests
Browse files Browse the repository at this point in the history
Refactor appending session url to check if the route we want to go to has the middleware
  • Loading branch information
amenk committed Mar 26, 2021
2 parents cb95077 + 0e182b0 commit c7d1061
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ Installation
2. In your `config/app.php` at `providers` replace
`'Illuminate\Session\SessionServiceProvider'` with `\iMi\LaravelTransSid\SessionServiceProvider::class`
3. Add `\iMi\LaravelTransSid\UrlServiceProvider::class` at the end of the providers array
4. In your `app/Http/Kernel.php` add `'urlsession' => \iMi\LaravelTransSid\UrlSession::class` to the `$routeMiddleware` array.
4. (optional) In your `app/Http/Kernel.php` add `'urlsession' => \iMi\LaravelTransSid\UrlSession::class` to the `$routeMiddleware` array.

Usage
-----

To use SessionIDs in URLs add the middleware `urlsession` to your route or routegroup.
To use SessionIDs in URLs add the middleware `urlsession` (if you registered the middleware globally) or add the
`\iMi\LaravelTransSid\UrlSession::class` class directly to your route or routegroup.

URLs generated with Laravel's URL function (for example `URL::to()`) will now have a session ID appended.

Expand Down
20 changes: 12 additions & 8 deletions src/UrlGeneratorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@

class UrlGeneratorService extends UrlGenerator
{
public function addSid($url)
public function addSid($url, ?\Illuminate\Routing\Route $route = null)
{
// Only apply transsid to routes/routegroups with the urlsession middleware.
if (in_array('urlsession', Route::current()->computedMiddleware)) {
if (strpos($url, \Config::get('session.cookie')) !== false) {
return $url;
}
$sep = (strpos($url, '?') !== false) ? '&' : '?';
$url .= $sep . \Config::get('session.cookie') . '=' . \Session::getId();
if ($route === null || !in_array(UrlSession::class, $route->getAction('middleware'))) {
return $url;
}

if (strpos($url, \Config::get('session.cookie')) !== false) {
return $url;
}

$separator = (strpos($url, '?') !== false) ? '&' : '?';
$url .= $separator . \Config::get('session.cookie') . '=' . \Session::getId();

return $url;
}

Expand All @@ -39,7 +43,7 @@ public function toRoute($route, $parameters, $absolute)
}

$url = parent::toRoute($route, $parameters, $absolute);
$url = $this->addSid($url);
$url = $this->addSid($url, $route);
return $url;
}

Expand Down
4 changes: 1 addition & 3 deletions src/UrlSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ class UrlSession
*/
public function handle($request, Closure $next)
{
ini_set('session.use_trans_sid', 1);
ini_set('session.use_cookies', 0);
ini_set('session.use_only_cookies', 0);
// intentionally left empty - just for marking the requests
return $next($request);
}
}

0 comments on commit c7d1061

Please sign in to comment.