Skip to content

Commit

Permalink
Add base url (#108)
Browse files Browse the repository at this point in the history
* Add custom base url attribute and its tests

* Add Custom Base URL section
  • Loading branch information
evokelektrique authored May 5, 2024
1 parent 6fbdf89 commit 6e22c4a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ once the link is visited.
- [Http Response](#http-response-action)
- [Controller](#controller-action)
- [Custom Action](#custom-action)
- [Custom Base URL](#custom-base-url)

### Login Action

Expand Down Expand Up @@ -241,6 +242,18 @@ $action = new MyCustomAction('Hello world');
$urlToCustomAction = MagicLink::create($action)->url;
```

### Custom Base URL

To set the base URL for a magic link, you can use the `baseUrl` method. This method ensures that the provided base URL has a trailing slash, making it ready for URL generation.

```php
$magiclink = MagicLink::create($action);

$magiclink->baseUrl("http://example.com");

$urlShowView = $magiclink->url; // http://example.com/magiclink/...
```

## Protect with an access code

Optionally you can protect the resources with an access code.
Expand Down
13 changes: 11 additions & 2 deletions src/MagicLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,19 @@ public function setActionAttribute($value)
: serialize($value);
}

public function getUrlAttribute()
public function baseUrl(?string $baseUrl): self
{
$this->attributes['base_url'] = rtrim($baseUrl, '/') . '/';
return $this;
}

public function getUrlAttribute(): string
{
$baseUrl = rtrim($this->attributes['base_url'] ?? '', '/') . '/'; // Use the stored base_url or an empty string

return url(sprintf(
'%s/%s%s%s',
'%s%s/%s%s%s',
$baseUrl,
config('magiclink.url.validate_path', 'magiclink'),
$this->id,
urlencode(':'),
Expand Down
10 changes: 10 additions & 0 deletions tests/MagicLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,14 @@ public function test_increment_num_visits_exceeded()
{
$this->markTestSkipped();
}

public function test_create_magiclink_with_custom_base_url()
{
$magiclink = MagicLink::create(new LoginAction(User::first()));

$custom_base_url = "http://example.com";
$magiclink->baseUrl($custom_base_url);

$this->assertStringContainsString($custom_base_url, $magiclink->url);
}
}

0 comments on commit 6e22c4a

Please sign in to comment.