- Upgrade to Statamic 3.0.0-beta.13
- Copy the contents of this gist's
console.php
intoroutes/console.php
- Run
php artisan route:convert
- Delete the
routes
,vanity
, andredirect
arrays fromconfig/statamic/routes.php
- Delete the command you just copied. It was only temporary.
-
-
Save jasonvarga/6cccccf9eb94729629936ab8a99292f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Artisan::command('route:convert', function () { | |
$routes = collect(config('statamic.routes.routes')) | |
->map(function ($route) { | |
if (!is_array($route)) $route = ['template' => $route]; | |
return $route; | |
}) | |
->map(function ($route, $uri) { | |
if (! $view = array_pull($route, 'template')) { | |
throw new \Exception("Route [$uri] doesnt have a template"); | |
} | |
$amp = array_pull($route, 'amp', false) ? 'amp' : 'not-amp'; | |
$str = "Route::statamic('$uri', '$view'"; | |
if (!empty($route)) { | |
$str .= ', ' . \Symfony\Component\VarExporter\VarExporter::export($route); | |
} | |
$str .= ');'; | |
return compact('str', 'amp'); | |
}) | |
->groupBy->amp; | |
$str = collect($routes['not-amp'] ?? [])->map->str->join("\n"); | |
if ($amp = $routes['amp'] ?? null) { | |
$str .= "\n\nRoute::amp(function () {\n"; | |
$inner = $amp->map->str->join("\n"); | |
$str .= collect(explode("\n", $inner))->map(function ($line) { | |
return ' ' . $line; | |
})->join("\n"); | |
$str .= "\n});"; | |
} | |
$str .= "\n\n"; | |
$str .= collect(config('statamic.routes.vanity'))->map(function ($to, $from) { | |
return "Route::redirect('$from', '$to');"; | |
})->join("\n"); | |
$str .= "\n\n"; | |
$str .= collect(config('statamic.routes.redirect'))->map(function ($to, $from) { | |
return "Route::permanentRedirect('$from', '$to');"; | |
})->join("\n"); | |
$str = rtrim($str) . "\n"; | |
$routesFile = base_path('routes/web.php'); | |
$routesFileContents = \Statamic\Facades\File::get($routesFile); | |
$routesFileContents .= "\n" . $str; | |
\Statamic\Facades\File::put($routesFile, $routesFileContents); | |
$relativeRoutesFile = \Statamic\Facades\Path::makeRelative($routesFile); | |
$this->info("Routes file $relativeRoutesFile updated."); | |
$this->line("You can now remove the <comment>routes</comment>, <comment>vanity</comment>, and <comment>redirect</comment> keys from config/statamic/routes.php"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the gist.