Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Bot detection #3

Merged
merged 4 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ and the `x-scripts` tag at the end of the body.
```html
<!DOCTYPE html>
<html lang="en">
<head>
...
<head>
...

<x-styles />
</head>
<body>
...
<x-styles />
</head>
<body>
...

<x-scripts />
</body>
<x-scripts />
</body>
</html>
```

Expand Down Expand Up @@ -262,3 +262,15 @@ $form->route('route_field')->collection('app')->title('Pick a route.');
</span>
@endforeach
```

### (Google)-Bot detection

For bot-detection in your templates use the custom Blade-If:

```php
@bot
i'm a bot
@else
i'm human
@endbot
```
16 changes: 16 additions & 0 deletions src/BladesmithServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function register()
$this->loadViewsFrom(__DIR__.'/../views', 'bladesmith');

$this->registerBladeComponents();

$this->registerBladeIfs();

$this->registerPublishes();

Expand Down Expand Up @@ -73,6 +75,20 @@ protected function registerBladeComponents()
}
}

/**
* Register blade ifs.
*
* @return void
*/
protected function registerBladeIfs()
{
// Credits: https://stackoverflow.com/questions/677419/how-to-detect-search-engine-bots-with-php
Blade::if('bot', function () {
return isset($_SERVER['HTTP_USER_AGENT'])
&& preg_match('/bot|crawl|slurp|spider|mediapartners/i', $_SERVER['HTTP_USER_AGENT']);
});
}

/**
* Register publishes.
*
Expand Down