PHP code example of vormkracht10 / laravel-redirects
1. Go to this page and download the library: Download vormkracht10/laravel-redirects 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/ */
vormkracht10 / laravel-redirects example snippets
return [
/*
* Available HTTP status codes for redirection.
* Uncomment additional codes as needed.
*/
'status_codes' => [
301 => 'Moved Permanently', // Permanent redirect, cached by browsers
302 => 'Found', // Temporary redirect, not cached
307 => 'Temporary Redirect', // Temporary, maintains HTTP method
308 => 'Permanent Redirect', // Permanent, maintains HTTP method
],
/*
* The model to use for managing redirects.
* Override this to use your own custom model.
*/
'model' => Backstage\Redirects\Laravel\Models\Redirect::class,
/*
* Default status code for new redirects.
* Can be overridden via REDIRECT_DEFAULT_STATUS_CODE env variable.
*/
'default_status_code' => env('REDIRECT_DEFAULT_STATUS_CODE', 301),
/*
* Case sensitivity for URL matching.
*
* false: /example and /Example are treated as the same
* true: /example and /Example are treated as different URLs
*/
'case_sensitive' => env('REDIRECT_CASE_SENSITIVE', false),
/*
* Trailing slash sensitivity for URL matching.
*
* false: /example and /example/ are treated as the same
* true: /example and /example/ are treated as different URLs
*/
'trailing_slash_sensitive' => env('REDIRECT_TRAILING_SLASH_SENSITIVE', false),
/*
* Add trailing slash to redirect destinations.
* Useful for maintaining URL consistency and SEO.
*/
'trailing_slash' => env('REDIRECT_WITH_TRAILING_SLASH', false),
/*
* Middleware stack for handling redirects.
* Order matters - first match wins.
*
* - HttpRedirects: Matches URLs with protocol/www variations
* - WildRedirects: Partial URL matching (contains)
* - StrictRedirects: Exact URL matching
*/
'middleware' => [
Backstage\Redirects\Laravel\Http\Middleware\HttpRedirects::class,
Backstage\Redirects\Laravel\Http\Middleware\WildRedirects::class,
Backstage\Redirects\Laravel\Http\Middleware\StrictRedirects::class,
],
];
use Backstage\Redirects\Laravel\Events\UrlHasChanged;
// When a blog post URL changes
event(new UrlHasChanged(
oldUrl: 'https://example.com/old-slug',
newUrl: 'https://example.com/new-slug',
code: 301
));
// Created automatically by the listener
Redirect::create([
'source' => 'https://example.com/old-slug',
'destination' => 'https://example.com/new-slug',
'code' => 301,
]);
use Backstage\Redirects\Laravel\Events\UrlHasChanged;
use Illuminate\Database\Eloquent\Model;
class BlogPost extends Model
{
protected static function booted()
{
static::updating(function ($post) {
if ($post->isDirty('slug')) {
$oldUrl = route('blog.show', $post->getOriginal('slug'));
$newUrl = route('blog.show', $post->slug);
event(new UrlHasChanged($oldUrl, $newUrl, 301));
}
});
}
}
namespace App\Models;
use Backstage\Redirects\Laravel\Models\Redirect as BaseRedirect;
class Redirect extends BaseRedirect
{
// Add custom scopes
public function scopeActive($query)
{
return $query->where('active', true);
}
// Add relationships
public function user()
{
return $this->belongsTo(User::class);
}
// Override redirect logic
public function redirect(Request $request): ?RedirectResponse
{
// Custom logic before redirect
\Log::info("Redirecting from {$this->source} to {$this->destination}");
return parent::redirect($request);
}
}
// Perform the redirect
public function redirect(Request $request): ?RedirectResponse
// Increment hits counter (called automatically)
public function increment('hits'): void
use Backstage\Redirects\Laravel\Events\UrlHasChanged;
event(new UrlHasChanged(
oldUrl: 'https://example.com/old',
newUrl: 'https://example.com/new',
code: 301 // Optional, defaults to 301
));