Skip to content

Instantly share code, notes, and snippets.

@jasonvarga

jasonvarga/console.php Secret

Last active Feb 5, 2020
Embed
What would you like to do?

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

This comment has been minimized.

Copy link

@mikemartin 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

This comment has been minimized.

Copy link

@ebeauchamps ebeauchamps commented Feb 1, 2020

+1

@jasonvarga

This comment has been minimized.

Copy link
Owner Author

@jasonvarga jasonvarga commented Feb 1, 2020

Updated the gist.

@mikemartin

This comment has been minimized.

Copy link

@mikemartin mikemartin commented Feb 2, 2020

@jasonvarga That worked. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.