Skip to content

Commit

Permalink
Merge pull request #78 from fyrts/feature/signed-routes
Browse files Browse the repository at this point in the history
Add URL signing for localized routes
  • Loading branch information
chinleung authored Feb 26, 2024
2 parents a769cd0 + 400b0f2 commit 66a7e9b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ Route::multilingual('contact')->redirect('support');
Request::localizedRouteIs('home');
```

### Signing localized routes

```php
URL::signedLocalizedRoute('unsubscribe', ['user' => 1]);
URL::temporarySignedLocalizedRoute('unsubscribe', now()->addMinutes(30), ['user' => 1]);
```

## Upgrading from 1.x to 2.x

To update from 1.x to 2.x, you simply have to rename the namespace occurrences in your application from `LaravelMultilingualRoutes` to `MultilingualRoutes`. The most common use case would be the `DetectRequestLocale` middleware.
Expand Down
44 changes: 44 additions & 0 deletions src/Macros/UrlGeneratorMacros.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace ChinLeung\MultilingualRoutes\Macros;

use Closure;

class UrlGeneratorMacros
{
/**
* Create a temporary signed route URL for a named route in the current
* locale.
*
* @param string $name
* @param \DateTimeInterface|\DateInterval|int $expiration
* @param array $parameters
* @param bool $absolute
* @return string
*/
public function temporarySignedLocalizedRoute(): Closure
{
return function ($name, $expiration, $parameters = [], $absolute = true) {
return $this->signedLocalizedRoute($name, $parameters, $expiration, $absolute);
};
}

/**
* Create a signed route URL for a named route in the current
* locale.
*
* @param string $name
* @param mixed $parameters
* @param \DateTimeInterface|\DateInterval|int|null $expiration
* @param bool $absolute
* @return string
*
* @throws \InvalidArgumentException
*/
public function signedLocalizedRoute()
{
return function ($name, $parameters = [], $expiration = null, $absolute = true) {
return $this->signedRoute(locale().".{$name}", $parameters, $expiration, $absolute);
};
}
}
3 changes: 3 additions & 0 deletions src/MultilingualRoutesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use ChinLeung\MultilingualRoutes\Macros\RedirectorMacros;
use ChinLeung\MultilingualRoutes\Macros\RequestMacros;
use ChinLeung\MultilingualRoutes\Macros\RouterMacros;
use ChinLeung\MultilingualRoutes\Macros\UrlGeneratorMacros;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\ServiceProvider;

Expand All @@ -22,6 +24,7 @@ public function boot(): void
Redirect::mixin(new RedirectorMacros);
Request::mixin(new RequestMacros);
Router::mixin(new RouterMacros);
UrlGenerator::mixin(new UrlGeneratorMacros);

require __DIR__.'/helpers.php';

Expand Down
46 changes: 46 additions & 0 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace ChinLeung\MultilingualRoutes\Tests;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\URL;
use Orchestra\Testbench\TestCase;

class UrlTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

config(['locales.supported' => [
'en', 'fr',
]]);
}

/** @test **/
public function a_multilingual_route_can_be_signed(): void
{
Route::multilingual('test');

$request = Request::create(URL::signedLocalizedRoute('test'));

$this->assertTrue(URL::hasValidSignature($request));
$this->assertTrue(str_starts_with($request->url(), localized_route('test')));
}

/** @test **/
public function a_multilingual_route_can_be_signed_with_temporary_signature(): void
{
Route::multilingual('test');

$request = Request::create(URL::temporarySignedLocalizedRoute('test', now()->addMinutes(30)));

$this->assertTrue(URL::hasValidSignature($request));
$this->assertTrue(str_starts_with($request->url(), localized_route('test')));

$this->travel(5)->hours();

$this->assertFalse(URL::hasValidSignature($request));
}
}

0 comments on commit 66a7e9b

Please sign in to comment.