Skip to content

Commit ae890d7

Browse files
PHP updates (#3553)
* Fix PHP getLastEventId example * Move old Laravel instructions to the bottom of the page * Explain how to use configureScope vs. withScope * Add missing unset-user PHP snippet * Update set-level PHP snippet with realistic code * Add sensitive data PHP snippets * Update src/includes/set-context/php.mdx Co-authored-by: Fiona <[email protected]> Co-authored-by: Fiona <[email protected]>
1 parent 46e6df4 commit ae890d7

File tree

7 files changed

+70
-31
lines changed

7 files changed

+70
-31
lines changed

src/includes/enriching-events/set-context/php.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
The `configureScope` helper will setup the scope for all events being captured by the Sentry SDK.
2+
13
```php
24
\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
35
$scope->setContext('character', [
@@ -7,3 +9,18 @@
79
]);
810
});
911
```
12+
13+
If you need to modify the scope for a single event, you can use the `withScope` helper instead, which does not keep the scope changes made.
14+
15+
```php
16+
\Sentry\withScope(function (\Sentry\State\Scope $scope) use ($e): void {
17+
$scope->setContext('character', [
18+
'name' => 'Mighty Fighter',
19+
'age' => 19,
20+
'attack_type' => 'melee'
21+
]);
22+
23+
\Sentry\captureMessage('The fighter is out of energy!');
24+
// or: \Sentry\captureException($e);
25+
});
26+
```

src/includes/enriching-events/user-feedback/example-widget/php.laravel.mdx

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
1-
For Laravel 5 up to 5.4 you need to open up `App/Exceptions/Handler.php` and extend the
2-
`render` method to make sure the 500 error is rendered as a view correctly, in 5.5+ this
3-
step is not required anymore and you can skip ahead to the next one:
1+
Make sure you've got the JavaScript SDK available:
2+
3+
```html
4+
<script
5+
src="https://browser.sentry-cdn.com/{{ packages.version('sentry.javascript.browser') }}/bundle.min.js"
6+
integrity="sha384-{{ packages.checksum('sentry.javascript.browser', 'bundle.min.js', 'sha384-base64') }}"
7+
crossorigin="anonymous"
8+
></script>
9+
```
10+
11+
Next, create `resources/views/errors/500.blade.php`, and embed the feedback code:
12+
13+
```html {filename:resources/views/errors/500.blade.php}
14+
<div class="content">
15+
<div class="title">Something went wrong.</div>
16+
17+
@if(app()->bound('sentry') && app('sentry')->getLastEventId())
18+
<div class="subtitle">Error ID: {{ app('sentry')->getLastEventId() }}</div>
19+
<script>
20+
Sentry.init({ dsn: '___PUBLIC_DSN___' });
21+
Sentry.showReportDialog({
22+
eventId: '{{ app('sentry')->getLastEventId() }}'
23+
});
24+
</script>
25+
@endif
26+
</div>
27+
```
28+
29+
For Laravel 5 up to 5.4 there is some extra work needed. You need to open up `App/Exceptions/Handler.php` and extend the
30+
`render` method to make sure the 500 error is rendered as a view correctly, in 5.5+ this step is not required anymore.
431

532
```php {filename:app/Exceptions/Handler.php}
633
<?php
@@ -33,29 +60,3 @@ class Handler extends ExceptionHandler
3360
}
3461
}
3562
```
36-
37-
Make sure you've got the JavaScript SDK available:
38-
39-
```html
40-
<script
41-
src="https://browser.sentry-cdn.com/{{ packages.version('sentry.javascript.browser') }}/bundle.min.js"
42-
integrity="sha384-{{ packages.checksum('sentry.javascript.browser', 'bundle.min.js', 'sha384-base64') }}"
43-
crossorigin="anonymous"
44-
></script>
45-
```
46-
47-
Next, create `resources/views/errors/500.blade.php`, and embed the feedback code:
48-
49-
```html {filename:resources/views/errors/500.blade.php}
50-
<div class="content">
51-
<div class="title">Something went wrong.</div>
52-
53-
@if(app()->bound('sentry') && app('sentry')->getLastEventId())
54-
<div class="subtitle">Error ID: {{ app('sentry')->getLastEventId() }}</div>
55-
<script>
56-
Sentry.init({ dsn: '___PUBLIC_DSN___' });
57-
Sentry.showReportDialog({ eventId: '{{ app('sentry')->getLastEventId() }}' });
58-
</script>
59-
@endif
60-
</div>
61-
```

src/includes/enriching-events/user-feedback/example-widget/php.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Make sure you've got the JavaScript SDK available:
1717
Depending on how you render your templates, the example would be in a simple php file:
1818

1919
```html
20-
<?php if (Sentry\State\Hub::getCurrent()->getLastEventId()) { ?>
20+
<?php if (\Sentry\SentrySdk::getCurrentHub()->getLastEventId()) { ?>
2121
<script>
2222
Sentry.init({ dsn: "___PUBLIC_DSN___" });
2323
Sentry.showReportDialog({
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
```php
2+
\Sentry\configureScope(function (\Sentry\State\Scope $scope) use ($user): void {
3+
$scope->setTag('birthday', checksum_or_hash($user->birthday));
4+
});
5+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```php
2+
\Sentry\configureScope(function (\Sentry\State\Scope $scope) use ($user): void {
3+
$scope->setUser(['id' => $user->id]);
4+
5+
// or
6+
7+
$scope->setUser(['username' => $user->username]);
8+
});
9+
```

src/includes/set-level/php.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
```php
2-
\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
2+
\Sentry\withScope(function (\Sentry\State\Scope $scope): void {
33
$scope->setLevel(\Sentry\Severity::warning());
4+
5+
\Sentry\captureMessage('This is now a warning!');
46
});
57
```

src/includes/unset-user/php.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
```php
2+
\Sentry\configureScope(function (\Sentry\State\Scope $scope): void {
3+
$scope->removeUser();
4+
});
5+
```

0 commit comments

Comments
 (0)