Skip to content

Instantly share code, notes, and snippets.

@jasonvarga
Last active February 5, 2020 16:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonvarga/6cccccf9eb94729629936ab8a99292f3 to your computer and use it in GitHub Desktop.
Save jasonvarga/6cccccf9eb94729629936ab8a99292f3 to your computer and use it in GitHub Desktop.

Upgrading to the new routes in beta 13

  • Upgrade to Statamic 3.0.0-beta.13
  • Copy the contents of this gist's console.php into routes/console.php
  • Run php artisan route:convert
  • Delete the routes, vanity, and redirect arrays from config/statamic/routes.php
  • Delete the command you just copied. It was only temporary.
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");
});
@mikemartin
Copy link

mikemartin commented Feb 1, 2020

Running the round command after upgrading to v3.0.0-beta.14, but getting an amp error.

$ php artisan route:convert

In Collection.php line 1290:
                        
  Undefined index: amp  

@ebeauchamps
Copy link

+1

@jasonvarga
Copy link
Author

Updated the gist.

@mikemartin
Copy link

@jasonvarga That worked. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment