PHP code example of spatie / laravel-missing-page-redirector

1. Go to this page and download the library: Download spatie/laravel-missing-page-redirector 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/ */

    

spatie / laravel-missing-page-redirector example snippets


// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->append([
        \Spatie\MissingPageRedirector\RedirectsMissingPages::class,
    ]);
})

php artisan vendor:publish --provider="Spatie\MissingPageRedirector\MissingPageRedirectorServiceProvider"

return [
    /*
     * This is the class responsible for providing the URLs which must be redirected.
     * The only Redirector\Redirector\ConfigurationRedirector::class,
    
    /*
     * By default the package will only redirect 404s. If you want to redirect on other
     * response codes, just add them to the array. Leave the array empty to redirect
     * always no matter what the response code.
     */
    'redirect_status_codes' => [
        \Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND
    ],
    
    /*
     * When using the `ConfigurationRedirector` you can specify the redirects in this array.
     * You can use Laravel's route parameters here.
     */
    'redirects' => [
//        '/non-existing-page' => '/existing-page',
//        '/old-blog/{url}' => '/new-blog/{url}',
    ],

];

'redirects' => [
   '/non-existing-page' => '/existing-page',
],

    'redirects' => [
       '/old-blog/{url}' => '/new-blog/{url}',
    ],

    'redirects' => [
       '/old-blog/{url?}' => '/new-blog/{url}',
    ],

    'redirects' => [
       '/old-blog/*' => '/new-blog/{wildcard}', // {wilcard} will be the entire path
    ],

    'redirect_status_codes' => [\Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND],

    'redirects' => [
       'old-page' => ['/new-page', 302],
    ],

namespace Spatie\MissingPageRedirector\Redirector;

use Symfony\Component\HttpFoundation\Request;

interface Redirector
{
    public function getRedirectsFor(Request $request): array;
}


use Spatie\MissingPageRedirector\MissingPageRouter;
//... Your other route

Route::fallback(function (Request $request) {
    $redirectResponse = app(MissingPageRouter::class)->getRedirectFor($request);

    if ($redirectResponse !== null) {
        return $redirectResponse;
    }
    //... Your other logic
});