PHP code example of jegex / laravel-seo
1. Go to this page and download the library: Download jegex/laravel-seo library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jegex / laravel-seo example snippets
use Jegex\LaravelSeo\Traits\HasSeo;
class Post extends Model
{
use HasSeo;
}
return [
'site_name' => 'My Blog',
'site_description' => 'A blog about Laravel',
'separator' => ' - ',
'templates' => [
'post' => [
'title' => '%title% %sep% %sitename%',
'description' => '%excerpt%',
],
],
'webmaster_verification' => [
'google' => 'your-google-verification-code',
'bing' => 'your-bing-verification-code',
],
];
// Set SEO data
seo('title', 'My Page Title');
seo('description', 'Page description');
seo('og:image', '/path/to/image.jpg');
// Or use methods
seo()->setTitle('My Title')->setDescription('My Description');
// Parse templates manually
parse_seo_template('%title% %sep% %sitename%', ['title' => 'Hello']);
'webmaster_verification' => [
'google' => 'abc123...',
'bing' => 'xyz789...',
'pinterest' => 'pinterest-code',
'yandex' => 'yandex-code',
],
use Jegex\LaravelSeo\Models\Redirect;
// Create a 301 redirect
Redirect::create([
'from_url' => '/old-page',
'to_url' => '/new-page',
'type' => 301,
]);
// Create a regex redirect
Redirect::create([
'from_url' => '#/blog/(.*)#',
'to_url' => '/articles/$1',
'type' => 301,
'is_regex' => true,
]);
// 410 Gone
Redirect::create([
'from_url' => '/deleted-page',
'type' => 410,
]);
protected $middleware = [
// ...
\Jegex\LaravelSeo\Http\Middleware\RedirectMiddleware::class,
\Jegex\LaravelSeo\Http\Middleware\NotFoundTrackerMiddleware::class,
];
$entry = $post->seoEntry;
// Get full analysis
$analysis = $entry->analyze();
// [
// 'score' => 85,
// 'alerts' => ['Title is slightly long...'],
// 'checks' => [...],
// 'details' => [...]
// ]
// Get score only
$entry->calculateScore();
echo $entry->seo_score; // 85
// Get score label
echo $entry->getScoreLabel(); // 'Good', 'Needs Improvement', or 'Poor'
use Jegex\LaravelSeo\Services\AnalyzerService;
$analyzer = app(AnalyzerService::class);
$analysis = $analyzer->analyze($content, [
'title' => 'My Title',
'description' => 'My Description',
'focus_keyword' => 'laravel seo',
]);
echo $analysis['score']; // 0-100
echo $analysis['alerts'][0]; // First alert message
// Manual breadcrumbs
seo()->breadcrumbs()
->add('Home', '/')
->add('Blog', '/blog')
->add($post->title, $post->url());
// Auto from route
seo()->breadcrumbs()->fromRoute();
// Render
{{ seo()->breadcrumbs()->renderHtml() }}
{{ seo()->breadcrumbs()->renderSchema() }}
// Via SEO service (auto-rendered)
seo()->addSchema('article')
->headline('My Article')
->author('John Doe')
->datePublished(now()->toIso8601String())
->image('https://example.com/image.jpg');
// Via Schema service
seo()->schema()
->website()
->name('My Site')
->url('/')
->potentialActionSearch('/search?q={search_term_string}');
// Organization schema
seo()->schema()
->organization()
->name('My Company')
->url('https://example.com')
->contactPoint('+1-234-567-8900', 'customer service');
class PostController extends Controller
{
public function show(Post $post)
{
// Automatically uses HasSeo trait for defaults
seo()->for($post);
// Or manually override
seo()->setTitle($post->title . ' | Custom Suffix');
return view('posts.show', compact('post'));
}
}
bash
php artisan vendor:publish --tag="laravel-seo-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="laravel-seo-config"
bash
php artisan vendor:publish --tag="laravel-seo-views"